I have compiled a little LFS system and I'm currently trying to get Internet connectivity up and running. I have managed to get it working manually, but this required me to know the router ip address. However, I want my system to be able to connect to the internet in any network, even if the router ip address is initially unknown. How can I retrieve the router ip address (also referred to as the gateway) dynamically/automatically?
Assuming you want the external IP gateway address, you'll need to either find out from the gateway device itself or contact an external service. A site that I use for stuff like this (particularly for
ddclient, as it needs to know when to re-register my DDNS and tunnel) is icanhazip.com, which can show your Internet-facing IPv4 and IPv6 addresses, among other things. However, as IPv6 is supposed to be globally-routable, you probably don't need to ask an external service regarding IPv6 addresses.
There are a couple of ways of using this service:
- In a configuration file, point to
(http://)ipv4.icanhazip.com(or ipv6 if you need that.)
In a script, get the address using
echo My external IP address is $(curl -s http://ipv4.icanhazip.com)