Wednesday, June 10, 2009

Linux: Find ip of known MAC address

Useful for DHCP only networks where you can't get a reservation and have multiple machines (laptop / desktop) and you reservations are short. Requires nmap, and nmap must run as root to get the scan the subnet and get the MAC. Copy and paste this into a script file.

#!/bin/bash
# enter known values
other_mac=00:00:00:00:00:00 <-- change for the mac of the machine you want to find
subnet=1.2.3.1-254 <--change for your subnet

# don't modify from here on down
# set current date stamp
prefix=`echo $subnet | /usr/bin/cut -d "." -f1`

# find the mac of wanted pc with nmap and get current ip
# requires nmap, and nmap has to run as root, you can add your account to sudoers with visudo
# username ALL= NOPASSWD: /usr/bin/nmap
newip=`/usr/bin/sudo /usr/bin/nmap -nsP $subnet | /bin/grep -B1 -i "$other_mac" | grep $prefix | /usr/bin/cut -d " " -f2 | egrep -iv "[a-z]|[A-Z]"`

#do whatever you want here, add to your hosts file, launch command with ip, etc...
#vncviewer $newip -compresslevel 0 -quality -5
echo $newip

No comments:

Post a Comment