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

I'm trying to get the mac address of a workstation on my domain and I only have the IP address

I understand I need NETPACKET module to use the ARP submodule

I essentially want to replicate the following:

arp -a <IP Address>

I'm not too clear from the NETPACKET::ARP readme and example how to accomplish this.. any help (example would be best served.. thanks!!)

20040113 Edit by Corion: Moved from Perl Monks Discussion to SoPW, added formatting

Replies are listed 'Best First'.
Re: How to get Mac Address from IP
by Abigail-II (Bishop) on Jan 13, 2004 at 15:43 UTC
    Well, you could always use
    my ($mac) = `arp -a $ip_address` =~ /at\s+(\S+)\s+/;
    although you may have to adjust the regex to match the wording of your particular arp.

    Abigail

      You need to be careful about WHOM you request the ARP info from.

      If the workstation is on a different subnet than the querier, an ARP will cause your router to respond with a "Proxy ARP", and you will end up with the mac address of the router - this is usaually a GOOD thing, since the router will help you send traffic to it's destination, but is that what you really want? The original poster's intent was not clear.

      In the Windows world, you can get a remote machine's MAC address running

      NBTSTAT -A <IP-addr>
      on your workstation.

      "When you are faced with a dilemma, might as well make dilemmanade. "
Re: How to get Mac Address from IP
by b10m (Vicar) on Jan 13, 2004 at 15:39 UTC

    Based solely on the "Name" (for I have no experience with neither one of them), I think you're looking at the wrong module.

    NetPacket::ARP: "Assemble and disassemble ARP (Address Resolution Protocol) packets."
    Net::Address::Ethernet: "find hardware ethernet address."
    --
    b10m
      Sorry b10m, but bengmau is looking for the MAC of a remote system on his domain. According to CPAN, Net::Address::Ethernet only finds the MAC of the local machine.

      Unfortunately bengmau, I don't have an answer strictly in perl for you yet, I just didn't want you to get frustrated with this module. Abigail-II's solution below works great if there's already an entry in your arp table. You might want to ping the ip just before you do the arp -a on it... Oh yeah, and make sure that either arp is in your path or address it explicitly.