I have a shell script which uses etherwake to wake up a machine on my local network. After the machine is awake, I'm not sure of the IP address.
While trying to answer my own question I came up with:
ip=$(ping -c 1 hostname | head -1 | awk '{print $3}' | sed 's/[()]//g')
This solution stipulates that I know the hostname of the remote machine, which isn't so onerous.
Is there a way to get the IP if all I know is the MAC address?
Can you find an IP address from a MAC address? Yes. Open a Command Prompt window and enter the command arp -a. The output shows all of the IP addresses that are active on your network.
To see all the MAC addresses and their associated IP addresses, type “arp -a”. This command will list all the available MAC addresses in the system. The address on the left is the IP address, while the right is the MAC address.
MAC addresses can sometimes be used to identify the maker and potentially model of the device even without the device in hand. This is called the OUI (organizationally unique identifier).
It's easiest to find the command prompt from the search box in the Start menu. For Mac devices, the application is known as Terminal and is found in the Utilities folder. Once you've located the command prompt, open it and you'll see a black DOS screen appear. Now, you can use a ping to find the IP address.
I don't think there is a single command to do this. One hack would be to do a ping scan or a broadcast ping on the subnet and then query the arp table for the IP address of the MAC address. Obviously not an ideal solution. Example:
nmap -sP 192.168.1.0/24 >/dev/null && arp -an | grep <mac address here> | awk '{print $2}' | sed 's/[()]//g'
Here nmap will do a ping scan and populate your arp cache. Once the scan is done, the arp command can be used to print the arp table and then you pull out the IP address with grep/awk. You could try replacing nmap with a broadcast ping, but that probably isn't as reliable.
I would simply use
ip neighbor | grep -i "00:1E:C9:56:3C:8E" | cut -d" " -f1
The other methods presented here were unreliable, e.g. the output of ip neighbor
did not always contain the most recent state, so I ended up re-scanning the network using arp-scan
, and hence I simply used the output of the scanning to obtain the IP address for a given MAC address.
For scanning a single network interface, simply use this:
arp-scan -q -l --interface en4 2>/dev/null | grep "00:1E:C9:56:3C:8E" | cut -d$'\t' -f1
The following command scans multiple network interfaces at once:
{ arp-scan -q -l --interface en0 2>/dev/null & arp-scan -q -l --interface en4 2>/dev/null } | grep "00:1E:C9:56:3C:8E" | cut -d$'\t' -f1
You could try the arp command and grep by mac address
arp -a | grep "00:00:00:00:00:00"
(replace with your own mac addr)
I wrote a python module that can do this:
>>> from ethip import ethip
>>> print ethip.getip('00:1E:C9:56:3C:8E', '10.5.42.255')
10.5.42.3
I just makes rapid arp requests to find the ip, then caches what it finds. The code is on github.
I know is old, but the simplest way in linux is:
arp -a | grep "00:1E:C9:56:3C:8E"
The point of this is to ignore if is connected in one or another network meanwhile each device can see each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With