I want to use the name of the country where I am now in a bash script. I can get the external IP using next command:
curl ifconfig.me
But how can I get my country name?
I want to use the name of the country where I am now in a bash script. I can get the external IP using next command:
curl ifconfig.me
But how can I get my country name?
ipinfo.io has nice JSON API for using from command line:
$ curl ipinfo.io
{
"ip": "X.X.X.X",
"hostname": "No Hostname",
"city": "Hanoi",
"region": "Ha Noi",
"country": "VN",
"loc": "21.0333,105.8500",
"org": "AS18403 The Corporation for Financing & Promoting Technology"
}
You can get somewhat close by querying the public whois database. It'll likely be somewhat difficult to "productify" to handle every possible case, but a reasonable approximation might be:
$ whois a.b.c.d | grep -iE ^country:
where a.b.c.d
is the IP address in question.
whois
is often installed by default, so this meets a reasonable interpretation of your "I prefer not to install any package for doing this" read as "I don't want to install additional software".
To print only the value of the country
field and force it to upper case only (to make comparisons easier, for example), you can do something like:
$ whois a.b.c.d | awk -F':[ \t]+' 'tolower($1) ~ /^country$/ { print toupper($2) }'
whois $(curl ifconfig.me) | grep -iE ^country: | awk '{print $2}'
gives me what I want; thanks!
Commented
Jul 18, 2013 at 11:32
whois $(curl ifconfig.me) | awk 'tolower($1) ~ /^country:/ { print $2 }'
instead.
Use another IP locator than ifconfig.me that provides with that information like:
Not available anymore as of 2015-03-09
curl -s 'http://geoiplookup.net/geoapi.php?output=countrycode'
or:
curl -s 'http://geoiplookup.net/geoapi.php?output=country'
(see the API for details)
or:
curl -s http://whatismycountry.com/ |
sed -n 's|.*,\(.*\)</h3>|\1|p'
or:
curl -s http://whatismycountry.com/ |
sed -n 's|.*> *\(.*\)</h3>|\1|p'
for more precision, or:
curl -s http://whatismycountry.com/ |
sed -n 's/.*Coordinates \(.*\)<.*/\1/p'
for the coordinates.
That makes assumptions on the HTML formatting of the page. So it may stop working if they decide to change that format in the future.
curl --silent https://whatismycountry.com/ grep --only-matching --perl-regexp 'Your Country is \K[^<]+'
does.
In addition to ipinfo.io
mention in the other answer, you could also use freegeoip.net
as well, which seems to be officially using the publicly available code/database (which you can run on your own instance, if needed), and appears to have a higher limit for the number of requests -- 15k/hour, vs. 1k/day on ipinfo.io.
% curl -i ipinfo.io
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 17 Sep 2017 02:17:47 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 240
Vary: Accept-Encoding
x-cloud-trace-context: 86d62d74c999fc62715d7dff810ea16c/9504640995707975809;o=0
Access-Control-Allow-Origin: *
X-Content-Type-Options: nosniff
Via: 1.1 google
{
"ip": "88.198.54.xx",
"hostname": "static.88-198-54-xx.clients.your-server.de",
"city": "Nuremberg",
"region": "Bavaria",
"country": "DE",
"loc": "49.4478,11.0683",
"org": "AS24940 Hetzner Online GmbH",
"postal": "90455"
}%
% curl -i freegeoip.net/json/
HTTP/1.1 200 OK
Date: Sun, 17 Sep 2017 02:17:54 GMT
Content-Type: application/json
Content-Length: 230
Connection: keep-alive
Set-Cookie: __cfduid=dacbae017e5ee70d57b251c89c4ba418b1505614674; expires=Mon, 17-Sep-18 02:17:54 GMT; path=/; domain=.freegeoip.net; HttpOnly
Vary: Origin
X-Database-Date: Thu, 07 Sep 2017 04:08:50 GMT
X-Ratelimit-Limit: 15000
X-Ratelimit-Remaining: 14996
X-Ratelimit-Reset: 2697
Server: cloudflare-nginx
CF-RAY: 39f89263d43c6367-FRA
{"ip":"88.198.54.xx","country_code":"DE","country_name":"Germany","region_code":"BY","region_name":"Bavaria","city":"Nuremberg","zip_code":"90455","time_zone":"Europe/Berlin","latitude":49.4478,"longitude":11.0683,"metro_code":0}
%
freegeoip
doesn't seem to be still available: curl -i freegeoip.net/json/
returns HTTP/1.1 301 Moved Permanently
.
freegeoip.net/json
permanently (HTTP 301) redirects to freegeoip.net/shutdown
, which is a domain parking/advertising page. "Domain Parking is a simple way to earn money from your domains' natural traffic. If you have registered domain names, but they are not currently being used, then domain parking is a great way to put those domains to work, earning you revenue. " The domain itself redirects to a IP-address user data API/broker business.
Commented
May 16, 2023 at 6:12