如何将IP地址转换为批处理文件变量?

时间:2021-03-25 23:30:23

I have an odd question, not sure if its possible.

我有一个奇怪的问题,不确定是否有可能。

I'd like to write a script, and for example I'm going to use ipconfig as my command.

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

Now when you normally run this command theres a ton of output.

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

What I'd like to have is a script that would show only the IP address, for example.

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

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

The output would be

的输出将是

Network Connection Test

Your IP Address is: 192.168.1.1

Is this even possible?

这是可能吗?

15 个解决方案

#1


34  

This will print the IP addresses in the output of ipconfig:

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

@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

To only print the first IP address, just add goto :eof (or another label to jump to instead of :eof) after the echo, or in a more readable form:

要只打印第一个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
)

A more configurable way would be to actually parse the output of ipconfig /all a little bit, that way you can even specify the adapter whose IP address you want:

一种更可配置的方式是实际解析ipconfig /all的输出,这样您甚至可以指定您想要的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


39  

The following code works on any locale of any platform since Windows XP and it looks for the network IP from a (more or less) random of your network cards. It will never take longer than a few milliseconds.

下面的代码适用于任何平台的任何地区,因为Windows XP和它从你的网卡(或多或少)随机地寻找网络IP。它永远不会超过几毫秒。

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

The following one looks for your public IP instead and works on Windows 7 and newer machines.

下面的内容将用于您的公共IP,并在Windows 7和更新的机器上工作。

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

You can find detailed explanations of these commands on my blog.

你可以在我的博客上找到这些命令的详细解释。

#3


15  

In Windows 7:

在Windows 7:

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

#4


11  

Extracting the address all by itself is a bit difficult, but you can get the entire IP Address line easily.

将地址本身提取出来有点困难,但是您可以轻松地获取整个IP地址。

To show all IP addresses on any English-language Windows OS:

显示所有英文视窗操作系统的所有IP地址:

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

To show only IPv4 or IPv6 addresses on Windows 7+:

在Windows 7+上只显示IPv4或IPv6地址:

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

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

Here's some sample output from an XP machine with 3 network adapters.

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

        IP Address. . . . . . . . . . . . : 192.168.1.10
        IP Address. . . . . . . . . . . . : 10.6.102.205
        IP Address. . . . . . . . . . . . : 192.168.56.1

#5


10  

@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%

#6


4  

This work even if you have a virtual network adapters or VPN connections:

即使你有一个虚拟的网络适配器或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%

#7


3  

This is a modification of @mousio's answer. My network connection is not persistent hence the IP address of the next adapter gets displayed if the string "IPv4 Address" is missing from ipconfig. The result of ipconfig has 2 blank spaces between adapters. After an adapter is found and 2 blank lines occurs before the "IPv4 Address" text, it assumes it is missing. Tested on Windows 7 64-bit only.

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

Processing blank lines from @dbenham's answer in: DOS batch FOR loop with FIND.exe is stripping out blank lines?

处理来自@dbenham的答案的空白行:DOS批处理循环查找。exe去掉了空行?

@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

#8


2  

Using IPCONFIG is good, unless you want the flexibility of getting the IP of a remote host in addition to the local host. To get it from something like www.google.com using PING try:

使用IPCONFIG很好,除非您希望在本地主机之外获得远程主机的IP的灵活性。要想从www.google.com这样的东西中获取信息,请使用PING:

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

The result for that example is:

这个例子的结果是:

IP=173.194.73.106

To get the first IP of your local host, replace "www.google.com" with "%computername%" without the quotes. Note the "-4" in front will always get the IPv4 address instead of a possible IPv6 address. Omit as needed. Note also that this technique cannot get more than one IP address. If that is your goal, you'll need to use NSLOOKUP with some extra code.

要获得本地主机的第一个IP,请用“%computername%”替换“www.google.com”,而不用引号。注意,前面的“-4”总是会得到IPv4地址,而不是可能的IPv6地址。根据需要省略。还要注意,此技术不能获得多个IP地址。如果这是您的目标,您将需要使用一些额外的代码来使用NSLOOKUP。

Mind you, if you use NSLOOKUP instead of PING, and the host has more than one IP address, then your variable will have a comma at the end, since each address will be separated by a comma.

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

