通过命令行查找IP地理位置

我一直在拼凑一个快速和肮脏的脚本来返回一堆IP地址的地理位置。

我正在使用freegeoip.net,并找不到一个快速的wget等效于cmd行,所以使用powershell脚本,从我的batch file调用它(或者至less尝试,我认为这是我失败的地方)。

命令行是:

for /F "tokens=* usebackq delims=" %a in ("E:\DATA\02.ips.txt") do ( set /a N+=1 powershell -command "E:\DATA\Resources\geolocation.ps1 %a" ) 

(第一行需要跳过包含标题,而不是IP)

地理位置.ps1是:

 wget freegeoip.net/csv/$args[0] -OutFile "E:\DATA\IPs\$args[0].txt" 

我希望这会给我一个每个IP的返回CSV的文件夹,然后我可以整理成一个文件与副本E:\ DATA \ IPs * .txt alltheips.txt然后该文件将被加载到另一个应用程序使用。

这个(显然)散列是从我search和拾起来的东西混合在一起的,但是,(显然)我实际上并不知道我在这里做什么,所以任何帮助,在失踪的一块将不胜感激!

简而言之,我所追求的,就是把我的IP地址列表(我有一个文本文件有一个头文件,然后每一行都是一个新的IP地址,这些文件是x长度的,每次运行都会有所不同 – 偶尔它将是空的,所以如果我可以检查文件的长度,并且是否只包含标题来跳过这个过程会很方便。 检查这些IP与freegeoip.net,并将它们提供的数据整理到一个文件中,以便将用户的位置与我通过IP保存的其他数据关联起来。

干杯!

把整个脚本放在脚本中。 在这种情况下,无需将PowerShell封装在batch file中。 实际上,这样做会损失很多,因为PowerShell是面向对象的。

 # for the filename, this is a string # quotes needed if there's a space, # use single quotes unless the string is an expression which needs evaluation # # Import-CSV builds an array of objects from the contents of the csv # using the first row as a header # in your case the objects will be strings which are IP addresses # # For your own benefit, make sure the ip address column # (only one columnt per your post) has a simple title in the first row $IPList = Import-CSV E:\DATA\02.ips.txt # the Foreach command is a for loop used with arrays or collections to loop through all the members Foreach ($IPAddress in $IPList) { # In PowerShell, wget is an alias for Invoke-WebRequest # the $() wrapper around the $IPAddress.ipaddress is to force evaluation of that expression Invoke-WebRequest "freegeoip.net/csv/$($IPAddress.ipaddress)" -OutFile "E:\DATA\IPs\$($IPAddress.ipaddress).txt" } 

这里是我编码的一个batch file,它使用ipinfo.io的API进行IP查找。 https://pastebin.com/Ucs2Kuqn享受!