0

I need some pointing in the right direction as I've tried a lot of different things over the past few days without success. I'm new to using bash and linux in general.

I'm trying to detect when the gateway changes (on boot only) and update static ip configuration. I partially have this working in that on the first boot there is no static IP configuration in /etc/dhcpcd.conf. Upon first boot I can simply ping an ip address and if successful update the configuration based on the output of ip r. This works without any issue.

The second part is detecting (again only on boot) that the gateway has changed (e.g. the user has changed their router IP or changed ISP that uses a different configuration). Same concept - ping an external address but in this instance the ping fails as the gateway information (and static ip) are incorrect.

E.g.
Static IP configured as 192.168.1.110
Router configured as 192.168.1.254
User changes their network and their router is now 192.168.0.1
Raspberry PI boots and has static configuration .1.110 and gateway .1.254.

Upon failure the script tries to add a new gateway and IP address (from a list of common IP addresses used for gateways in a file) and ping an external address. If successful then update the static config in /etc/dhcpcd.conf and reboot. If not successful then try the next IP address in the list until it finds a valid gateway.

As far as I've worked out I need to add a new route and new default gateway and ping. Adding these works but ping still fails (even though it is going via the correct IP address). Output of several commands below after the new route, gateway and IP address have been added.

ip r
default via 192.168.0.1 dev eth0 
192.168.0.1 dev eth0 scope link 
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.110 metric 202 

netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.0.1     0.0.0.0         UG        0 0          0 eth0
192.168.0.1     0.0.0.0         255.255.255.255 UH        0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

ping -q -w 10 -c 1 -I 192.168.0.1 -r 1.1.1.1
Tue 25 Sep 14:07:00 UTC 2018 Testing new gateway...192.168.0.1
PING 1.1.1.1 (1.1.1.1) from 192.168.0.110 : 56(84) bytes of data.

--- 1.1.1.1 ping statistics ---
10 packets transmitted, 0 received, 100% packet loss, time 9330ms

Any help or pointing in the right direction appreciated.

0

First off, I think you might be trying to solve a problem that is not present...

You say...

User changes their network and their router is now 192.168.0.1

This is not something that occurs very often, the vast majority (according to a "Tom's Guide" survey >80% of folks don't even change the password!)

However, what you are trying to do would be better served with dhcp, at least that way you only have to reconfigure things once on the dhcp server, and all devices would learn the new network layout.

Yet, if you are dead set against dhcp, the solution while not simple, you can logic your way out of it...

Many years ago, I was responsible for configuring the network on a network attached storage device. I initially tried to use DHCP and if that failed due to no server response, it configured itself with IP address 0.0.0.0 and grabbed a few seconds (5 IIRC) of network traffic with tcpdump. It then did some statistical analysis to figure out the local address space (if you look at src and dst in the ip header, addresses in the local space will be more common), and then by using ping, discovered an unused IP address within that space. The router's IP address was also statistically derived by using arp requests. It worked really well on well trafficked networks, but poorly on networks with low traffic. As network switches became more common and the lowly hub fell by the wayside, it stopped working as the switch filters out traffic that is not destined for your MAC address.

Oh the memories of a fun project.

  • Agreed this is a minor use case and I could simply delete the static configuration in /etc/dhcpcd.conf, reboot and use the already working code that grabs the gateway and ip, write the relevant details back to /etc/dhcpcd.conf and reboot again. It's annoying me that I can't work it out however - there has to be a way to do it surely. In terms of DHCP the PI at the point the network config was changed by the user is the DHCP server so it needs to be on the right network config in order to perform that function. – Josh Sep 25 '18 at 16:43
  • Welp, in that case, you are editing file on the Pi, just add "Update the Pi's IP address" My DHCP server at home is in a VM, and it (and the hypervisor) would need it's IP changing but, the 192.168.1.x network and addresses I chose almost 20 years ago have not needed changing, – Sufferer Sep 25 '18 at 16:53
  • I've actually just made it work after all this time - well mostly. I can access the pi that booted on 192.168.1.x network from 192.168.0.x IP address via router at 192.168.0.1. There isn't a gateway on the PI but seems to be working. Have an idea of what needs changing to get the gateway to add properly. Will keep testing and see if I can get it fully configured! – Josh Sep 25 '18 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.