How to get a computer's internet (IP address) location using command line?
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"
}
Source: http://xmodulo.com/geographic-location-ip-address-command-line.html
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.