There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash driver drivers error ethernet excel freeze gaming google gpu graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard mouse network printer problem ram registry router server slow software sound svchost.exe trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Batch / VB request

Reply  
Thread Tools
essdeekay's Avatar
Computer Specs
Junior Member with 4 posts.
 
Join Date: Oct 2009
Experience: Intermediate
21-Oct-2009, 11:19 AM #1
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
essdeekay's Avatar
Computer Specs
Junior Member with 4 posts.
 
Join Date: Oct 2009
Experience: Intermediate
28-Oct-2009, 12:07 PM #2
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's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
30-Oct-2009, 10:39 PM #3
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 Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

essdeekay's Avatar
Computer Specs
Junior Member with 4 posts.
 
Join Date: Oct 2009
Experience: Intermediate
02-Nov-2009, 10:08 AM #4
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's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
02-Nov-2009, 01:06 PM #5
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 Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

LinuxHacker's Avatar
Member with 140 posts.
 
Join Date: Dec 1969
02-Nov-2009, 01:12 PM #6
download IPSCAN, it's free.
Reply

Tags
batch file, script

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




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.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:40 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.