Computing.Net > Forums > Programming > Batch - Ping Ip Address scan

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch - Ping Ip Address scan

Reply to Message Icon

Name: bignold
Date: December 4, 2008 at 18:18:20 Pacific
OS: Vista Ultimate
CPU/Ram: 8g
Product: Custom / CUSTOM
Comment:

Hey again,

Im working on a series of batch programs to help me this misc tasks around the network I work at, on feature i would like to include is to be able to ping multiple hosts without user interaction and save the results to a txt file. The only part i am having difficulty with is inputing a range of ip addresses. ie, 10.129.28.0 to 10.129.28.255.

This is part of a script i have already written.


echo.
echo Please specify target. Type 17331 for client PC.
set /p t=Target:
echo.
ping -n 1 -l 1 %t% > nul
if %errorlevel%==0 echo Host %t% is UP! >> %t%.txt
if %errorlevel%==1 echo Host %t% is DOWN! >> %t%.txt
set /p a=Continue? (Y/n):
if "%a%"=="n" exit

Please feel free to post any questions concerning my dilemma and I will answer as soon as possible. Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: BatchFreak
Date: December 4, 2008 at 19:41:19 Pacific
Reply:

you would need to have it slowly going upwards, how many Ip's are you using? And what is the range?

You could use somethin like


@echo off
SET t=0
:start
SET /a t=t+1
ping -n 1 -l 1 127.0.0.%t% > nul
if %errorlevel%==0 echo Host %t% is UP! >> 127.0.0.%t%.txt
if %errorlevel%==1 echo Host %t% is DOWN! >> 127.0.0.%t%.txt
IF %t%==999 Exit
Goto start


^^^This would scan from 127.0.0.1-127.0.0.999


You could make it scan any way you want by using increments of increasing variables. So if you wanted to scan every ip addess in this range 0.0.0.0 - 999.999.999.999


@echo off
Set a=0
set b=0
set c=0
set d=0

ping -n 1 -l 1 %a%.%b%.%c%.%d% > nul
if %errorlevel%==0 echo Host %a%.%b%.%c%.%d% is UP! >> %a%.%b%.%c%.%d%.txt
if %errorlevel%==1 echo Host %a%.%b%.%c%.%d% is DOWN! >> %a%.%b%.%c%.%d%.txt


:start
SET /a d=d+1

IF %d%==1000 (
SET /a c=c+1
set d=0
)
IF %c%==1000 (
SET /a b=b+1
set c=0
)
IF %b%==1000 (
SET /a a=a+1
set b=0
)
IF %a%==1000 EXIT

ping -n 1 -l 1 %a%.%b%.%c%.%D% > nul
if %errorlevel%==0 echo Host %t% is UP! >> %a%.%b%.%c%.%d%.txt
if %errorlevel%==1 echo Host %t% is DOWN! >> %a%.%b%.%c%.%d%.txt
Goto start

This would take about.... a looong time....
Better specify a range :)

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 2
Name: bignold
Date: December 4, 2008 at 20:23:35 Pacific
Reply:

thanks for this, i will spend some time implementing it into my script and get back to you. thanks again


0

Response Number 3
Name: Judago
Date: December 4, 2008 at 23:09:25 Pacific
Reply:

Batchfreak,

Ipv4 addresses only range form, 0.0.0.0 to 255.255.255.255 and if you wanted to ping them all it would be more efficient to use nested for /l loops(in nt+).


@ECHO OFF
for /l %%a in (0,1,255) do (
for /l %%b in (0,1,255) do (
for /l %%c in (0,1,255) do (
for /l %%d in (0,1,255) do (
ping %%a.%%b.%%c.%%d>nul
if errorlevel 1 (echo Host is down.) else echo Host is up.

)
)
)
)

The actual "work" the script is doing would be faster but the pings would take the same amount of time. The reason for this is the goto jumps cause the command processor to search the batch file for the :label.

Unfortunately using for /l loops to scan ranges is a little more difficult, I may look into it if I have time.


0

Response Number 4
Name: klint
Date: December 5, 2008 at 01:51:14 Pacific
Reply:

I agree with Judago. Also, to go through all possible addresses would take 2^32 (about 4 billion) iterations, which would take ages. You probably only need to go through the last one or two loops, depending on your subnet. Also, you probably only want to check from 1 to 254. (255 is used for multicasting for example.)


0

Response Number 5
Name: FishMonger
Date: December 5, 2008 at 06:31:30 Pacific
Reply:

A better option would be to use a tool that is designed for this purpose.

Nmap ("Network Mapper") is a free and open source (license) utility for network exploration or security auditing.

nmap.org

This will do a ping sweep of your network and save it in 3 separate formats; normal, xml, and grepable.

nmap -sP -oA filename 10.129.28.0/24

You can also specify a specific range of IP's instead of the CIDR mask.

nmap -sP -oA filename 10.129.28.50-150


-oN/-oX/-oS/-oG <file>: Output scan in normal, XML, s|<rIpt kIddi3,
and Grepable format, respectively, to the given filename.
-oA <basename>: Output in the three major formats at once


0

Related Posts

See More



Response Number 6
Name: bignold
Date: December 5, 2008 at 08:04:14 Pacific
Reply:

thanks for the script Fishmonger. I have been playing around with it and i am still yet to make it in a set range but i am confident that i will. Another question, is it possible to change the interval of each ping, i know it is possible in unix usine ping -i "interval here", can this be done on Windows. I have been unable to find a solution through ping /?

thanks


0

Response Number 7
Name: bignold
Date: December 5, 2008 at 08:13:40 Pacific
Reply:

just noticed that there are more posts beneath fishmongers. I intend to use the batch for 255-510 address at a time so it shouldnt take to long. I currently use Nmap on both my vista and my unix machines but find it unstable with prolonged session use, besides i prefer to use programs i have written and know that they work and how they work. Important in a security measure.


0

Response Number 8
Name: bignold
Date: December 5, 2008 at 08:23:52 Pacific
Reply:

Again, finished going over Batchfreak's script and i would have to say that is yhe one best suited for me at this time, fixed just a few errors in your script though, it exports each ping to a different file instead of just the one file and the ip address limit i changed from 999,999.999.999 to 255.255.255.255.

Thanks, I think that this is the script i will implement into my current batch for now.


0

Response Number 9
Name: BatchFreak
Date: December 5, 2008 at 08:57:40 Pacific
Reply:

Ok, No problem, I haven't worked with ip adress but I knew that it never went over 3 digits. Also, my script was just a demonstration of how it would work, *notices my last sentence*

""This would take about.... a looong time....
Better specify a range :)""

I only Batch if possible, 2000 more lines of code, oh well.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch - Ping Ip Address scan

Batch Ping www.computing.net/answers/programming/batch-ping/18881.html

Extract IP address from txt file www.computing.net/answers/programming/extract-ip-address-from-txt-file/13912.html

Batch File - Finding the IP address www.computing.net/answers/programming/batch-file-finding-the-ip-address/13900.html