获取 DHCP 服务器的 IP
get a DHCP server's IP
我想将我的 DHCP 服务器的 IP 放入 bash 变量中。
喜欢:IP="192.168.1.254"
我知道这个 IP 可以在 /var/lib/dhcp/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。
为了更好的兼容性,我最终选择了一个简单的解决方案,即每秒向 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 }')
Broadcast=$(ifconfig $interface | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f3 | awk '{ print }')
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
我想将我的 DHCP 服务器的 IP 放入 bash 变量中。
喜欢:IP="192.168.1.254"
我知道这个 IP 可以在 /var/lib/dhcp/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。
为了更好的兼容性,我最终选择了一个简单的解决方案,即每秒向 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 }')
Broadcast=$(ifconfig $interface | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f3 | awk '{ print }')
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