[ Script ] DHCP and Static IP related

Hey lovely ppl at Diet-Pi forum ,

I’m trying rn to write a " Script " that start every time your restart your SBC ,

The goal of the script is :
– get the network/router DHCP settings than assign a Statics IP based on the DHCP Settings ,

Example :
– if i switch from network with IP of 192.168.1.1 and my ip was 192.168.1.200 to another network with IP of 192.168.100.1 i want the script automatically switch the ipv4 to 192.168.100.200

Any ideas / tips / tricks ?

  • consider that I’m literally noob

Maybe the easier way would be to you just configure DHCP reserveration in your DHCP server for your device. So it binds e.g. always the same IP to a particular MAC address.
Which DNS server do you use?

Nice suggestions , i need to read more about DHCP reserveration ,

RN I’m testing with :
Adguard home + unbound

Question is who is DHCP server? I guess it is the router?

Yes , the router is the dhcp server ,

When i switch between routers i want the network ID to switch as wil ,
Examples :

Router 1 gateway ( 192.168.1.1 )
Router 2 gateway ( 192.168.100.1 )
Router 3 gateway ( 192.168.3.1 )

On router 1 :
SBC Static ipv4 ( 192.168.1.200 )

Switch to Router 2

Automatically SBC Static ipv4 ( 192.168.100.200 )

Which specific routers do you use? Some may have the option for DHCP reservation, some not.

yes have a look to router manual if they are offer IP address reservation on DHCP.

Here is the problem ,
i want the SBC it self to detect router DHCP settings than from this infos change the static ip to be suitable for the router where the SBC connecting to

This is not a good idea as you probably another device is using the IP address you like to assign as STATIC. Therefore IP address reservation on router is way better option.

Oh , so if i want to change my SBC router ( Gateway 192.168.1.1 ) to another router ( Gateway 192.168.100.1 ) i need to do the Static ip manually ? No way to automate the process ?

Of course you can automate such. You just need to write your own script to handle it :wink:

But I guess you misunderstood what IP address reservation on a DHCP server means. Because for this you don’t need any static IP on your SBC. Because the router will assign the correct IP via DHCP automatically.

Thanks a lot , I’m trying now to write my own script !
any beginners tips ? ( like really noobs tips XD )

So I would config your SBC to get first a IP via DHCP. You will get an IP and a gateway IP. Than you can check the gateawy IP to see, on which network you are right now. Based on this information, you can assign the SBC a static IP (Which is optimally not in the DHCP range) in /etc/network/interfaces.

Somethink like: (NOT TESTED)

#!/bin/bash
if grep -q "default via 192.168.1.1"  $(ip route | grep default);
        then echo -e "Your are on the network 192.168.1.1"
        ifconfig eth0 down
        ifconfig eth0 191.168.1.200 netmask 255.255.255.0 broadcast 192.168.1.255
        route add default gw 192.168.1.1 eth0
        infconfig eth0 up

elif grep -q "default via 192.168.100.1"  "$(ip route | grep default)";
        then echo -e "Your are on the network 192.168.100.1"  
        ifconfig eth0 down
        ifconfig eth0 191.168.100.200 netmask 255.255.255.0 broadcast 192.168.100.255
        route add default gw 192.168.100.1 eth0
        infconfig eth0 up
        

elif grep -q "default via 192.168.3.1"  "$(ip route | grep default)";
        then echo -e "Your are on the network 192.168.3.1"  
        ifconfig eth0 down 
        ifconfig eth0 191.168.3.200 netmask 255.255.255.0 broadcast 192.168.3.255
        route add default gw 192.168.3.1 eth0
        infconfig eth0 up

else echo -e "Your are on some other network";
       ip r
fi

You could execute this script at atartup, via cron. And maybe before that some other script, which sets the interface eth0 to DHCP, to be able too “see” the new network. ifconfig eth0 0.0.0.0 0.0.0.0 && dhclient

Some Info for you to learn:
https://wiki.debian.org/NetworkConfiguration
https://devhints.io/bash

(BTW I’m also pretty new to this)

And I had a look into /boot/dietpi/dietpi-config.
Around line 2000 you can see what they do after changing values for the network ( Network_Restart() and Network_ApplyChanges())
So I guess my script needs some additions, for sure :wink:

