17

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?

7
  • 3
    unix.stackexchange.com/questions/7399/…
    – fortytwo
    Commented Jul 18, 2013 at 10:40
  • @bhau I prefer not to install any package for doing this. Commented Jul 18, 2013 at 10:44
  • @MichaelKjörling I want to get the country for my particular public IP address. Maybe sound strange, but I exlained why. Commented Jul 18, 2013 at 10:59
  • @Radu What's wrong with installing a package?
    – jobukkit
    Commented Jul 18, 2013 at 11:09
  • 1
    @Jop Just a guess, but maybe the OP doesn't have root access to the system? Installing a package, particularly if it brings in library dependencies which might refer to files by full path, can require root access.
    – user
    Commented Jul 18, 2013 at 11:19

4 Answers 4

24

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"
}
2
  • 9
    Elegantly getting just the country code using jq: curl -s ipinfo.io/ | jq ".country".
    – Pablo A
    Commented Apr 14, 2019 at 2:00
  • Or even just ... curl -s ipinfo/country
    – bu5hman
    Commented Jan 6 at 15:55
18

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) }'
2
  • 1
    whois $(curl ifconfig.me) | grep -iE ^country: | awk '{print $2}' gives me what I want; thanks! Commented Jul 18, 2013 at 11:32
  • 1
    @Radu You can save yourself one pipe by doing whois $(curl ifconfig.me) | awk 'tolower($1) ~ /^country:/ { print $2 }' instead.
    – user
    Commented Jul 18, 2013 at 11:39
12

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.

4
  • This is an excellent illustration of why it's a good idea to wait a little before accepting an answer. +1 from me.
    – user
    Commented Jul 18, 2013 at 16:42
  • @MichaelKjörling ...I can see, but now I think that is not right for you to change the accepted answer. Commented Jul 18, 2013 at 19:00
  • 1
    @Radu It's okay - if you feel this answer is more useful, Stephane deserves the accept (though I hope I'll get or keep an/the upvote from you :)). In general, it's usually a good idea to wait at least some 12-24 hours before accepting an answer, to give multiple people a chance to answer. Accepting an answer basically says "the question has been satisfactorily answered", reducing the incentive for others to add answers.
    – user
    Commented Jul 18, 2013 at 19:06
  • 1
    Stéphane, doesn't work any more, but curl --silent https://whatismycountry.com/ grep --only-matching --perl-regexp 'Your Country is \K[^<]+' does.
    – Fabby
    Commented Apr 26, 2018 at 14:17
2

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}
%
2
  • freegeoip doesn't seem to be still available: curl -i freegeoip.net/json/ returns HTTP/1.1 301 Moved Permanently.
    – ijoseph
    Commented Jun 23, 2021 at 20:12
  • 1
    FYI a query to 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.
    – Joel Purra
    Commented May 16, 2023 at 6:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .