AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 799930
Accepted
le__bon
le__bon
Asked: 2016-08-31 00:45:19 +0800 CST2016-08-31 00:45:19 +0800 CST 2016-08-31 00:45:19 +0800 CST

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

  • 772

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

我正在使用 freegeoip.net 并且找不到与 cmd 行等效的快速 wget ,因此为此使用 powershell 脚本,从我的批处理文件中调用它(或者至少,尝试,我认为这就是我失败的地方)。

命令行是:

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)

geolocation.ps1 是:

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

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

这个(显然)哈希是从我在谷歌上搜索并在这里和那里收集的内容混合在一起的,但是,(显然)我实际上并不知道我在这里做什么,所以任何对缺失部分的帮助将不胜感激!

简而言之,我所追求的是获取我的 IP 地址列表(我有一个带有标题的文本文件,然后每一行都是一个新 IP,文件的长度为 x 并且每次运行都会有所不同 - 偶尔它将是空的,所以如果我可以检查文件的长度并且它只包含标题以跳过该过程,那将很方便)。对照 freegeoip.net 检查这些 IP,并将它们提供的数据整理到一个文件中,以便在一个程序中使用,该程序将用户的位置与我通过 IP 持有的关于他们的其他数据相关联。

干杯!

powershell command-line-interface ip windows-command-prompt geolocation
  • 2 2 个回答
  • 2640 Views

2 个回答

  • Voted
  1. Best Answer
    Jeter-work
    2016-08-31T09:52:29+08:002016-08-31T09:52:29+08:00

    将整个脚本放入脚本中。在这种情况下,无需将 PowerShell 包装在批处理文件中。实际上,这样做会损失很多,因为 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"
        }
    
    • 1
  2. DSTAT
    2017-07-26T07:25:55+08:002017-07-26T07:25:55+08:00

    这是我编写的一个批处理文件,它使用 ipinfo.io 的 API 进行 IP 查找。https://pastebin.com/Ucs2Kuqn 享受吧!

    • -1

相关问题

  • 资源锁和电源外壳

  • 脚本 - 如何断开远程桌面会话?

  • 如何限制向通讯组发送到外部地址?

  • Powershell对值与数组的作用不同?

  • Windows Powershell Vim 键绑定

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve