From: Konrad Meyer Date: 2007-11-01T03:26:12+09:00 Subject: Re: Find out the mac address --nextPart10856235.kSaMqtL6qM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth Stephen Kyle: > On 31/10/2007, K. R. 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: >=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 > > > > thanks for your comment! Regards, =2D-=20 Konrad Meyer 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--