gpt4 book ai didi

linux - 获取 DHCP 服务器的 IP

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:00 25 4
gpt4 key购买 nike

我想将 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 .

最佳答案

为了提高兼容性,我最终选择了一个简单的解决方案,即每秒向 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

关于linux - 获取 DHCP 服务器的 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com