get a DHCP server's IP

1.3k views Asked by At

I wanted to get the IP of my DHCP server into a bash variable.

like : IP="192.168.1.254"

I know this IP can be found in /var/lib/dhcp/dhclient.leases or in /var/log/syslog but I don't know of to extract it and put it in variable during my script (bash)

EDIT: file dhclient.leases look's like

lease {
  interface "eth0";
  fixed-address 192.168.1.200;
  option subnet-mask 255.255.255.0;
  option routers 192.168.1.254;
  option dhcp-lease-time 7200;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.1.254;
  option dhcp-server-identifier 192.168.1.254;
  option host-name "bertin-Latitude-E6430s";
  option domain-name "laboelec";
  renew 1 2015/02/16 10:54:34;
  rebind 1 2015/02/16 11:53:49;
  expire 1 2015/02/16 12:08:49;
}

I want the IP from line option dhcp-server-identifier 192.168.1.254;.

1

There are 1 answers

0
Jérémy On BEST ANSWER

To more compatibility I finally opted for a simple solution which is to send the IP server like a string on broadcast every seconds. For that I use socat (because netcat can't send message to braodcast) my DHCP server run this script in background:

#!/bin/bash
interface="eth0"
IP=$(ifconfig $interface  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
Broadcast=$(ifconfig $interface  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f3 | awk '{ print $1}')
Port="5001"

while [ true ];
do
    sleep 1
    echo $IP | socat - UDP4-DATAGRAM:$Broadcast:$Port,so-broadcast
    #to listen: netcat -l -u $Broadcast -p $Port
done
exit 0