alzhaimer has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

is there an elegant way to ascertain the MAC address of a given network device and, if it's wireless, the MAC address of the access point where the NIC is logged in?

I want to run it locally on several Unices, including Linux and actually don't want to parse the different output printed by ifconfig,iwconfig,etc. The /proc FS isn't a solution either, because it is depricated in Unix. I thought Perl is a good choice because its platform independency.

I found some articles, but they did not cover the access point's MAC adress.

Thanks Niklas
  • Comment on Getting MAC Adress from wireless device and access point

Replies are listed 'Best First'.
Re: Getting MAC Adress from wireless device and access point
by zengargoyle (Deacon) on Nov 11, 2003 at 11:12 UTC

    ping the IP of the network device, then check your ARP cache. if the IP<->MAC mapping is present then you are in the same broadcast domain as the network device and the MAC address is relevant. if the IP<->MAC mapping is not present in your ARP cache then you are in a different broadcast domain than the network device and the MAC is unavailable (through any direct means).

    if the network device is wireless then you will not be able to determine the MAC nor the IP of the access point through any direct means.

    you may have access to indirect means like SNMP. then anything is possible but you need the right passwords for the access points.

    if you have the IP of a network device and the passwords to the access points (and a list of the access points) then you can query the access point for the IP<->MAC mapping of all current connections (by IP or MAC). query each access point untill you find the IP/MAC you are interested in.

    depending on the access point and it's SNMP funcionality you may find the information in different places and have to find the right little bit.

    # on an Enterasys R2 AP it goes a bit like this. sub mac2oid { join '.', map {hex} split ':', $_[0] } my $macoid = mac2oid( '0:de:ad:be:ee:ef' ); my ($ifoid) = qw( .1.3.6.1.2.1.17.7.1.2.2.1.2.1 ); my ($howoid)= qw( .1.3.6.1.2.1.17.7.1.2.2.1.3.1 ); my $gets = [ [ $ifoid, $macoid ], [ $howoid, $macoid ], ]; use SNMP; ... # $session->get( $gets ); # i have an async module that # scatters the gets across a # list of access points that # won't fit in this margin. my $interface = $gets->[0]->[2]; my $how = $gets->[1]->[2]; if ( $interface & 0x6 and $how = 3 ) { # on this switch the MAC has been seen # on the wireless interface (over the radio) # and was learned automatically (vs learned from mgmt # or learned from a filer) # found which AP the MAC was on! # do something }

    in my case i always can find the MAC of the network device in question, then i query all of the access points looking for the one (or more) that have seen that MAC address on their radio interface (vs seeing the MAC on their wired interface). this gets me which access points the network device is using.

    my next step is usually to add them to the filter on the access point to deny them wireless access. (MuAhahah)

Re: Getting MAC Adress from wireless device and access point
by Abigail-II (Bishop) on Nov 11, 2003 at 11:06 UTC
    I've recently written a program where I needed to get the MAC address of a local interface. I used an ioctl call, with SIOCGIFHWADDR as second parameter. This was in C, but Perl also has an ioctl function - or you could write a piece of XS code to do it for you.

    Abigail

Re: Getting MAC Adress from wireless device and access point
by Anonymous Monk on Nov 11, 2003 at 11:32 UTC
Re: Getting MAC Adress from wireless device and access point
by Anonymous Monk on Nov 11, 2003 at 11:14 UTC
Re: Getting MAC Adress from wireless device and access point
by Anonymous Monk on Nov 11, 2003 at 11:01 UTC
    I found some articles, but they did not cover the access point's MAC adress
    Which articles?