How to write a function to get multiple ip ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

How to write a function to get multiple ip ?

#1 Post by Hackoo » 17 Jan 2015 02:00

Hi :)
I have this script to get ip adress from cmd line that works fine :wink:

Code: Select all

@echo off
Title Getting The IP address
Setlocal EnableDelayedExpansion
set myServer=www.yahoo.com
for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (
 if "%%b" NEQ "" set ip=%%b
)
echo The IP address of !myServer! is !ip!
EndLocal
Pause

My aim is how to write a function to get multiple ip ?
I give a try but without success :cry:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
Title Getting The IP address
echo.
echo                       Getting The IP address......
set myServer=www.developpez.net www.yahoo.com bbat.forumeiro.com www.voila.fr www.orange.fr www.google.com
For %%a in (%myServer%) do call :test_ping %%a
Pause
:test_ping
echo.
echo.
for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 %%a') DO (
 if "%%b" NEQ "" set %2=%%b
)
 echo The IP address of %1 is %2

So, i ask you here how to write it ? and what's my mistake ?
Thank you !

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to write a function to get multiple ip ?

#2 Post by foxidrive » 17 Jan 2015 02:46

Here's a way to do it:

Code: Select all

@echo off
Title Getting The IP address
echo.
echo                       Getting The IP address......
set myServer=www.developpez.net www.yahoo.com bbat.forumeiro.com www.voila.fr www.orange.fr www.google.com
For %%z in (%myServer%) do (
   for /f "tokens=2,3 delims=[] " %%a IN ('ping -a -n 1 %%z ^|find "[" ') DO (
      echo The IP address of %%z is %%b and displays as %%a
      echo(
   )
)
pause

Post Reply