Find out the mac address

Discussion in 'Ruby' started by K. R., Oct 31, 2007.

  1. K. R.

    K. R. Guest

    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!
    --
    Posted via http://www.ruby-forum.com/.
     
    K. R., Oct 31, 2007
    #1
    1. Advertising

  2. K. R.

    Stephen Kyle Guest

    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:

    # 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 your comment!
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Stephen Kyle, Oct 31, 2007
    #2
    1. Advertising

  3. K. R.

    Stephen Kyle Guest

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

    On 31/10/2007, Stephen Kyle <> wrote:
    > 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:
    >
    > # 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 your comment!
    > > --
    > > Posted via http://www.ruby-forum.com/.
    > >
    > >

    >
     
    Stephen Kyle, Oct 31, 2007
    #3
  4. K. R.

    ara.t.howard Guest

    On Oct 31, 2007, at 4:02 AM, 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...
    >
    > thanks for your comment!
    > --
    > 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/
    --
    share your knowledge. it's a way to achieve immortality.
    h.h. the 14th dalai lama
     
    ara.t.howard, Oct 31, 2007
    #4
  5. K. R.

    K. R. Guest

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

    --
    Posted via http://www.ruby-forum.com/.
     
    K. R., Oct 31, 2007
    #5
  6. K. R.

    Roger Pack Guest

    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!


    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Oct 31, 2007
    #6
  7. K. R.

    Aaron Turner Guest

    On Oct 31, 2007 3:02 AM, 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...


    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
     
    Aaron Turner, Oct 31, 2007
    #7
  8. K. R.

    Konrad Meyer Guest

    --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--
     
    Konrad Meyer, Oct 31, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arun

    Find MAC address.

    Arun, Nov 3, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    7,130
    John Saunders
    Nov 4, 2003
  2. UJ
    Replies:
    4
    Views:
    716
    John Rivers
    Sep 2, 2005
  3. SJ

    MAC Address/Physical Address?

    SJ, Jan 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    528
  4. Jay
    Replies:
    13
    Views:
    12,511
    Mark McIntyre
    Oct 27, 2003
  5. Daniel T.

    How to find MAC address and uids?

    Daniel T., Nov 10, 2004, in forum: Python
    Replies:
    4
    Views:
    3,861
    Mike Hall
    Nov 10, 2004
Loading...

Share This Page