linux - 获取 DHCP 服务器的 IP

标签 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 .

最佳答案

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

相关文章:

linux - 使用 Bash 迭代计算斐波那契数

Linux Bash 脚本和 Mongo

c - 这个代码段告诉我什么?

c# httplistener 只捕获本地ip请求

c - Pcap 函数有 "undefined reference"

bash - 引用 LSF 作业数组中的作业索引

python - 使用参数从 python 执行 Shell 脚本

linux - 导出在另一个文件中定义的变量

sockets - Debian - 每个用户的默认 IP?

http - TCP 在没有 SYN 的情况下启动?