2011-05-05 77 views
35

我有一个奇怪的问题,不知道它是否可能。如何将IP地址获取到批处理文件变量中?

我想写一个脚本,例如我打算使用ipconfig作为我的命令。

现在,当你正常运行这个命令时,会产生大量的输出。

我想要的是一个脚本,只会显示IP地址,例如。

echo Network Connection Test 
ipconfig <---This would run in the background 
echo Your IP Address is: (INSERT IP ADDRESS HERE) 

输出将

Network Connection Test 

Your IP Address is: 192.168.1.1 

这甚至可能吗?

+1

你可以有多个IP地址,大多数机器甚至有多个IP地址。那么你想要哪一个? – Joey 2011-05-05 20:50:43

回答

34

这将打印的IP地址中的ipconfig输出:

@echo off 
set ip_address_string="IP Address" 
rem Uncomment the following line when using Windows 7 (with removing "rem")! 
rem set ip_address_string="IPv4 Address" 
echo Network Connection Test 
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do echo Your IP Address is: %%f 

仅打印第一个IP地址,刚过添加goto :eof(或其他标签跳转到的,而不是:eof)回声或以更易读的形式:

set ip_address_string="IP Address" 
rem Uncomment the following line when using Windows 7 or Windows 8/8.1 (with removing "rem")! 
rem set ip_address_string="IPv4 Address" 
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
    echo Your IP Address is: %%f 
    goto :eof 
) 

更可配置的方法是实际解析的输出一点点,这样你甚至可以指定要其IP地址的适配器:

@echo off 
setlocal enabledelayedexpansion 
::just a sample adapter here: 
set "adapter=Ethernet adapter VirtualBox Host-Only Network" 
set adapterfound=false 
echo Network Connection Test 
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do (
    set "item=%%f" 
    if /i "!item!"=="!adapter!" (
     set adapterfound=true 
    ) else if not "!item!"=="!item:IP Address=!" if "!adapterfound!"=="true" (
     echo Your IP Address is: %%g 
     set adapterfound=false 
    ) 
) 
+2

我认为您需要将“IP地址”更改为“IP地址”,或将'/ I'传递给'findstr'以区分大小写比赛。 – indiv 2011-05-05 18:07:07

+0

@indiv:正确(我有不同的区域设置,我手动更改了字符串);答案更新... – mousio 2011-05-05 18:18:38

+3

在我的Win7电脑上匹配的东西字符串是“IPv4地址”。 – 2011-05-05 18:48:21

0

尝试这样的事情

echo "yours ip addresses are:" 
ifconfig | grep "inet addr" | cut -d':' -f2 | cut -d' ' -f1 

的Linux类系统

1

假设Windows操作系统你提到我p配置

如果你愿意安装一些Unixy公用事业像grep的windows-port和切你可以做到这一点。但是,在像ipconfig这样的例子中,它将会在具有多个NIC或例如VMWare的机器中变得混乱。

Powershell可能是你想要的工具,看看here为例。

+0

这是非常感谢,我正在寻找一个非常简单的方法来执行此操作,但是要感谢信息 – level42 2011-05-05 14:34:10

11

提取地址本身有点困难,但您可以轻松获得整个IP地址线。

为了显示在任何英文Windows操作系统的所有IP地址:

ipconfig | findstr /R /C:"IP.* Address" 

要仅显示IPv4或IPv6的Windows地址7+:

ipconfig | findstr /R /C:"IPv4 Address" 

ipconfig | findstr /R /C:"IPv6 Address" 

下面是一个XP的一些示例输出带有3个网络适配器的机器。

 IP Address. . . . . . . . . . . . : 192.168.1.10 
     IP Address. . . . . . . . . . . . : 10.6.102.205 
     IP Address. . . . . . . . . . . . : 192.168.56.1 
+1

On> = Windows 7,您应该找到字符串“IPv4地址”(而不是“IP地址”),所以正确的命令如下所示:'ipconfig | findstr/R/C:“IPv4地址。*:”'。我想你应该把它添加到你的文章。 :)我虽然upvoted,因为这是另一个好主意。 :) – Sk8erPeter 2013-11-21 18:01:24

