From time-to-time I need to find the geographic location of any IP address or its domain name. It’s quickest for me to do this from the Mac Terminal. Here’s a shortcut you can set up to make it really easy to get the GEOIP data for a domain name or IP address.

In Terminal edit your ~/.bash_aliases file. E.g., nano ~/.bash_aliases

In the editor, add the following lines of code:

function geoip() {
        curl https://freegeoip.app/xml/$1
}

Save the file and exit the editor (e.g., in nano: ctrl-x, then y to save the file).
Use this command to load the modified file, source ~/.bash_aliases

Now you can look up the GeoIP data for a domain or IP address, with a command like this: geoip best-mac-tips.com, and you’ll see output like this:

$ geoip best-mac-tips.com
<Response>
	<IP>64.227.57.224</IP>
	<CountryCode>US</CountryCode>
	<CountryName>United States</CountryName>
	<RegionCode>CA</RegionCode>
	<RegionName>California</RegionName>
	<City>San Francisco</City>
	<ZipCode>94119</ZipCode>
	<TimeZone>America/Los_Angeles</TimeZone>
	<Latitude>37.7794</Latitude>
	<Longitude>-122.4176</Longitude>
	<MetroCode>807</MetroCode>
</Response>

The command used above returns the results in and XML format. There are three other formats available, shown below.

Other Formats

The freegeoip.app service also supports CSV, JSON and JSONP.

For CSV, use the following in your bash file, curl https://freegeoip.app/csv/$1

64.227.57.224,US,United States,CA,California,San Francisco,94119,America/Los_Angeles,37.7794,-122.4176,807

For JSON, use this in your bash function, curl https://freegeoip.app/json/$1

{"ip":"64.227.57.224","country_code":"US","country_name":"United States","region_code":"CA","region_name":"California","city":"San Francisco","zip_code":"94119","time_zone":"America/Los_Angeles","latitude":37.7794,"longitude":-122.4176,"metro_code":807}

Checking your own IP

You can check your own IP, using the new function, with the simple command, geoip.