How to get a computer's internet (IP address) location using command line?
For example, using curl or wget?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityHow to get a computer's internet (IP address) location using command line?
For example, using curl or wget?
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
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 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