获取DHCP服务器的IP

时间:2015-06-18 13:15:24

标签: linux bash ip server dhcp

我想将我的DHCP服务器的IP转换为bash变量。

喜欢:IP="192.168.1.254"

我知道这个IP可以在 /var/lib/dh​​cp/dhclient.leases / var / log / syslog 中找到,但我不知道在我的脚本(bash)中提取它并将其放入变量

编辑:文件 dhclient.leases 看起来像

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;
}

我想要来自option dhcp-server-identifier 192.168.1.254;行的IP。

1 个答案:

答案 0 :(得分:0)

为了获得更高的兼容性,我最终选择了一个简单的解决方案,即每隔一秒就在广播上发送一个像字符串一样的IP服务器。为此,我使用socat(因为netcat不能向braodcast发送消息) 我的DHCP服务器在后台运行这个脚本:

#!/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