20

How to get a computer's internet (IP address) location using command line?

For example, using curl or wget?

2
  • 4
    Could you specify what an "internet location" is?
    – Carsten S
    Commented Apr 16, 2016 at 14:27
  • The IP's address location. Adding to the questions... Commented Apr 17, 2016 at 6:48

3 Answers 3

24

There's a service providing this: ipinfo.io.

You can invoke it using curl. Example:

curl ipinfo.io

Result:

{
  "ip": "...",
  "hostname": "...",
  "city": "...",
  "region": "...",
  "country": "...",
  "loc": "...,...",
  "org": "..."
}

A specific IP's info can also be requested: curl ipinfo.io/216.58.194.46:

{
  "ip": "216.58.194.46",
  "hostname": "dfw25s12-in-f14.1e100.net",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.4192,-122.0574",
  "org": "AS15169 Google Inc.",
  "postal": "94043"
}

https is also available: curl https://ipinfo.io/216.58.194.46

Source: http://xmodulo.com/geographic-location-ip-address-command-line.html

10
  • 4
    Please note this service does not provide an accurate location. 50km wrong for me.
    – DavidPostill
    Commented Apr 14, 2016 at 8:20
  • 1
    I've no idea how it works. I'm currently on mobile internet. Google maps manages to locate me perfectly well.
    – DavidPostill
    Commented Apr 14, 2016 at 8:29
  • 1
    The curl command above just goes to a service without providing anything private. ipinfo.io has almost only the source IP to use to pin point the location and get data. Commented Apr 14, 2016 at 8:36
  • 2
    geoip is notoriously inaccurate... I can be either in Idaho, Minnesota or Oregon depending on the DHCP netblock of the day. Commented Apr 17, 2016 at 3:51
  • 3
    @DavidPostill Google Maps uses GPS to position you on the map, it's pretty accurate. Geolocation by IP address usually gets region right. Commented Apr 17, 2016 at 10:42
5

Since the question doesn't specify an OS, this is how to get that same information with PowerShell's curl (actually an alias of Invoke-WebRequest):

(curl ipinfo.io).Content

That produces a JSON string. To get the object that JSON represents, use ConvertFrom-Json:

curl ipinfo.io | ConvertFrom-Json

Since that's a PowerShell object, you can easily get specific fields from it. For example, this command gets just the external IP as a string:

(curl ipinfo.io | ConvertFrom-Json).ip

Note that the geographical information from this service isn't super accurate, but it did locate me within 20 miles or so. The ISP information seems to be reliable.

0

You can also use from PowerShell:

Invoke-RestMethod http://ipinfo.io/json

The command output will already give us the location in JSON

Extracted from: https://www.sysadmit.com/2019/01/windows-saber-ip-publica-PowerShell.html

You must log in to answer this question.

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