Set static ip if failed to get dhcp ip using udhcp in OpenWrt GUI

Hi,

I have one more issue in openwrt GUI. By default, openwrt opens in static IP i.e., eg: 192.168.1.1.
Suppose we have changed this static IP to DHCP IP and forgot to put the LAN then the board will not get IP from DHCP then it should get the default static IP.
I am using udhcp for DHCP IP. So it should support fallback.
Can anyone help me how can I set static IP after fail to get DHCP IP in 15 sec using UDHCP.

Thank in advance.

Regards
Sakshi

You can use hotplug to override persistent configuration with runtime one and reload network service:
https://openwrt.org/docs/guide-user/base-system/hotplug

Hi @vgaetera

Thanks for your reply.
I want to know that how we will run this hotplug.d when not able to get DHCP IP.
Is there any file in which I have to call this hotplug.d file.

I have read somewhere on google about dhclient.conf which is used when we are using dhcpc but in my case, I am using udhcp for DHCP IP.

Thank in advance

Regards
Sakshi

Hi,

I am having this problem as i have DHCP IP using "udhcp" command.
I have googled this thing and getting some answer that i have to add some line in dhclient.conf.
But i am not having dhclient.conf file in my board.

So anyone can help me in this as i dont know anything about this.

Thanks in advance

Regards
Sakshi

cat << "EOF" > /etc/hotplug.d/iface/95-dhcp-fallback
NET_ACTION="ifup"
NET_IFLO="lo"
NET_IF="lan"
NET_DELAY="15"
UCI_IP="192.168.1.123"
UCI_MASK="255.255.255.0"
UCI_GW="192.168.1.1"
UCI_DNS="192.168.1.1"
. /lib/functions/network.sh
network_flush_cache
if [ "${ACTION}" = "${NET_ACTION}" -a "${INTERFACE}" = "${NET_IFLO}" ]
then
sleep "${NET_DELAY}"
network_get_ipaddr NET_IP "${NET_IF}"
if [ -z "${NET_IP}" ]
then
uci set network.${NET_IF}.ipaddr="${UCI_IP}"
uci set network.${NET_IF}.netmask="${UCI_MASK}"
uci set network.${NET_IF}.gateway="${UCI_GW}"
uci set network.${NET_IF}.dns="${UCI_DNS}"
/etc/init.d/network reload
fi
fi
EOF

Hi @vgaetera

I don't have 95-dhcp-fallback on the path "cat /etc/hotplug.d/iface/"
in that path i have:

root@HFCI:/# cat /etc/hotplug.d/iface/
00-netstate   25-dnsmasq    65-pppoe      80-ipsec
15-teql       50-miniupnpd  70-quagga     90-rngd
20-firewall   55-mcproxy    75-rstp       95-ddns

Thanks in advance

Regards
Sakshi

The code above creates that file.

Hi @vgaetera

So I have to write that code :

in "lib/functions/network.sh" file
How this file will run?

Thanks in advance

Regards
Sakshi

1 Like

No, command cat writes the code until EOF inside /etc/hotplug.d/iface/95-dhcp-fallback.
You just need to copy-paste the whole code-block inside the terminal via SSH.

1 Like

Means copy the following text into the file 95-dhcp-fallback until EOF is reached

1 Like

Hi @vgaetera and @mbo2o

Thanks for your reply.
I have run that cmmand

and written everything in the file

but how we will run this file? i think it is souce another file which is network.sh

Thanks in advance
Regards
Sakshi

The script runs automatically when network-service brings up lo-interface.
It waits 15 seconds and checks lan-interface IPv4-address.
If there's no IPv4-address, it configures static address.

Hi @vgaetera

I have bridge interface on which I get the static and DHCP IP.
So same for fallback IP, i want the ip on bridge interface. Adding ifconfig:

root@root:/# ifconfig 
br-lan    Link encap:Ethernet  HWaddr 00:03:7F:EE:66:D7  
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::203:7fff:feee:66d7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1207 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1188 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:143670 (140.3 KiB)  TX bytes:511720 (499.7 KiB)

eth0      Link encap:Ethernet  HWaddr 00:03:7F:EE:66:D7  
          UP BROADCAST ALLMULTI MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:252 

