How do I get the IP address into a batch-file variable?
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.
Now when you normally run th开发者_Go百科is command theres a ton of output.
What I'd like to have is a script that would show only the IP address, for example.
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?
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.
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.
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.
This will print the IP addresses in the output of ipconfig
:
@echo off
set ip_address_string="IPv4 Address"
rem Uncomment the following line when using older versions of Windows without IPv6 support (by removing "rem")
rem set ip_address_string="IP 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:
set ip_address_string="IPv4 Address"
rem Uncomment the following line when using older versions of Windows without IPv6 support (by removing "rem")
rem set ip_address_string="IP 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:
@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
)
)
In Windows 7:
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
echo %ip%
pause
@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%
Extracting the address all by itself is a bit difficult, but you can get the entire IP Address line easily.
To show all IP addresses on any English-language Windows OS:
ipconfig | findstr /R /C:"IP.* Address"
To show only IPv4 or IPv6 addresses on Windows 7+:
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.
IP Address. . . . . . . . . . . . : 192.168.1.10
IP Address. . . . . . . . . . . . : 10.6.102.205
IP Address. . . . . . . . . . . . : 192.168.56.1
tldr;
I wasn't able use any of the above answers in my use case so I used the following command to get the ip address of my Ethernet network adapter and echo it as mentioned in the above question.
In a command line:
for /f "tokens=3 delims=: " %i in ('netsh interface ip show config name^="Ethernet" ^| findstr "IP Address"') do echo Your IP Address is: %i
Or in a batch file:
for /f "tokens=3 delims=: " %%i in ('netsh interface ip show config name^="Wi-Fi" ^| findstr "IP Address" ^| findstr [0-9]') do set IP=%%i
For those who are curious to know what all that means, read on
Most commands using ipconfig
for example just print out all your IP addresses and I needed a specific one which in my case was for my Ethernet network adapter.
You can see your list of network adapters by using the netsh interface ipv4 show interfaces
command. Most people need Wi-Fi or Ethernet.
You'll see a table like so in the output to the command prompt:
Idx Met MTU State Name
--- ---------- ---------- ------------ ---------------------------
1 75 4294967295 connected Loopback Pseudo-Interface 1
15 25 1500 connected Ethernet
17 5000 1500 connected vEthernet (Default Switch)
32 15 1500 connected vEthernet (DockerNAT)
In the name column you should find the network adapter you want (i.e. Ethernet, Wi-Fi etc.).
As mentioned, I was interested in Ethernet
in my case.
To get the IP for that adapter we can use the netsh command:
netsh interface ip show config name="Ethernet"
This gives us this output:
Configuration for interface "Ethernet"
DHCP enabled: Yes
IP Address: 169.252.27.59
Subnet Prefix: 169.252.0.0/16 (mask 255.255.0.0)
InterfaceMetric: 25
DNS servers configured through DHCP: None
Register with which suffix: Primary only
WINS servers configured through DHCP: None
(I faked the actual IP number above for security reasons
精彩评论