Sign in to follow this  
Followers 0
gruntydatsun

Extract MAC from STDOUT using REGEX`

6 posts in this topic

First of all, my apologies for my woeful lack of regex skill. Now my question.

I am trying to extract the mac address from and windows commandline of:

arp -a <IPADDRESS>

First I ping the address to populate the arp table, then I run the arp -a command and capture the stdout to a string.

Then I want to hit the string with a regex to extract the mac address.

Below is the code I have so far.

#include <Constants.au3>
#include <Array.au3>

$network = "165.240.164"
$startip = InputBox("FIRST IP","Enter first octet of first IP address")
$endip = Inputbox("LAST IP","Enter last octet of last IP address")
$network = "165.240.164"
Local $text

for $x = $startip to $endip
    Run(@ComSpec & " /c " & 'ping -n 5 ' & $network & '.' & $x, "", @SW_HIDE)
    $foo = Run(@ComSpec & " /c " & 'arp -a ' & $network & '.' & $x, "", @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)

    
    While 1
        $line = StdoutRead($foo)
        If @error Then ExitLoop
        $text &= $line
    Wend

    $text = StringStripWS($text,8)   ;trying to get rid of special characters

    ;curse you regular expression!!!!
    $res = StringRegExp($text, "^[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}", 1) 

    if IsArray($res) then
        _ArrayDisplay($res)
        msgbox(1,"MAC IS",$res[0])
        $res[0] = ""
        Sleep(1500)
    EndIf
    $text = ""
Next

Share this post


Link to post
Share on other sites



"(?i)((?:[a-f\d]{2}\-){5}[a-f\d]{2})\W"


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

"(?i)((?:[a-f\d]{2}\-){5}[a-f\d]{2})\W"

Just to modify that a bit using the xdigit class.

"((?:[[:xdigit:]]{2}\-){5}[[:xdigit:]]{2})\W"

Should be the same and case sense is not required.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thanks SmOke_N,

Another crispy, nutritious answer. That regex is right on the money. :mellow:

After adding it, one line of my code was breaking the program:

$text = StringStripWS($text,8)

Once I got rid of that it works like a charm. Here's the working code:

#include <Constants.au3>            ;for STDOUT_CHILD & STDERR_CHILD

Local $network = "165.240.164"
Local $startip = InputBox("FIRST IP","Enter first octet of first IP address")
Local $endip = Inputbox("LAST IP","Enter last octet of last IP address")
Local $network = "165.240.164"
Local $text

for $x = $startip to $endip
    Run(@ComSpec & " /c " & 'ping -n 5 ' & $network & '.' & $x, "", @SW_HIDE)   ;ping ip to populate arp table
                                                                                ;get arp table entry for IP
    $foo = Run(@ComSpec & " /c " & 'arp -a ' & $network & '.' & $x, "", @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
    While 1
        $line = StdoutRead($foo)    ;read stdout into line
        If @error Then ExitLoop     ;if no more stdout get out of loop
        $text &= $line              ;append this line of $text into $line
    Wend
    msgbox(1,"TEXT",$text)

    $res = StringRegExp($text, "(?i)((?:[a-f\d]{2}\-){5}[a-f\d]{2})\W", 1)  ;get MAC out of $text thanks SmOke_N

    if IsArray($res) then               ;if regex works $res is an array
        msgbox(1,"MAC IS",$res[0])
        Sleep(1500)
    EndIf
    
    $text = ""
Next

Share this post


Link to post
Share on other sites

Just to modify that a bit using the xdigit class.

"((?:[[:xdigit:]]{2}\-){5}[[:xdigit:]]{2})\W"

Should be the same and case sense is not required.

There's a difference, I did mine the way I did to save the 7 chars :mellow: .

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

That's called shortcuting and whoever it was that got me rolling with RegEx explained that it isn't good to shortcut.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
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
Sign in to follow this  
Followers 0