35

I need to write a bash script wherein I have to create a file which holds the details of IP Addresses of the hosts and their mapping with corresponding MAC Addresses.

Is there any possible way with which I can find out the MAC address of any (remote) host when IP address of the host is available?

32

If you just want to find out the MAC address of a given IP address you can use the command arp to look it up, once you've pinged the system 1 time.

Example

$ ping skinner -c 1
PING skinner.bubba.net (192.168.1.3) 56(84) bytes of data.
64 bytes from skinner.bubba.net (192.168.1.3): icmp_seq=1 ttl=64 time=3.09 ms

--- skinner.bubba.net ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 3.097/3.097/3.097/0.000 ms

Now look up in the ARP table:

$ arp -a
skinner.bubba.net (192.168.1.3) at 00:19:d1:e8:4c:95 [ether] on wlp3s0

fing

If you want to sweep the entire LAN for MAC addresses you can use the command line tool fing to do so. It's typically not installed so you'll have to go download it and install it manually.

$ sudo fing 10.9.8.0/24

    fing example

References

  • I know this is an old answer, but do you have any insight as to how Fing is implemented? I am trying to learn about this layer of networking and the tools to monitor it. – akaphenom Mar 8 '16 at 20:31
  • @akaphenom If you have new questions please them outright, comments aren't meant for that. – slm Mar 9 '16 at 0:33
7

You can use arp command:

arp -an

But you can only use this command in LAN, if you want to find out the MAC address of any remote host, maybe you must use some tool to capture the packet like tcpdump and parsing the result.

  • 1
    tcpdump(8) will only show you the local MACs (i.e., the MAC of the last leg router). The MAC layer headers of input packets are stripped out by the router, and new ones added to the outgoing packet. – vonbrand Feb 9 '16 at 15:15
5

This is from my question and answer in askubuntu.

You can use the command

   sudo nmap -sP -PE -PA21,23,80,3389 192.168.1.*

nmap: Network exploration tool and security / port scanner. From the manual:

-sP (Skip port scan) . This option tells Nmap not to do a port scan after host discovery, and only print out the available hosts that responded to the scan. This is often known as a “ping scan”, but you can also request that traceroute and NSE host scripts be run. This is by default one step more intrusive than the list scan, and can often be used for the same purposes. It allows light reconnaissance of a target network without attracting much attention. Knowing how many hosts are up is more valuable to attackers than the list provided by list scan of every single IP and host name.

-PE; -PP; -PM (ICMP Ping Types) . In addition to the unusual TCP, UDP and SCTP host discovery types discussed previously, Nmap can send the standard packets sent by the ubiquitous ping program. Nmap sends an ICMP type 8 (echo request) packet to the target IP addresses, expecting a type 0 (echo reply) in return from available hosts.. Unfortunately for network explorers, many hosts and firewalls now block these packets, rather than responding as required by RFC 1122[2]. For this reason, ICMP-only scans are rarely reliable enough against unknown targets over the Internet. But for system administrators monitoring an internal network, they can be a practical and efficient approach. Use the -PE option to enable this echo request behavior.

-A (Aggressive scan options) . This option enables additional advanced and aggressive options.

21,23,80,3389 Ports to search through

192.168.1.* Range of IPs. replace with yours.

  • -sP is for "scanPing", it is also -sn in never versions of nmap it seems. – meawoppl May 12 '15 at 18:14
4

arping

arping -I <interface> -c 1 <host>

The command should return the MAC address in the response. Something like,

$ arping -I eth0 -c1 192.168.1.2
ARPING 192.168.1.2 from 192.168.1.5 eth0
Unicast reply from 192.168.1.2 [08:01:27:38:EF:32]  0.746ms
Sent 1 probes (1 broadcast(s))
Received 1 response(s)

arping is provided by package iputils-arping on Debian.

  • arping also needs to be told which interface to use, with the -I option. – Stephen Kitt Feb 9 '16 at 14:09
  • You can use arping -f to quit after the first reply. – Ryan Griggs Sep 28 '18 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.