#9


2  

I know this is an older post but I ran across this while trying to solve the same problem in vbscript. I haven't tested this with mulitple network adapters but hope that it's helpful nonetheless.

我知道这是一个较旧的帖子,但我在试图解决vbscript中同样的问题时遇到了这个问题。我还没有使用mulitple网络适配器来测试它,但希望它会有所帮助。

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

This assumes Win7. For XP, replace IPv4 Address with IP Address.

这个假设这个主题。对于XP,用IP地址替换IPv4地址。

#10


1  

Assuming a windows OS as you mention i p config

假设有一个windows操作系统,就像你提到的i p配置。

If you're willing to install some Unixy utilities like a windows-port of grep and cut you can do that. However, in cases like your example with ipconfig it will be a mess in machines with multiple NICs or e.g VMWare.

如果您愿意安装一些unix实用工具,比如windows-port of grep和cut,您可以这样做。但是,在像您的ipconfig这样的例子中,它将会在具有多个NICs或e的机器上造成混乱。g VMWare。

Powershell might be the tool you want, look here for a example.

Powershell可能是您想要的工具,请看这里的一个示例。

#11


1  

In linux environment:

在linux环境:

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

or

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

example in FreeBSD:

在FreeBSD示例:

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

If you have more than one IP address configured, you will have more than one IP address in stdout.

如果配置了多个IP地址,则在stdout中将有多个IP地址。

#12


0  

try something like this

这样的尝试

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

linux like systems

linux这样的系统

#13


0  

The following was all done in cygwin on a Windows XP box.

下面这些都是在Windows XP系统的cygwin中完成的。

This will get your IP address. Note that there are backquotes around the hostname command, not single quotes.

这将得到你的IP地址。注意,在主机名命令周围有一些反向引号,而不是单引号。

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

This will get your subnet.

这将得到你的子网。

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

The following will list all hosts on your local network (put it into a script called "netmap"). I had taken the subnet line above and put it into an executable called "getsubnet", which I then called from the following script.

下面将列出本地网络上的所有主机(将其放入名为“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}

#14


0  

Below script will store the ip address to the variable ip_address

下面的脚本将把ip地址存储到变量ip_address。

@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

Ideas from this blog post.

这篇博客的观点。

#15


-3  

if you just want the ip address try this:

如果你想要ip地址试试这个:

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

echo "$ipaddr"

#1


34  

This will print the IP addresses in the output of ipconfig:

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

@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

To only print the first IP address, just add goto :eof (or another label to jump to instead of :eof) after the echo, or in a more readable form:

要只打印第一个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
)

A more configurable way would be to actually parse the output of ipconfig /all a little bit, that way you can even specify the adapter whose IP address you want:

一种更可配置的方式是实际解析ipconfig /all的输出,这样您甚至可以指定您想要的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


39  

The following code works on any locale of any platform since Windows XP and it looks for the network IP from a (more or less) random of your network cards. It will never take longer than a few milliseconds.

下面的代码适用于任何平台的任何地区,因为Windows XP和它从你的网卡(或多或少)随机地寻找网络IP。它永远不会超过几毫秒。

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

The following one looks for your public IP instead and works on Windows 7 and newer machines.

下面的内容将用于您的公共IP,并在Windows 7和更新的机器上工作。

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

You can find detailed explanations of these commands on my blog.

你可以在我的博客上找到这些命令的详细解释。

#3


15  

In Windows 7:

在Windows 7:

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

#4


11  

Extracting the address all by itself is a bit difficult, but you can get the entire IP Address line easily.

将地址本身提取出来有点困难,但是您可以轻松地获取整个IP地址。

To show all IP addresses on any English-language Windows OS:

显示所有英文视窗操作系统的所有IP地址:

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

To show only IPv4 or IPv6 addresses on Windows 7+:

在Windows 7+上只显示IPv4或IPv6地址:

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

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

Here's some sample output from an XP machine with 3 network adapters.

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

        IP Address. . . . . . . . . . . . : 192.168.1.10
        IP Address. . . . . . . . . . . . : 10.6.102.205
        IP Address. . . . . . . . . . . . : 192.168.56.1

#5


10  

@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%

#6


4  

This work even if you have a virtual network adapters or VPN connections:

即使你有一个虚拟的网络适配器或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%

#7


3  

This is a modification of @mousio's answer. My network connection is not persistent hence the IP address of the next adapter gets displayed if the string "IPv4 Address" is missing from ipconfig. The result of ipconfig has 2 blank spaces between adapters. After an adapter is found and 2 blank lines occurs before the "IPv4 Address" text, it assumes it is missing. Tested on Windows 7 64-bit only.

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

Processing blank lines from @dbenham's answer in: DOS batch FOR loop with FIND.exe is stripping out blank lines?

处理来自@dbenham的答案的空白行:DOS批处理循环查找。exe去掉了空行?

@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

#8


2  

Using IPCONFIG is good, unless you want the flexibility of getting the IP of a remote host in addition to the local host. To get it from something like www.google.com using PING try:

使用IPCONFIG很好,除非您希望在本地主机之外获得远程主机的IP的灵活性。要想从www.google.com这样的东西中获取信息,请使用PING:

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

The result for that example is:

这个例子的结果是:

IP=173.194.73.106

To get the first IP of your local host, replace "www.google.com" with "%computername%" without the quotes. Note the "-4" in front will always get the IPv4 address instead of a possible IPv6 address. Omit as needed. Note also that this technique cannot get more than one IP address. If that is your goal, you'll need to use NSLOOKUP with some extra code.

要获得本地主机的第一个IP,请用“%computername%”替换“www.google.com”,而不用引号。注意,前面的“-4”总是会得到IPv4地址,而不是可能的IPv6地址。根据需要省略。还要注意,此技术不能获得多个IP地址。如果这是您的目标,您将需要使用一些额外的代码来使用NSLOOKUP。

Mind you, if you use NSLOOKUP instead of PING, and the host has more than one IP address, then your variable will have a comma at the end, since each address will be separated by a comma.

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

#9


2  

I know this is an older post but I ran across this while trying to solve the same problem in vbscript. I haven't tested this with mulitple network adapters but hope that it's helpful nonetheless.

我知道这是一个较旧的帖子,但我在试图解决vbscript中同样的问题时遇到了这个问题。我还没有使用mulitple网络适配器来测试它,但希望它会有所帮助。

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

This assumes Win7. For XP, replace IPv4 Address with IP Address.

这个假设这个主题。对于XP,用IP地址替换IPv4地址。

#10


1  

Assuming a windows OS as you mention i p config

假设有一个windows操作系统,就像你提到的i p配置。

If you're willing to install some Unixy utilities like a windows-port of grep and cut you can do that. However, in cases like your example with ipconfig it will be a mess in machines with multiple NICs or e.g VMWare.

如果您愿意安装一些unix实用工具,比如windows-port of grep和cut,您可以这样做。但是,在像您的ipconfig这样的例子中,它将会在具有多个NICs或e的机器上造成混乱。g VMWare。

Powershell might be the tool you want, look here for a example.

Powershell可能是您想要的工具,请看这里的一个示例。

#11


1  

In linux environment:

在linux环境:

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

or

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

example in FreeBSD:

在FreeBSD示例:

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

If you have more than one IP address configured, you will have more than one IP address in stdout.

如果配置了多个IP地址,则在stdout中将有多个IP地址。

#12


0  

try something like this

这样的尝试

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

linux like systems

linux这样的系统

#13


0  

The following was all done in cygwin on a Windows XP box.

下面这些都是在Windows XP系统的cygwin中完成的。

This will get your IP address. Note that there are backquotes around the hostname command, not single quotes.

这将得到你的IP地址。注意,在主机名命令周围有一些反向引号,而不是单引号。

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

This will get your subnet.

这将得到你的子网。

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

The following will list all hosts on your local network (put it into a script called "netmap"). I had taken the subnet line above and put it into an executable called "getsubnet", which I then called from the following script.

下面将列出本地网络上的所有主机(将其放入名为“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}

#14


0  

Below script will store the ip address to the variable ip_address

下面的脚本将把ip地址存储到变量ip_address。

@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

Ideas from this blog post.

这篇博客的观点。

#15


-3  

if you just want the ip address try this:

如果你想要ip地址试试这个:

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

echo "$ipaddr"