Saturday, October 4, 2008

More cmd.exe vs Powershell

This ditz below gets me a columnar listing of IP Address, DNS Name from cmd.exe . In the next post, we will whip that up in Powershell.

:: Produces columnar listing of IP and DNS name for range of IPs
:: Takes one argument with the first three octets of an IP  
:: 'CheckNames 209.85.171 | findstr IP_Name' or
:: 'CheckNames 209.85.171 | findstr IP_Name >> Out.txt'
:: requires Cygwin in path or GNU grep, gawk, tr for Win32

@echo off
 set args=%1
 (for /l %%i in (1,1,255) do ( set OCT=%%i  && set args && call :loop ))
goto end

:loop
set OCT
for /f %%i in ('ping -n 1 -l 1 -w 750 %args%.%OCT% ^| grep Reply ^| gawk '{ print $3 }' ^| tr -d : '  ) do set IP=%%i
for /f %%i in ('nslookup %args%.%OCT% ^| grep Name: ^| gawk '{ print $2 }' ') do set Name=%%i
echo IP_Name %IP% %Name%
 set IP=
 set Name=
:end

Some sample output shows some of the problems in constructing complicated cmd.exe logic:

IP_Name  cg-in-f107.google.com
IP_Name 209.85.171.108 cg-in-f108.google.com
IP_Name
IP_Name
IP_Name
IP_Name 209.85.171.112 cg-in-f112.google.com
IP_Name 209.85.171.113 cg-in-f113.google.com
IP_Name
IP_Name 209.85.171.115 cg-in-f115.google.com
IP_Name 209.85.171.116

We know some ICMP fails, we know some name resolution fails, but we can't easily uncover the reasons for the failures or run additional code to check on the status of the failed ICMP and DNS requests.