Only get IPv4 address without the “IPv4 Address. . . . . . . . . . . :” in bat-file

雨燕双飞 提交于 2019-12-23 03:15:44

问题


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 a for /f command

  • The first line in the ping output includes the ip address enclosed in square brackets

  • The 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!