Change default gateway & assigned IP via script
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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.
networking raspberry-pi network-interface dhcp gateway
add a comment |Â
up vote
0
down vote
favorite
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.
networking raspberry-pi network-interface dhcp gateway
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
networking raspberry-pi network-interface dhcp gateway
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.
networking raspberry-pi network-interface dhcp gateway
networking raspberry-pi network-interface dhcp gateway
asked Sep 25 at 15:11
Josh
51
51
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
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 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 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 at 17:01
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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 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 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 at 17:01
add a comment |Â
up vote
0
down vote
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 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 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 at 17:01
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Sep 25 at 16:36
Sufferer
1
1
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 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 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 at 17:01
add a comment |Â
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 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 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 at 17:01
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 at 16:43
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 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 at 16:53
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 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 at 17:01
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 at 17:01
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471355%2fchange-default-gateway-assigned-ip-via-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password