On Wednesday 31 May 2017, Carlos E. R. wrote:
Hi,
In order to get the external IP on a script I used (Dec 2015) this one liner:
wget -qO - checkip.dyndns.org | cut -d":" -f2 | cut -d"<" -f1
and this is failing now (returns empty), but I'm unable (ie, too thick today) to see why. The output of the wget run is:
cer@minas-tirith:~> wget -qO - checkip.dyndns.org <html><head><title>Current IP Check</title></head><body>Current IP Address: 2.141.215.163</body></html> cer@minas-tirith:~>
Something has changed, no idea what.
Meanwhile, looking on google, I got another simpler oneliner (from https://coderwall.com/p/lmi2jw/get-external-ip-in-a-script):
curl http://icanhazip.com
they also suggest using http://ifconfig.me/ip instead, but it fails:
cer@minas-tirith:~> curl http://icanhazip.com 2.141.215.163 cer@minas-tirith:~> curl http://checkip.dyndns.org cer@minas-tirith:~> curl http://ifconfig.me/ip cer@minas-tirith:~>
All sites work on a browser, but only http://icanhazip.com works on the CLI with curl. Why?
I use this because I've had bad experiences with dyndns and friends: $ dig @resolver1.opendns.com myip.opendns.com +short In a script I'm using it like below, inclusive error handling and validating the IP. Also easy to change the actual method in case it will break anytime. ------------ #!/bin/sh isvalid_ip4() { local ip="$1" test -n "$ip" || return 1 ip -4 route get "$ip" >/dev/null || return 1 ping -W 1 -c1 "$ip" >/dev/null || return 1 } myip4_dig_opendns() { dig @resolver1.opendns.com myip.opendns.com +short } myip4_generic() { local myipfun="$1" local myip="" myip="$("$myipfun")" || return 1 isvalid_ip4 "$myip" || return 1 echo "$myip" } MYIP4=$(myip4_generic myip4_dig_opendns) if [ $? != 0 ]; then echo "error: unable to find out our public IPv4 address" 1>&2 exit 1 fi # do something with $MYIP4 ... ------------------------- cu, Rudi -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org