Determining your WAN ip address from the terminal (or in a bash script)1. Using external means- Code: Select all
curl ident.me ; echo
curl http://checkip.amazonaws.com ; echo
Either of these two work fast. If you haven't got curl and you don't want to or can't install is, you can also use 'wget -O - -q' instead of 'curl -s'.
This one uses dig and the resolver of OpenDNS:
- Code: Select all
dig +short myip.opendns.com @resolver1.opendns.com
The next one is also very fast and uses icanhazip.com but only with commands built-in to every Linux:
- Code: Select all
exec 3<> /dev/tcp/icanhazip.com/80 && # open connection
echo 'GET /' >&3 && # send http 0.9 request
read -u 3 && echo $REPLY && # read response
exec 3>&- # close fd
2. By internal meansMany internet home routers for cable or DSL show a status page with the WAN ip address which doesn't require a login. If so, you can get your WAN ip address from your router.
This is an example for DDWRT:
- Code: Select all
curl -s router | grep "ipinfo" | awk -v FS="(IP: |</span)" '{print $2}'
or if a login is needed:
- Code: Select all
curl -s -u username:password router | grep "ipinfo" | awk -v FS="(IP: |</span)" '{print $2}'
Replace 'router' with the hostname or LAN ip address of your router.
Replace username and password by the username and password needed to login to the webinterface of your router.
Many Europeans (including me) have an AVM Fritz1Box router. That one yields the WAN ip address as well, but it's a little more complicated:
- Code: Select all
curl -s "http://router.:49000/igdupnp/control/WANIPConn1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress" -d "<?xml version='1.0' encoding='utf-8'?> <s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> <s:Body> <u:GetExternalIPAddress xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /> </s:Body> </s:Envelope>" |grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>'
Replace 'router.' with the actual hostname or LAN ip address of your Fritz!Box router.
(source: https://askubuntu.com/questions/95910/c ... -public-ip)