# Batch / VB request



## essdeekay (Oct 21, 2009)

I have the following batch script which basically takes a list of IP addresses, pings them to see whether they are on- or off-line and then runs nslookup against the IP to ascertain the Fully Qualified Domain Name that is associated with it. It then outputs all the information for each IP address to a CSV file in 3 separate columns - IP Address, IP-Status and FQDN.

What I would like to do is expand this to accomplish two things:-
- allow an input of either IP addresses or hostnames
- cater for 4 columns: Hostname, IP Address, IP-Status and FQDN

If the input was a list of IP addresses instead of hostnames for example, then the hostname column could just show the text "Not Applicable" or something.

I'm using a batch file as that's what I'm used to, however if this is something that is more easily done in VB then please let me know.

Many thanks.

The current script is as follows:-
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET InputFile=ips.txt
SET OutputFile=PNS-Results.csv

IF NOT EXIST "%InputFile%" ECHO "%InputFile%" file does not exist. &GOTO :EndScript
FOR %%R IN ("%InputFile%") DO IF %%~zR EQU 0 ECHO "%InputFile%" file is empty. &GOTO :EndScript
IF EXIST "%OutputFile%" DEL /F /Q "%OutputFile%"

PING 10.100.65.19 -n 2 | find "TTL" > nul
IF ERRORLEVEL 1 (ECHO "Unable to reach LONADS04 (10.100.65.19). Edit script to use an alternative DNS server" &GOTO :EndScript)

FOR %%i IN (10.100.65.19) do (
ECHO IP Address,IP-Status,FQDN>>!OutputFile!
FOR /F %%c IN ('TYPE "%InputFile%"') DO (
ECHO Processing: %%c
NSLOOKUP.EXE %%c %%i|FIND /I "Name:" >NUL
IF NOT ERRORLEVEL 1 (
FOR /F "tokens=1*" %%k in ('NSLOOKUP.EXE %%c %%i') do (
IF [%%k]==[Name:] set name=%%l)
PING.EXE -n 2 -w 100 %%c | Find/i "TTL=" >Nul && (
CALL ECHO %%c,Online,%%name%%>>"%OutputFile%")|| (
CALL ECHO %%c,Offline,%%name%%>>"%OutputFile%")
)ELSE (PING.EXE -n 2 -w 100 %%c | Find/i "TTL=" >Nul && (
ECHO %%c,Online,Unable to resolve>>"%OutputFile%")|| (
ECHO %%c,Offline,Unable to resolve>>"%OutputFile%"))))

:EndScript
ENDLOCAL
EXIT /B 0


----------



## essdeekay (Oct 21, 2009)

Does anyone have any advice on how I'd be able to get the results I am after?

Or would I just be nesting too many functions together?

Many thanks.


----------



## TheOutcaste (Aug 8, 2007)

You've enabled Delayed Expansion, but aren't using it where it's needed (would eliminate the Call statements), and using it on one line where it's not needed.
And I don't see why you use a For loop to get the DNS address into a variable. Just set it at the start of the file and you can eliminate that loop. Makes it easier to change the DNS server, as you only have to edit one line instead of two.
This should do what you want. You can mix IP Addresses and Hostnames in the same file.
It will determine the IP address by pinging the hostname.
Give it a whirl.

```
@Echo Off
SetLocal EnableDelayedExpansion
Set InputFile=ips.txt
Set OutputFile=PNS-Results.csv
Set _DNSSVR=10.100.65.19
IF NOT Exist "%InputFile%" Echo "%InputFile%" file does not exist. &Goto EndScript
For %%R In ("%InputFile%") Do If %%~zR EQU 0 Echo "%InputFile%" file is empty. &Goto EndScript
If Exist "%OutputFile%" Del /F /Q "%OutputFile%"
Ping %_DNSSVR% -n 2 | Find "TTL" >Nul 2>&1
If ERRORLEVEL 1 (Echo "Unable to reach LONADS04 (%_DNSSVR%). Edit script to use an alternative DNS server" &GOTO EndScript)
Echo Hostname,IP Address,IP-Status,FQDN>>%OutputFile%
For /F %%c In ('TYPE "%InputFile%"') Do (
  Echo Processing: %%c
  Set _Hostname=Not Applicable
  Set _IP=%%c
  Set _Status=Offline
  :: Check if %%c is an IP Address. If not, set _Hostname=%%c
  ECHO.%%c|FindStr /R /B /C:"[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" >Nul
  If ERRORLEVEL 1 (Set _Hostname=%%c)&(Set _IP=Unknown)
  ::Ping the hostname/IP, check if online and get IP address if a hostname
  For /F "Tokens=1-3 Delims= " %%I In ('Ping "%%c" -n 2 -w 100') Do If /I "%%I %%J"=="Reply from" (Set _IP=%%K)&(Set _Status=Online)
  If Not "!_IP!"=="Unknown" (
    NSLookup.exe %%c %_DNSSVR% 2>Nul|Find /I "Name:" >Nul
    If NOT ERRORLEVEL 1 (
      For /F "tokens=1* Skip=2" %%k In ('NSLookup.exe %%c %_DNSSVR% 2^>Nul') Do If [%%k]==[Name:] Set _Name=%%l
      Echo !_Hostname!,!_IP!,!_Status!,!_Name!>>"%OutputFile%"
    ) Else (Echo !_Hostname!,!_IP!,!_Status!,Unable to resolve>>"%OutputFile%")
  ) Else (Echo !_Hostname!,!_IP!,!_Status!,Unable to resolve>>"%OutputFile%"))
:EndScript
EndLocal
EXIT /B 0
```
HTH

Jerry


----------



## essdeekay (Oct 21, 2009)

Jerry, that's fantastic - thank you very very much.

Quick query however: What would be required in order for the IP addresses in the CSV output to not have the colon that follows? As at the moment is it copying the entire contents between 'Reply from' and 'bytes=32', i.e. 192.168.100.1:

Thank you for taking the time out to assist with this, your help has been great.


----------



## TheOutcaste (Aug 8, 2007)

Sorry about that, I actually didn't even notice that colon was there. Guess I need a bigger font for my poor old eyes.
Just need to add the colon as a delimiter in line 22, the one right below the *:ing the hostname/IP* comment, so that line will start like this:
*For /F "Tokens=1-3 Delims=: "*

Jerry


----------



## LinuxHacker (Jan 1, 1970)

download IPSCAN, it's free.


----------

