Find out the mac address

K

K. R.

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!
 
S

Stephen Kyle

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)
 
S

Stephen Kyle

Of course I meant if 'ip route show' doesn't say the command wasn't found.
- S

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)
 
A

ara.t.howard

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!

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/
 
K

K. R.

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

Aaron Turner

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 <IP>' command. It's reasonably portable and easy to
parse.

--
Aaron Turner
http://synfin.net/
http://tcpreplay.synfin.net/ - Pcap editing & replay tools for Unix
They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety. -- Benjamin Franklin
 
K

Konrad Meyer

--nextPart10856235.kSaMqtL6qM
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Quoth Stephen Kyle:
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:
=20
# get your gateway IP address
gateway =3D `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=
=20
decimals))
# get the mac address for the gateway
mac_address =3D `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.
=20
Steve (abuser of match)
=20

Regards,
=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart10856235.kSaMqtL6qM
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHKMjCCHB0oCiR2cwRAtmhAKDLWPRPB1g1FvDLw50SLvw86EmnngCfYwd8
u7KK3GxVG+EZfOwNYq6DK6E=
=1tZK
-----END PGP SIGNATURE-----

--nextPart10856235.kSaMqtL6qM--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top