Network_Restart(){

		# Stop all services (required for hotspot)
		/boot/dietpi/dietpi-services stop

		# Enable/Disable WiFi modules
		/boot/dietpi/func/dietpi-set_hardware wifimodules $(( ! $WIFI_DISABLED ))

		(( $WIFI_DISABLED )) && G_WHIP_BUTTON_CANCEL_TEXT='Skip' G_WHIP_YESNO 'Would you like to purge all WiFi related APT packages?
 \nThis will free up space, but an internet-capable Ethernet connection is required to re-enable WiFi functionality.
 \nAffected packages: iw wireless-tools crda wpasupplicant' && G_AGP 'iw' 'wireless-tools' 'crda' 'wpasupplicant'

		# Drop Connections
		G_DIETPI-NOTIFY 0 'Dropping connections, please wait...'
		ifdown "$ETH_DEV_IFACE" --force 2> /dev/null
		ifdown "$WIFI_DEV_IFACE" --force 2> /dev/null

		# Kill dhclient
		killall dhclient 2> /dev/null

		# Flush, not viable to handle this if change of IP, requires a detect of SSH loss/IP change, then exit script.
		#ip a flush dev "$ETH_DEV_IFACE"
		#ip a flush dev "$WIFI_DEV_IFACE"

		# Restart Networking
		G_DIETPI-NOTIFY 2 'Restarting network, please wait...'
		G_EXEC systemctl daemon-reload

		# Manually bring up adapters
		(( $ETH_DISABLED == 0 )) && ifup "$ETH_DEV_IFACE" --force
		(( $WIFI_DISABLED == 0 )) && ifup "$WIFI_DEV_IFACE" --force

		# Restart all services (required for hotspot)
		/boot/dietpi/dietpi-services start

		# Add a little delay to ensure all network device data are updated (eg: SSID current takes a little longer)
		G_DIETPI-NOTIFY 2 'Reloading networking data, please wait...'
		sleep 2

		# Update network data
		Network_GetData

		G_DIETPI-NOTIFY 0 'Network restarted'

		if [[ $ETH_MODE_TARGET == 0 && $ETH_IP != "$ETH_IP_STATIC" ]] || [[ $WIFI_MODE_TARGET == 0 && $WIFI_IP != "$WIFI_IP_STATIC" ]]; then

			G_WHIP_MSG 'IP STATIC address change detected. A reboot is required to apply the new IP.\n\nPlease reboot the system.'
			REBOOT_REQUIRED=1

		fi

	}

	Network_ApplyChanges(){

		# Check if resolvconf is available, else apply static DNS server via /etc/resolv.conf directly instead of /etc/network/interfaces entry
		local i resolvconf=0
		command -v resolvconf > /dev/null && resolvconf=1
		if (( ! $resolvconf )); then

			if (( ! $ETH_DISABLED && $ETH_MODE_TARGET == 0 )); then

				> /etc/resolv.conf
				for i in $ETH_DNS_STATIC; do echo "nameserver $i" >> /etc/resolv.conf; done

			elif (( ! $WIFI_DISABLED && $WIFI_MODE_TARGET == 0 )); then

				> /etc/resolv.conf
				for i in $WIFI_DNS_STATIC; do echo "nameserver $i" >> /etc/resolv.conf; done

			fi

		fi

What a legend this help a lot !

thanks tho , i will work into improve it and implant it !

  • let me put an idea on the table :
    [ Script ] that ( ) the dhcp server settings ( in our case dhcp = the router ) take this infos put it in .txt file somewhere in the SBC than compare your Network id , gateway ( 192.168.1.xxx ) with the dhcp settings he got and depends on that the script changes the static ip values ?
  • The script needs as well to set the dns server as the getaway

What’s your opinion ?

That’s what I tried to show with the “script”, but without the step of saving things to a text file, it just compares the dhcp settigns “on the fly”

The DNS settings can be handled by your gateway, just set DNS to the gateway IP and the SBC will receice the DNS settings from the gateway.

But like I wrote earlier, have a close look into the dietpi-config script, they do a lot of stuff (ifup, if down, killall dhclient etc)
and I’m, not sure if it is a good way to detect the current gateway by “ip route | grep default”
I’m just a noob, too :wink: