Home > Blockchain >  CMD: assigning IP to variable to get location
CMD: assigning IP to variable to get location

Time:05-04

I would like to get the location of my external IP address in Window's command prompt upon double-clicking a .cmd or .bat file.

To the best of my knowledge this requires at least two steps in CMD:

Step 1: Getting my external IP:

curl ip-adresim.app

Output:

216.58.194.46

Step 2: Getting that IP's location:

curl ipinfo.io/216.58.194.46

Output:

{ "ip": "216.58.194.46", "hostname": "dfw25s12-in-f46.1e100.net", "city": "San Francisco", "region": "California", "country": "US", "loc": "37.7749,-122.4194", "org": "AS15169 Google LLC", "postal": "94102", "timezone": "America/Los_Angeles", "readme": "https://ipinfo.io/missingauth" }

I would have to connect both steps by a common variable to make this an automated script, right.

But how?

CodePudding user response:

I've used ipinfo.io/city for this if you would like to capture this information in one step:

for /f "tokens=*" %%g in ('%__APPDIR__%curl.exe -s ipinfo.io/city') do set "_myLocation=%%g"
if defined _myLocation echo %_myLocation%

This was under the assumption you were just looking for the City. I'm not sure what you mean by "Input Text,"' but if you want the IP, City and Country, you could do the following:

for /f "tokens=*" %%g in ('%__APPDIR__%curl.exe -s ipinfo.io/ip') do set "_myIP=%%g"
for /f "tokens=*" %%g in ('%__APPDIR__%curl.exe -s ipinfo.io/city') do set "_myCity=%%g"
for /f "tokens=*" %%g in ('%__APPDIR__%curl.exe -s ipinfo.io/country') do set "_myCountry=%%g"
if defined _myIP echo External IP: %_myIP%
if defined _myCity echo City: %_myCity%
if defined _myCountry echo Country: %_myCountry%
  • Related