+0

@ Sk8erPeter:谢谢!我用新的正则表达式更新了答案。 – indiv 2013-11-21 19:36:56

+0

不客气!并感谢您的更新。 'ipconfig | findstr/R /C:"IP.*地址“'你刚才写的是一个更好的主意:) – Sk8erPeter 2013-11-22 16:50:01

2

我知道这是一个老的文章,但我碰到这个跑在试图解决在VBScript相同的问题。我没有用多个网络适配器测试过,但希望它有帮助。

for /f "delims=: tokens=2" %%a in ('ipconfig ^| findstr /R /C:"IPv4 Address"') do (set tempip=%%a) 
set tempip=%tempip: =% 
echo %tempip% 

这是假设Win7。对于XP,请将IPv4 Address替换为IP Address

2

使用IPCONFIG是很好的,除非你想获得除本地主机以外的远程主机IP的灵活性。要使用PING尝试从得到的东西像www.google.com:

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

结果对于例子是:

IP=173.194.73.106 

为了让您的本地主机的第一个IP,将“WWW。 google.com“和”%computername%“,不含引号。请注意,前面的“-4”将始终获得IPv4地址,而不是可能的IPv6地址。根据需要省略。还要注意,这种技术不能获得多个IP地址。如果这是你的目标,你需要使用带有一些额外代码的NSLOOKUP。

请注意,如果您使用NSLOOKUP而不是PING,并且主机有多个IP地址,那么您的变量最后会有一个逗号,因为每个地址都会用逗号分隔。

+1

'ping localhost'会给你'127.0.0.1'。改为使用'ping%computername%'。 – Stephan 2013-09-02 09:43:37

+1

对我而言最佳。另外用delims = []过滤“ping”似乎是最健壮的,恕我直言。 – mkoertgen 2015-08-11 14:04:42

15

在Windows 7中:

for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b 
set ip=%ip:~1% 
echo %ip% 
pause 
+1

感谢'set ip =%ip:〜1%'来摆脱前面的额外空间。 :) – sudheeshix 2016-10-05 07:18:57

3

这是@ mousio的回答修改。我的网络连接不是永久的,因此如果ipconfig中缺少字符串“IPv4地址”,则会显示下一个适配器的IP地址。 ipconfig的结果在适配器之间有2个空格。在找到适配器并且在“IPv4地址”文本之前出现2个空白行之后,它假定它缺失。仅在Windows 7 64位上进行测试。

处理来自@ dbenham的回答空行: DOS batch FOR loop with FIND.exe is stripping out blank lines?

@echo off 

rem --- complete adapter name to find without the ending ":" --- 
set adapter=Wireless LAN adapter Wireless Network Connection 

rem --- token under an adapter to extract IP address from --- 
set IPAddrToken=IPv4 Address 

setlocal enableextensions enabledelayedexpansion 
set adapterfound=false 
set emptylines=0 
set ipaddress= 

for /f "usebackq tokens=1-3 delims=:" %%e in (`ipconfig ^| findstr /n "^"`) do (

    set "item=%%f" 

    if /i "!item!"=="!adapter!" (
     set adapterfound=true 
     set emptylines=0 
    ) else if not "!item!"=="" if not "!item!"=="!item:%IPAddrToken%=!" if "!adapterfound!"=="true" (
     @rem "!item:%IPAddrToken%=!" --> item with "IPv4 Address" removed 
     set ipaddress=%%g 
     goto :result 
    ) 
    if "%%f-%%g-!adapterfound!-!emptylines!"=="--true-1" (
     @rem 2nd blank line after adapter found 
     goto :result 
    ) 
    if "%%f-%%g-!adapterfound!-!emptylines!"=="--true-0" (
     @rem 1st blank line after adapter found 
     set emptylines=1 
    ) 
) 

endlocal 

