Various nuggets of useful technical information.

Thursday, June 26, 2008

Obtaining machine name to IP mappings from DHCP Server on Linux

Say you're running dhcpd on Linux.

Need to know what IP a certain machine has gotten (and you don't have access to that machine?)
or
Need to know what machine a given IP belongs to ?

One needs to look in the following file:
/var/lib/dhcpd/dhcpd.leases

Since, this file contains all the mappings and lease times handed out (and a lot of redundant information), you may want to grep the file for the information you need. On top of that the information you need may be located several lines away from your search string, so look at the man page for grep for added functionality.

I use the following simple scripts:

To find the IP from the netbios name/machine name:
see_ip [netbios_name]

#!/bin/bash

# Automates display of machines' DHCP IP on LINUX
# Shows machine name and IP in one go from dhcpd.leases file

# Show the machine in /var/lib/dhcpd/dhcpd.leases
# Since the machine name is the 8th line in a stack of info about it
# Display the preceding 7 lines as well
echo "Machine details in DHCP database:"
cat /var/lib/dhcpd/dhcpd.leases | grep -B 7 $1


To find the netbios name/machine name from the IP address:
see_netbios [ip_address]

#!/bin/bash

# Automates display of DHCP IP's machine name on LINUX
# Shows machine name and IP in one go from dhcpd.leases file

# Show the machine in /var/lib/dhcpd/dhcpd.leases
# Since the IP is the 1st line of 8 in a stack of info about it
# Display the following 7 lines as well
echo "IP details in DHCP database:"
cat /var/lib/dhcpd/dhcpd.leases | grep -A 7 $1



No comments: