如何在 Windows 命令行中将变量设置为 IP?

发布于 2024-12-06 23:59:55 字数 213 浏览 1 评论 0原文

有没有一种简单的方法可以从我的服务提供商处获取 IP 地址并通过命令提示符将其放入变量中?类似于以下内容:

SET hostIP = nslookup \address
ECHO %hostIP%

或者

SET hostIP = ipconfig \address
ECHO %hostIP%

Is there an easy way to grab the IP address from my service provider and put it into a variable via command prompt? Something like the following:

SET hostIP = nslookup \address
ECHO %hostIP%

Or

SET hostIP = ipconfig \address
ECHO %hostIP%

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一紙繁鸢 2024-12-13 23:59:55
for /f "skip=1 tokens=2 delims=: " %f in ('nslookup %COMPUTERNAME% ^| find /i "Address"') do echo %f
for /f "skip=1 tokens=2 delims=: " %f in ('nslookup %COMPUTERNAME% ^| find /i "Address"') do echo %f
二智少女猫性小仙女 2024-12-13 23:59:55

Arun 的答案很好,但我发现,当为给定主机分配/关联多个 IP 时,使用 NSLOOKUP 会在主机名后生成一个恶意逗号。

然而,我确实找到了另一种方法,可以从给定的主机名解析(第一个分配的)IP,并且不会生成恶意逗号 - 它使用 PING。非常快,非常可靠。

for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 %COMPUTERNAME% ^| find /i "pinging"') do echo IP=%f

它为主机名生成一个简单的 IPv4 地址到变量 IP 中。如果您随后执行 ECHO %IP%,它将显示 IP,如下所示:
IP=192.168.1.2

当然,在批处理脚本中,您需要将单个 %f 替换为 %%f。请注意管道(“|”)符号前面的克拉(“^”),这在批处理脚本中是必需的,这样它们就不会解释管道,而是将 ping 语句的结果通过管道传输到 find 语句。

The answer by Arun is good but I found that using NSLOOKUP generates a rogue comma after the hostname when more than one IP is assigned/associated with a given host.

However, I did find another way that resolves the (first assigned) IP from a given host name and doesn't generate the rogue comma - it uses PING. Very fast, very reliable.

for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 %COMPUTERNAME% ^| find /i "pinging"') do echo IP=%f

It generates a simple IPv4 address for the hostname into the variable IP. If you then do an ECHO %IP% it will show you the IP like:
IP=192.168.1.2

Of course, in batch scripts, you're going to need to replace the single %f with %%f. Note the carat ("^") in front of the pipe ("|") symbol, which is required in batch scripts so they don't interpret the pipe, and instead pipes the results of the ping statement to the find statement.

清秋悲枫 2024-12-13 23:59:55

如果您可以使用 bash(如在 cygwin 中),则可以使用反引号轻松完成 SET hostIP 行中您想要执行的任何操作。

如在

export hostIP = `curl 'http://whatsmyip.net' | grep '<title' | awk '{print $8}' | sed -e 's:<.*::g'`

If you could use bash, (as in cygwin) this would easily be done using back-ticks to execute anything you want in your SET hostIP line.

As in

export hostIP = `curl 'http://whatsmyip.net' | grep '<title' | awk '{print $8}' | sed -e 's:<.*::g'`
滥情稳全场 2024-12-13 23:59:55

尝试像这样的批处理来设置环境变量:

ipconfig > ipconfig.out
setx IPADDR /f ipconfig.out /a 7,13
setx IPADDR /f ipconfig.out /a 7,14
setx IPMASK /f ipconfig.out /a 8,14

退出命令提示符并打开一个新命令提示符。使用 SET 并查找 IPADDR 和 IPMASK,它们现在是持久的。要更新变量,您必须重新运行批处理并退出命令提示符。显示的不同坐标说明了 Windows 2003 与 Windows 2008 的 IPCONFIG 输出的差异(在 XP/7 上应该以相同的方式工作)。仅写入找到的值,因此只要未找到任何内容,失败的行就不会造成任何损害。添加网关:

setx IPGATE /f ipconfig.out /a 9,12

Try a batch like this to set environment variables:

ipconfig > ipconfig.out
setx IPADDR /f ipconfig.out /a 7,13
setx IPADDR /f ipconfig.out /a 7,14
setx IPMASK /f ipconfig.out /a 8,14

Exit the command prompt and open a new one. Use SET and look for IPADDR and IPMASK, which are now persistent. To update the variables, you would have to rerun the batch and exit the command prompt. The different coordinates shown account for differences in the IPCONFIG output for Windows 2003 vs Windows 2008 (should work on XP/7 in the same way). Only a found value is written, so the line that fails does no harm as long as nothing is found. Add the gateway with:

setx IPGATE /f ipconfig.out /a 9,12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文