 | Junior Member with 4 posts. | | Join Date: Oct 2009 Experience: Intermediate | | Batch / VB request 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 | | Junior Member with 4 posts. | | Join Date: Oct 2009 Experience: Intermediate | | 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. | | Distinguished Member with 5,490 posts. | | Join Date: Aug 2007 Location: Oregon, USA Experience: Intermediate | | 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. Code: @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
__________________ Microsoft MVP - Windows Desktop Experience
Of course I know all the answers  ; I just don't always match the answers to the right questions Are you aware of the New Signature Limitations? | | Junior Member with 4 posts. | | Join Date: Oct 2009 Experience: Intermediate | | 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. | | Distinguished Member with 5,490 posts. | | Join Date: Aug 2007 Location: Oregon, USA Experience: Intermediate | | 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 ::Ping the hostname/IP comment, so that line will start like this: For /F "Tokens=1-3 Delims=: "
Jerry
__________________ Microsoft MVP - Windows Desktop Experience
Of course I know all the answers  ; I just don't always match the answers to the right questions Are you aware of the New Signature Limitations? | | Senior Member with 111 posts. | | | | download IPSCAN, it's free. | |
Smart Search
| Find your solution! | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |  WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
| You Are Using: |
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 11:48 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd. | |
|