15 February, 2013

Get machine IP via script (and check if it's up)

Hi,

I encountered a problem, I have a list of servers from AD and wanted to get the IP on each server.
It seems that this is not easy, I could not find a clean way to do it on the web.

As a batch fan I wrote a small script that get's, as an input, a list of names, ping them and echo out to a file the name and the IP (so you can import it to excel easily).

You need to prepare a file named list.txt with all the names to convert to IP.

Next, create 2 files:

STEP1.CMD
========================================

del pinged_list.txt /q
for /f %%A in (list.txt) do (step2.cmd %%A)
========================================

STEP2.CMD
========================================

ping %1 -n 1 -w 1| find "TTL=" > 1.txt
if %errorlevel% NEQ 0 goto end

for /f "tokens=1-3 delims= " %%A in (1.txt) do (echo %%C > 2.txt)
for /f "tokens=1-3 delims=:" %%A in (2.txt) do (echo %1, %%A >>pinged_list.txt)
del 2.txt /q

:end
del 1.txt /q
=========================================

run step1.cmd and wait to the pinged_list.txt file to be prepared.

Enjoy...