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.
Static IP configured as
Router configured as
User changes their network and their router is now
Raspberry PI boots and has static configuration
.1.110 and gateway
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 184.108.40.206 Tue 25 Sep 14:07:00 UTC 2018 Testing new gateway...192.168.0.1 PING 220.127.116.11 (18.104.22.168) from 192.168.0.110 : 56(84) bytes of data. --- 22.214.171.124 ping statistics --- 10 packets transmitted, 0 received, 100% packet loss, time 9330ms
Any help or pointing in the right direction appreciated.