问题
I will create a little batch file to copy my IP address directly to my clipboard. I have tried:
@echo off
ipconfig | find "IPv4" | clip
pause
But gives me: IPv4 Address. . . . . . . . . . . : 192.168.xx.xx
. Is there a way to only get 192.168.xx.xx
?
回答1:
for /f "tokens=2 delims=[]" %%a in ('ping -n 1 -4 ""') do echo %%a | clip
Execute a
ping
command to local machine (""
), sending only one packet (-n 1
) using ipv4 (-4
)The output of the
ping
command is processed inside afor /f
commandThe first line in the
ping
output includes the ip address enclosed in square bracketsThe
for /f
tokenizes the line using the square brackets as delimiters, and retrieve the second token
v v (delimiters) Pinging computername [x.x.x.x] with 32 bytes of data 1 2 3 (tokens)
回答2:
This batch file can did the trick and can also give you the MAC Address too if you want of course !
@echo off
Title Get IP and MAC Address
@for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
set "MY_IP=%%a"
)
@For /f %%a in ('getmac /NH /FO Table') do (
@For /f %%b in ('echo %%a') do (
If /I NOT "%%b"=="N/A" (
Set "MY_MAC=%%b"
)
)
)
echo Network IP : %MY_IP%
echo MAC Address : %MY_MAC%
pause>nul & exit
回答3:
Here I have created a batch file. you can clip it out as per your requirement.
@echo off
ipconfig | findstr /R /C:"IPv4 Address"
pause
You can see the output of the batch file here...
来源:https://stackoverflow.com/questions/43966069/only-get-ipv4-address-without-the-ipv4-address-in-bat-f