0

How can I extract the ip address and the country and put them individual in a string without any quotes or any other characters which are present in the text by using the next command:

info_ip=`wget --tries=1 --timeout=10 -qO- http://ipinfo.io/?callback=callback; echo`

Now I'm looking to filter the data for ip and country

$ip = ?

$country = ?

ipinfo.io currently returns something like:

/**/ typeof callback === 'function' && callback({
  "ip": "198.51.100.123",
  "hostname": "host.example.com",
  "city": "Example City",
  "region": "Example Region",
  "country": "XY",
  "loc": "12.3456,-1.2345",
  "postal": "XXX",
  "org": "ASXXXX Example Organisation"
});

3 Answers3

3

Another approach.

A=($(wget -t1 -T10 -qO- ipinfo.io/?callback=callback|awk -F\" '/ip|country/{print$4}'))
echo ${A[0]}
35.230.152.185
echo ${A[1]}
US

As Stéphane Chazelas pointed out, this would behave badly if "ip" or "country" seen elsewhere in the output. A more robust solution:

A=($(wget -t1 -T10 -qO- ipinfo.io/?callback=callback|awk -F\" '$2~/^(ip|country)$/{print$4}'))
steve
  • 21,892
2

Maybe something like:

read -r ip country < <(
  wget --tries=1 --timeout=10 -qO- 'http://ipinfo.io/?callback=callback' |
     perl -MJSON -l -0777 -ne '
       if (/callback\((.*)\);$/s) {
          $j = from_json($1);
          print "$j->{ip} $j->{country}"
       }'
)

For a shell with builtin JSON support, see ksh93v (currently in beta):

ipinfo=${
  wget --tries=1 --timeout=10 -qO- 'http://ipinfo.io/?callback=callback'
} || exit
ipinfo=${ipinfo/%*"callback("@(*)");"/\1}
IFS= read -rm json j <<< $ipinfo
ip=${j.ip} country=${j.country}

Note that instead of that ?callback query (btw, ? is a shell wildcard so needs to be quoted), you can also use:

curl -H 'Accept: application/json' ipinfo.io/json

or:

wget --tries=1 --header='Accept: application/json' --timeout=10 -qO- http://ipinfo.io/json

Which you could pipe to jq -r '.ip, .country':

{ read -r ip; read -r country; } < <(
  curl -H 'Accept: application/json' ipinfo.io/json |
  jq -r '.ip, .country'
)
1
info_ip=`wget --tries=1 --timeout=10 -qO- http://ipinfo.io/?callback=callback; echo`
IFS=$'\n'
IP_country=( $(awk -F'[:"]' '/ip/ || /country/{ print $5}' <<<"$info_ip") )
ip=${IP_country[0]}
country=${IP_country[1]}
echo $ip
echo $country
MR RsO
  • 21
  • That is quite brittle. It would fail if any of the fields contained "ip" or "country" or if the ip or country contained ":" (like for IPv6 addresses) or wildcard characters (those ones causing a DoS vulnerability) or if ip appeared after country. – Stéphane Chazelas Sep 07 '18 at 09:27