简体   繁体   中英

Batch script to get website ip address?

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.

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%

与上面的@jeb答案相同,但不使用EnableDelayedExpansion ,只需将“ www.google.com”替换为您喜欢的网站或%variablename%

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM