Batch script to get website ip address?

不打扰是莪最后的温柔 提交于 2019-12-13 02:34:20

问题


I'm trying to put together a batch file, that will ping a website, and assign its ip to a variable - I've searched around, but haven't really been able to put something together. Can anyone shove me in the right direction.

Tim.


回答1:


You could try the ping command. The idea is to get the part between the [], of the ping output.

@echo off
setlocal EnableDelayedExpansion
set myServer=google.de

for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (
 if "%%b" NEQ "" set ip=%%b
)
echo ip is %ip%



回答2:


If all you want to do is look up the addresses, you might want to use nslookup rather than ping. Doing a search for "nslookup batch" gives you a bunch of results, including this one that looks like it should be fairly easy to adapt since it stores the result in variables.




回答3:


Same as @jeb answer, above, but without using EnableDelayedExpansion, just replace "www.google.com" with you favorite site or %variablename%:

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


来源:https://stackoverflow.com/questions/3870090/batch-script-to-get-website-ip-address

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