在 Anaconda Powershell 中只获取 IP 地址

Get only the IP Adress in Anaconda Powershell

我只想在 Windows 中的 Anaconda Powershell 中获取我的 IP 地址的输出。

curl https://ipinfo.io/ip

给我以下内容:

    StatusCode        : 200
    StatusDescription : OK
    Content           : 22.031.123.456
    RawContent        : HTTP/...
                        ...
                        ...
                    ...
                    ...
                    Content-Type: text/html; charset=utf-8
                    Date: ...
                    Via:...
Forms             : {}
Headers           : {[..., *], [...], [...],
                    [..., ...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : ...
RawContentLength  : ...

如您所见,“内容”显示我的 IP 地址。但如果没有任何其他信息,我无法获得 IP 地址。

注意:“curl api.ipify.org”和“curl ipinfo.io/ip”以及其他人正在做完全相同的事情。

Windows PowerShell 中,curl 参考外部curl.exe程序;相反,它 是 PowerShell Invoke-WebRequest cmdlet.

的内置别名

为了调用curl.exe包括文件扩展名,.exe:

curl.exe ipinfo.io/ip  # Note: The https:// part isn't strictly needed.

请注意,PowerShell (Core) v6+ 中不再需要,其中 curl 别名已被删除,以及其他,以免影响操作系统附带的外部程序。


另请注意,您也可以使用 Invoke-RestMethod cmdlet,这与 Invoke-WebRequest 不同,后者 returns wrapper object around the actual data - returns response data directly(并且,如果适用,将 XML 数据解析为 [xml] 个实例和 JSON 个数据到 [pscustomobject] 个实例);它的内置别名是 irm:

irm ipinfo.io/ip  # irm == Invoke-RestMethod

作为一般提示:确定哪个命令(PowerShell 本地命令或外部程序)给定名称,例如curl指的是,使用 Get-Command cmdlet.

  • 默认情况下,将列出具有该名称的有效命令。
  • 如果添加 -All 开关,您可能会看到 shadowed 命令表单。
    • 在 Windows 上,如果隐藏了外部程序,您仍然可以通过排除 .exe 扩展名来调用它,如图所示。

    • 一般来说,如果隐藏命令的类型与有效命令不同,您可以使用Get-Command-Type 参数来定位隐藏命令并通过 &call operator 调用它;例如,在 Windows 上附加 .exe 的(不太理想的)替代方法是:

      & (Get-Command -Type Application curl) ipinfo.io/ip