仅获取AnacondaPowershell中的IP地址
我只想在 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-WebRequestcmdlet的内置别名。
为了调用curl.exe,包括文件扩展名,.exe:
curl.exe ipinfo.io/ip # Note: The https:// part isn't strictly needed.
请注意,这在PowerShell (Core) v6+ 中不再需要,其中curl别名已被删除,以及其他,以免隐藏操作系统附带的外部程序。
另请注意,您也可以使用Invoke-RestMethodcmdlet,与cmdlet不同Invoke-WebRequest,它返回围绕实际数据的包装器对象-直接返回响应数据(并且,如果适用,将 XML 数据解析为[xml]实例,将 JSON 数据解析为[pscustomobject]实例);它的内置别名是irm:
irm ipinfo.io/ip # irm == Invoke-RestMethod
作为一般提示:要确定给定名称curl所指的是Get-Command哪个命令(PowerShell 本地命令或外部程序),请使用cmdlet。
- 默认情况下,将列出该名称的有效命令。
- 如果添加
-All开关,您可能会看到带阴影的命令表单。-
在 Windows 上,如果外部程序被隐藏,您仍然可以通过排除
.exe扩展名来调用它,如图所示。 -
一般地,如果一个阴影命令是一个的不同类型的比有效命令,则可以使用
Get-Command的-Type参数为目标的阴影命令,并通过调用它&,所述呼叫操作; 例如,.exe在 Windows上附加的(不太理想的)替代方法是:& (Get-Command -Type Application curl) ipinfo.io/ip
-