[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Find out the mac address

K. R.

10/31/2007 10:02:00 AM

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-....

7 Answers

Stephen Kyle

10/31/2007 11:02:00 AM

0

On 31/10/2007, K. R. <mcse@palstek.ch> 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-....
>
>

Stephen Kyle

10/31/2007 11:04:00 AM

0

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

On 31/10/2007, Stephen Kyle <jimmykane@gmail.com> wrote:
> On 31/10/2007, K. R. <mcse@palstek.ch> 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-....
> >
> >
>

ara.t.howard

10/31/2007 3:18:00 PM

0


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-....
>

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://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama



K. R.

10/31/2007 4:08:00 PM

0

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-....

Roger Pack

10/31/2007 4:08:00 PM

0

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-....

Aaron Turner

10/31/2007 4:44:00 PM

0

On Oct 31, 2007 3:02 AM, K. R. <mcse@palstek.ch> 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://s...
http://tcpreplay.s... - 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

Konrad Meyer

10/31/2007 6:26:00 PM

0

Quoth Stephen Kyle:
> On 31/10/2007, K. R. <mcse@palstek.ch> 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]
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,
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...