eth1      Link encap:Ethernet  HWaddr 00:03:7F:C0:A5:25  
          UP BROADCAST RUNNING ALLMULTI MULTICAST  MTU:1500  Metric:1
          RX packets:76911 errors:0 dropped:31 overruns:0 frame:0
          TX packets:84288 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:10637562 (10.1 MiB)  TX bytes:33448890 (31.8 MiB)
          Interrupt:255 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:76025 errors:0 dropped:0 overruns:0 frame:0
          TX packets:76025 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:5076660 (4.8 MiB)  TX bytes:5076660 (4.8 MiB)

Thanks in advance

Regards
Sakshi

OpenWrt operates interfaces/networks by their logical names.
Logical-level interface lan represents network-level interface br-lan.

Hi @vgaetera

I have tried your solution by running the above command.
When I change my board from static to DHCP and I didn't put the LAN wire in the board, so the board will not get the DHCP IP. After that, I have waited for more than 20 sec and my board didn't get the static IP define in the 95-dhcp-fallback file.
So i really didn't know how this will work.
Please confirm this how it will work.

Thanks in Advance

Regards
Sakshi

The script is triggered on boot/reboot or network service restart.


May be you should try to use aliases?
It's possible to hold both static and dynamic IP on the same interface simultaneously:
https://openwrt.org/docs/guide-user/network/network_interface_alias

Hi @vgaetera

I have rebooted my board to get that script trigger but didn't work for me.
I have changed my GUI from static to DHCP and wait for 15 sec and more but didn't get the fallback IP i.e., static IP which is written in 95-dhcp-fallback. PFA file.

source /lib/functions/network.sh
INTERFACE="br-lan"
NET_ACTION="ifup"
NET_IFLO="lo"
NET_IF="lan"
NET_DELAY="15"
UCI_IP="192.168.1.1"
UCI_MASK="255.255.255.0"
UCI_PROTO="static"

if [ "$ACTION" = "$NET_ACTION" -a "$INTERFACE" = "$NET_IFLO" ]
then
sleep "$NET_DELAY"
network_get_ipaddr NET_IP "$NET_IF"
if [ -z "$NET_IP" ]
then
uci set "network.$NET_IF.ipaddr=$UCI_PROTO"
uci set "network.$NET_IF.ipaddr=$UCI_IP"
uci set "network.$NET_IF.netmask=$UCI_MASK"
/etc/init.d/network restart
fi
fi

I don't know from where it will get the INTERFACE so i have INTERFACE in this file and changed according to my requirements.

Thanks in advance

Regards
Sakshi

I think it requires some debugging:

sed -r -i 's%^(UCI_PROTO=.*)%\1\n\nset -x -v\nexec \&>>/tmp/dhcp-fallback.log\ndate\n\n%' /etc/hotplug.d/iface/95-dhcp-fallback

Hi @vgaetera

I have run the above-defined command

tsed -r -i 's%^(UCI_PROTO=.*)%\1\n\nset -x -v\nexec \&>>/tmp/dhcp-fallback.log\ndate\n\n%' /etc/hotplug.d/iface/95-dhcp-fallback

my updated 95-dhcp-fallback is given below

source /lib/functions/network.sh
INTERFACE="br-lan"
NET_ACTION="ifup"
NET_IFLO="lo"
NET_IF="lan"
NET_DELAY="15"
UCI_IP="192.168.1.1"
UCI_MASK="255.255.255.0"
UCI_PROTO="static"

set -x -v
exec &>>/tmp/dhcp-fallback.log
date



if [ "$ACTION" = "$NET_ACTION" -a "$INTERFACE" = "$NET_IFLO" ]
then
sleep "$NET_DELAY"
network_get_ipaddr NET_IP "$NET_IF"
if [ -z "$NET_IP" ]
then
uci set "network.$NET_IF.ipaddr=$UCI_PROTO"
uci set "network.$NET_IF.ipaddr=$UCI_IP"
uci set "network.$NET_IF.netmask=$UCI_MASK"
/etc/init.d/network restart
fi
fi

and reboot the board.
After that, I have changed my router from static to DHCP and again wait for more than 15 sec.
Also, I didn't get any logs on /tmp path.

I think this file is not running in the backend.
Thanks in advance

Regards
Sakshi

sed -i 's%^exec.*%exec \&>"/root/dhcp-fallback-$(date -Iseconds).log"%' /etc/hotplug.d/iface/95-dhcp-fallback

To see the logs:

head -n -0 /root/dhcp-fallback-*