:result 
    echo %adapter% 
    echo. 
    if not "%ipaddress%"=="" (
     echo %IPAddrToken% =%ipaddress% 
    ) else (
     if "%adapterfound%"=="true" (
      echo %IPAddrToken% Not Found 
     ) else (
      echo Adapter Not Found 
     ) 
    ) 
    echo. 

pause 
0

以下是在Cygwin的全部完成在Windows XP框。

这将得到您的IP地址。请注意,hostname命令周围有反引号,而不是单引号。

ping -n 1 `hostname` | grep "Reply from " | cut -f 3 -d " " | cut -f 1 -d ":" 

这将得到您的子网。

ping -n 1 `hostname` | grep "Reply from " | cut -f 3 -d " " | cut -f "1 2 3" -d "." 

以下将列出本地网络上的所有主机(将其放入称为“netmap”的脚本)。我已将上面的子网行放入一个名为“getsubnet”的可执行文件中,然后我从下面的脚本中调用它。

MINADDR=0 
MAXADDR=255 
SUBNET=`getsubnet` 

hostcnt=0 

echo Pinging all addresses in ${SUBNET}.${MINADDR}-${MAXADDR} 

for i in `seq $MINADDR $MAXADDR`; do 
addr=${SUBNET}.$i 
ping -n 1 -w 0 $addr > /dev/null 

if [ $? -ne 1 ]  
then  
echo $addr UP  
hostcnt=$((hostcnt+1))  
fi 

done 

echo Found $hostcnt hosts on subnet ${SUBNET}.${MINADDR}-${MAXADDR} 
0

下面的脚本将IP地址

@echo off 

call :get_ip_address 
echo %ip_address% 

goto :eof 

REM 
REM get the ip address 
REM 
:get_ip_address 
FOR /f "tokens=1 delims=:" %%d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') do (FOR /F "tokens=3 delims= " %%g IN ("%%d") DO set ip_address=%%g) 

goto :eof 

创意从this blog post存储变量IP_ADDRESS。

39

下面的代码上任何平台上的任何区域,因为Windows XP中,它看起来从一个(或多或少)随机的网络卡网络IP。它永远不会超过几毫秒。

for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a 
echo Network IP: %NetworkIP% 

下一个查找您公共IP,而不是和适用于Windows 7和新机器。

for /f %%a in ('powershell Invoke-RestMethod api.ipify.org') do set PublicIP=%%a 
echo Public IP: %PublicIP% 

你可以在my blog找到这些命令的详细解释。

+2

它给了我一个错误:“%% a在这个时候是意外的。” – FelikZ 2014-10-13 12:29:26

+1

这仅适用于批处理,您可能正在命令行中运行它,对吧? – 2014-10-13 13:50:10

+0

是的。在该文件中,它的工作非常完美你能解释为什么吗? – FelikZ 2014-10-13 15:01:16

4

即使你有一个虚拟网络适配器或VPN连接,这项工作:

FOR /F "tokens=4 delims= " %%i in ('route print ^| find " 0.0.0.0"') do set localIp=%%i 
echo Your IP Address is: %localIp% 
+0

我喜欢它,太棒了! :-) – 2017-07-17 12:42:01

9
@echo off 

FOR /F "tokens=4 delims= " %%i in ('route print ^| find " 0.0.0.0"') do set localIp=%%i 

echo Your IP Address is: %localIp% 
+0

我可以限制界面吗? – Rick 2014-12-22 17:41:31

+0

最好的解决方案,如果你有多个适配器,如VirtualBox,VMWare – Kennet 2018-03-07 13:28:24

-3

如果你只是想在IP地址试试这个:

#Variable to store ip address 
ipaddr=$(hostname -I) 

echo "$ipaddr" 
+1

这个问题是关于Windows批处理文件,你显示一个答案为Linux bash – jeb 2015-11-14 08:51:31

1

在Linux环境:

ip="$(ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -d ':' -f 2)" 

ip="$(ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -d ':' -f 2)" | echo $ip 

例如,在FreeBSD下:

ifconfig re0 | grep -v "inet6" | grep -i "inet" | awk '{print $2}' 

如果你有一个以上的IP地址配置,你会在标准输出多个IP地址。