Jump to content

arp -a output .txt


Recommended Posts

What about a function:
Credit to jguinch for RegExp

#Include <Array.au3>
#Include <AutoItConstants.au3>
$aARP_192168 = ARP_OUTPUT('192.168', 'dynamic')
_ArrayDisplay($aARP_192168)

Func ARP_OUTPUT($ipRange, $ipType)
    Local $iPid = Run("cmd /c arp -a","",@SW_HIDE,$STDOUT_CHILD)
    ProcessWaitClose($iPid)

    Local $sArp = StdoutRead($iPid)
    Local $aArpTable = StringRegExp($sArp, "((?:\d+\.){3}\d+)\h+((?:[[:xdigit:]]{2}-){5}[[:xdigit:]]{2})\h+(\V+)", 3)
    Local $aArpTable2D[0][3]
    For $i = 0 To UBound($aArpTable) - 1 Step 3
        If StringStripWS($aArpTable[$i + 2], 7) <> $ipType Then ContinueLoop
        If StringLeft(StringStripWS($aArpTable[$i], 7), StringLen($ipRange)) <> $ipRange Then ContinueLoop
        _ArrayAdd($aArpTable2D, $aArpTable[$i] & '|' & $aArpTable[$i + 1] & '|' & $aArpTable[$i + 2])
    Next
    Return $aArpTable2D
EndFunc

 

Link to comment
Share on other sites

  • 2 months later...

Try the following, you can use partial string (left to right) of what ever you like to search for e.g IP Address "192.168", Mac Address "90-"or Type "Static", you can leave fields blank to return the entire arp list.

#Include <Array.au3>
#Include <AutoItConstants.au3>
$aARP_192168 = ARP_OUTPUT('', '90-', '')
_ArrayDisplay($aARP_192168)

Func ARP_OUTPUT($vIPAddress = '', $vMacAddress = '', $vType = '')
    Local $iPid = Run("cmd /c arp -a","",@SW_HIDE,$STDOUT_CHILD)
    ProcessWaitClose($iPid)
    Local $sArp = StdoutRead($iPid)
    Local $aArpTable = StringRegExp($sArp, "((?:\d+\.){3}\d+)\h+((?:[[:xdigit:]]{2}-){5}[[:xdigit:]]{2})\h+(\V+)", 3)
    Local $aArpTable2D[0][3]
    For $i = 0 To UBound($aArpTable) - 1 Step 3
        If $vIPAddress <> '' And StringLeft(StringStripWS($aArpTable[$i], 7), StringLen($vIPAddress)) <> $vIPAddress Then ContinueLoop
        If $vMacAddress <> '' And StringLeft(StringStripWS($aArpTable[$i + 1], 7), StringLen($vMacAddress)) <> $vMacAddress Then ContinueLoop
        If $vType <> '' And StringStripWS($aArpTable[$i + 2], 7) <> $vType Then ContinueLoop
        _ArrayAdd($aArpTable2D, $aArpTable[$i] & '|' & $aArpTable[$i + 1] & '|' & $aArpTable[$i + 2])
    Next
    Return $aArpTable2D
EndFunc

 

Link to comment
Share on other sites

RegEx confuses me as well, for me the easiest method for breaking down a regular expression is to use https://regex101.com, there are some tools you can download as well, but prefer this method personally:  Insert the regular expression pattern in the "Regular Expression" pane:

((?:\d+\.){3}\d+)\h+((?:[[:xdigit:]]{2}-){5}[[:xdigit:]]{2})\h+(\V+)

Then add a line from cmd arp-a to the "Test String" panel:

192.168.150.255       ff-ff-ff-ff-ff-ff     static

On the right hand side panels it explains everything rather nicely and is color coded.

Hope that helps.

Link to comment
Share on other sites

\d : One digit
\d+ : One or more digits
\h : One horizontal whitespace
\h+ : One or more horizontal whitespaces
\. : . (dot)
\V+ :  One or more characters mathing a non-vertical character
[:xdigit:] : One hexadecimal digit
[:xdigit:]{2} : Two hexadecimal digits
() : Capturing group (what you want to retrieve in the returned array)
(?:) : Non-capturing group (useful to use with quantifier)



((?:\d+\.){3}\d+)\h+((?:[[:xdigit:]]{2}-){5}[[:xdigit:]]{2})\h+(\V+)  :

((?:\d+\.){3}\d+) : (1st group) (One or more digits ,and a dot) 3 times, and one or more digits
\h+  : One or more horizontal whitespaces
((?:[[:xdigit:]]{2}-){5}[[:xdigit:]]{2}) : (2nd group) (Two hexadecimal digits and a dash) 5 times, and two hexadecimal digits
\h+ : One or more horizontal whitespaces
(\V+) : (3rd group) : One or more characters mathing a non-vertical character (used to match all characters until the end of the line)

 

https://regex101.com/r/aPdHcf/1

Edited by jguinch
Link to comment
Share on other sites

  • 3 weeks later...

I have a question to the expert. I can use arp -s $ipaddress $Macaddress to write the the ip address to the device interface, however is there a way of to programmatically add the gateway and the subnet also. Or by pinging the device it will add the correct gateway and subnet.

Thanks ahead

 

 

Link to comment
Share on other sites

#Include <Array.au3>
#Include <AutoItConstants.au3>

Local $sCmd = 'for /f "tokens=1,2 delims= " %a in (''arp -a ^| findstr "dyn stat"'') do @echo %a;%b'

Local $iPid = Run(@ComSpec & " /c " & $sCmd,@SystemDir,@SW_HIDE,$STDOUT_CHILD)
ProcessWaitClose($iPid)

Local $sArp = StdoutRead($iPid)
Local $aData = StringRegExp($sArp, "([^;]+);(\V+)", 4)
Local $aResult[UBound($aData)][2]
For $i = 0 To UBound($aData) - 1
    $aResult[$i][0] = ($aData[$i])[1]
    $aResult[$i][1] = ($aData[$i])[2]
Next

_ArrayDisplay($aResult)

can you explain in a lemon term that line below

Local $sCmd = 'for /f "tokens=1,2 delims= " %a in (''arp -a ^| findstr "dyn stat"'') do @echo %a;%b'

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...