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

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

For example, using curl or wget?

2

3 Answers

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 :

{ "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"
}

Source:

9

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.

You can also use from PowerShell:

Invoke-RestMethod 

The command output will already give us the location in JSON

Extracted from:

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like