如果没有从DHCP获取静态IP(脚本) [英] Set static ip if not obtained from DHCP (script)

查看:154
本文介绍了如果没有从DHCP获取静态IP(脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在linux上使用嵌入式设备。我想先使用DHCP客户端,但如果DHCP服务器没有应答,我想设置静态默认IP。我想这应该不复杂,但我没有找到严格的答案。

I work on embedded device with linux on it. I want to use DHCP client first, but if there will be no answer from DHCP Server I want to set static-default IP. I suppose it shouldn't be complicated, but I haven't found strict answer.

我正在考虑2个解决方案(不幸的是我可以在几天内测试它们) :

I'm thinking about 2 solutions (Unfortunately I can test them in few days):


  1. 我用ifconfig设置静态IP,然后调用udhcpc。如果udhcpc无法获得新IP,旧的IP将保留。

  1. I set static IP with ifconfig, then I call udhcpc. If udhcpc will not obtain new IP, old one will stay.

我也可以先调用udhcpc,稍等片刻,检查是否获得了IP。但这对我来说并不好。我不想在启动时添加任何等待例程。

I can also first call udhcpc, wait a while and check if IP is obtained. But this is not nice for me. I wouldn't like to add any wait routines into startup.

BR
Bartek

BR Bartek

我使用udhcpc - 类似于:

I use udhcpc - something like:

udhcpc -n -f -i eth0 
if ifconfig | grep -A1 eth0 | grep inet 
    then 


推荐答案

dhclient应支持回退通过租赁声明
查看 dhclient.conf 手册页。

dhclient should support fallback via lease declaration have a look at the dhclient.conf man page.

添加类似这样的内容到你的 dhclient.conf

Add something like this to your 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;
}

或者您可以为接口分配第二个IP,例如 / etc / network / interfaces

or you can assign a second IP to the interface like /etc/network/interfaces

auto lo
iface lo inet loopback
iface eth0 inet dhcp

auto eth0:1
iface eth0:1 inet static
address 10.10.10.2
netmask 255.255.255.0

这篇关于如果没有从DHCP获取静态IP(脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