in reply to MAC address for Linux Machine

Machines don't have necessarely MAC addresses (although SUN/Solaris boxes will - sort of) - but network interfaces have. A machine can have 0 network interfaces, and hence, no MAC address. Or it could have a gazillion, and have a gazillion (or less) MAC addresses (more than one interface can have the same MAC address).

Anyway, to get back to your question. The easy way is to call ifconfig and parse its output. The hard way is to call ioctl (with the appropriate arguments), and unpacking its return value.

Abigail

Replies are listed 'Best First'.
Re: Re: MAC address for Linux Machine
by biosysadmin (Deacon) on Apr 04, 2004 at 06:01 UTC
    Right on, assuming that you're looking at the MAC address(es) of your local machine. If you want to find the address of a remote machine, it might be in your local ARP cache. Try parsing the output of /usr/sbin/arp on a Unix machine (keep in mind that the arguments to arp vary by operating system).

    Also, if the MAC address of interest isn't in your ARP cache, you can try pinging it with Net::Arping. It will allow you to ping a host using ARP packets and get the MAC address directly from the reply. This is a cool module, even if the documentation leaves a bit to be desired.

    Your system's ARP cache should cache the MAC addresses of machines on the local network with which it has recently communicated, so you may not even need to install the module depending on your environment.

    Here's another link on ARP and Linux if you're interested.

Re: Re: MAC address for Linux Machine
by Anonymous Monk on Apr 03, 2004 at 10:28 UTC
    how can use in perl programm /sbin/ifconfig -a to find MAC of any IP in Linux hope you help me
Re: Re: MAC address for Linux Machine
by Anonymous Monk on Apr 03, 2004 at 10:12 UTC
    % /sbin/ifconfig eth0 using this iam getting mac address,my application is that when i pass IP i want to get MAC address which is in Linux Server
      It's quite unclear to me what you want. What do you mean by MAC address which is in Linux Server? If all you want is to get the MAC address of an IP address, given that the IP address belongs to the local subnet, you could issue an arp request. But that has nothing to do with a "Linux Server", whatever that may be.

      Abigail

        sub unique { keys %{{ map { $_, 1 } @_ }} } my $ifconfig; -x and $ifconfig = $_ , last for qw(/sbin/ifconfig /bin/ifconfig); die "Can't find ifconfig\n" unless $ifconfig; my @hwaddrs = `$ifconfig` =~ /\b((?:[a-f\d]{2}:){5}[a-f\d]{2})\b/gi; { local ($\, $,) = ("\n", "\n") and print unique @hwaddrs }

        iam this code to get MAC address ,but my application is if i give IP i want to get MAC address of remote IP

        20040403 Edit by Corion: Added formatting

      A reply falls below the community's threshold of quality. You may see it by logging in.