【问题标题】:get a DHCP server's IP获取 DHCP 服务器的 IP
【发布时间】:2015-09-04 03:40:12
【问题描述】:

我想将我的 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。

【问题讨论】:

  • 把你要解压的文件的内容贴出来,主行前后一两行就可以了。
  • 你没有说你正在使用什么操作系统,在我的 Linux Mint 17.1 上,/var/lib/dhcp/dhclient.leases 文件是空的,即使我的网络适配器正在使用 DHCP,如果/何时你会做什么/var/log/syslog 文件已翻转,尚未包含任何网络信息。
  • 字符串option dhcp-server-identifier在文件中是否唯一?
  • 我不知道/var/lib/dhcp/dhclient.leases可能是空的!
  • option dhcp-server-identifier 不是唯一的。这里“偶然”只有“eth0”接口,但它也可能有“wlan0”接口,其中包含相同的行结构。

标签: linux bash ip server dhcp


【解决方案1】:

为了提高兼容性,我最终选择了一个简单的解决方案,即每隔几秒发送一次 IP 服务器,就像一个字符串一样。为此,我使用 socat(因为 netcat 无法向广播发送消息) 我的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多