batch file - getting ip from ping command

∥☆過路亽.° 提交于 2019-12-24 06:49:05

问题


i have this command.

ping -n 1 piratelufi.com | find /i "reply from"

i want to get the ip inside the ping response. i thought of using %var:~0,10% but i have no idea how to redirect the output to echo.

i even tried to use pipe

ping -n 1 piratelufi.com | find /i "reply from" | echo %1 

but the %1 variable does not represent the output of the first two commands. i even tried to use &1 but i failed. What is the 'variable' needed to echo out the output from the first two commands?


回答1:


Check out the FOR command it's really cool:

FOR /f "tokens=1,3 delims=: " %%A IN ('ping -n 1 piratelufi.com') DO IF %%A==Reply ECHO IP IS %%B



回答2:


Try the following:

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


来源:https://stackoverflow.com/questions/9171774/batch-file-getting-ip-from-ping-command

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