【问题标题】:Set static IP from text file if not obtained via DHCP (sed)如果未通过 DHCP (sed) 获取,则从文本文件中设置静态 IP
【发布时间】:2016-01-05 22:59:22
【问题描述】:

当没有通过DHCP获取IP地址时,我想设置一个静态IP,我之前写在一个文本文件中。

我的 staticIP.txt 的内容:

10.10.0.254

到目前为止,当 DHCP 不起作用时,我得到了一个静态 IP。我为此编辑了/etc/dhcp/dhclient.conf

timeout 10;
lease {
interface "eth0";
fixed-address 10.0.0.10;
option subnet-mask 255.255.255.0;
renew 2 2022/1/1 00:00:01;
rebind 2 2022/1/1 00:00:01;
expire 2 2022/1/1 0:00:01;
}

(来源:Set static ip if not obtained from DHCP (script)

现在我想覆盖“fixed-address 10.0.0.10;”在我的 staticIP.txt 中使用静态 IP“10.10.0.254”,当没有获得 DHCP 时。

到目前为止,我所能做的就是替换“固定地址;”用 sed 阻塞并将其写入文件:

sed -i 's/fixed-address [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}/<MANUAL ENTERED IP>/g' /etc/dhcp/dhclient.conf

现在缺少两件事:

  1. 从 staticIP.txt 中提取 IP 并覆盖“fixed-address [IP];”

  2. 检查是否没有通过 DHCP 获取 IP 并运行 sed 命令

如果有人可以帮助我,我会很高兴。

非常感谢。

【问题讨论】:

    标签: linux sed ip dhcp ifconfig


    【解决方案1】:

    为什么要在文件中保留一个 IP 地址? 直接加入正则表达式不是更好吗?

    sed -i 's#fixed-address 10.0.0.10;#fixed-address 10.10.0.254;#' /etc/dhcp/dhclient.conf
    

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 2014-02-19
      • 2015-10-14
      • 2018-04-09
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多