Find out the mac address

Hi @all

How can I find out the mac address from the default gateway with a ruby
script?
Does exist any methods to read it out? The script runs on a linux
distribution…

thanks for your comment!

Of course I meant if ‘ip route show’ doesn’t say the command wasn’t
found.

  • S

On 31/10/2007, K. R. [email protected] wrote:

Hi @all

How can I find out the mac address from the default gateway with a ruby
script?
Does exist any methods to read it out? The script runs on a linux
distribution…
I don’t know if any methods, exist, but you could hack it up if you
have the ‘ip’ package installed on your system, ie. ‘ip show route’
doesn’t say the command wasn’t found.
Then you can try:

get your gateway IP address

gateway = ip route show.match(/default.*/)[0].match(/\d\d?\d?.\d\d?\d?.\d\d?\d?.\d\d?\d?/)[0]

get the mac address for the gateway

mac_address = ip neigh show.match(/#{gateway}.*/)[0].match(/…:…:…:…:…:…/)[0]

Steve (abuser of match)

thanks for the reply, but I search a solution to find out the mac
address from the default gw not my own mac address…

parse the output from tracert or traceroute??
K. R. wrote:9

Hi @all

How can I find out the mac address from the default gateway with a ruby
script?
Does exist any methods to read it out? The script runs on a linux
distribution…

thanks for your comment!

On Oct 31, 2007 3:02 AM, K. R. [email protected] wrote:

Hi @all

How can I find out the mac address from the default gateway with a ruby
script?
Does exist any methods to read it out? The script runs on a linux
distribution…

If you know the IP address of the gateway, then parse the output of
the ‘arp -n ’ command. It’s reasonably portable and easy to
parse.

On Oct 31, 2007, at 4:02 AM, K. R. wrote:

Posted via http://www.ruby-forum.com/.

this may not help you directly, but:

cfp:~ > ruby -r macaddr -e’ p Mac.addr ’
“00:17:f2:d5:c1:ec”

gem install macaddr

regards.

a @ http://codeforpeople.com/

Quoth Stephen Kyle:

Then you can try:

get your gateway IP address

gateway = ip route show.match(/default.*/)[0].match(/\d\d?\d?.\d\d?\d?.\d\d?\d?.\d\d?\d?/)
[0]
or .match(/((?:\d{1,3}.){3}(?:\d{1,3}))/)
(group (3 groups of 1-3 decimals followed by a period) with (a group of
1-3
decimals))

get the mac address for the gateway

mac_address = ip neigh show.match(/#{gateway}.*/)[0].match(/…:…:…:…:…:…/)[0]
this could also be more correct:
/((?:[\da-f]{2}:){5})(?:[\da-f]{2}))/
So it doesn’t match any character, but instead valid MAC addresses.

Steve (abuser of match)

thanks for your comment!

Regards,