Jump to content
Can't remember your login details? Read more... ×
Muinztar

I made an IP address scanner batch file

Recommended Posts

I have a router that I got from a Garage sale, and I couldn't remember what I'd set the custom IP address to. For some reason, instead of finding a scanning utility on the web, or using the one that I have (although, it doesn't seem to work anyway), I made my own from scratch. It was a fun little exercise in fiddling with Ye Olde DOS programming.

 

@echo off
echo Muinztar's IP Scanner
echo -----------------
echo This batch file scans the 192.168.0.x IP address range with a specified timeout
echo  for quickly scanning for responding stations on the given range.
echo The default is 80 ms)
echo It is useful for finding the unknown IP address of a router, for example.
echo.
echo Usage: IP_SCAN [timeout]
echo Where timeout is a number between 1 and 2000 miliseconds
echo.

rem CHECK INPUT
if "%1"=="" goto skip
rem ^ If the user inputs nothing (default) then skip further checking

IF %1 LSS 1 (
echo ERROR: Please use a number greater than 0 miliseconds
goto end
)

IF %1 GTR 2000 (
echo There is probably no need for a timeout greater than 2 seconds...
echo Please choose a smaller timeout value.
goto end
)



:skip
Rem echo (debug) input is ok


rem Declare variables
set timeout=0

if "%1"=="" (
echo Default timeout used [80 ms]
set timeout=80
echo.
)

:settimeout
if %timeout%==0 set timeout=%1


:default
echo Command structure: ping -n 1 -w %timeout% 192.168.0.x
echo.

for /l %%f in (1, 1, 254) do (
rem Announce the test cycle (debug)
rem echo Testing 192.168.0.%%f

rem Reset errorlevel variable before the test
set errorlevel=0

call errorchk ping -n 1 -w %timeout% 192.168.0.%%f  > nul

rem log entry
if not errorlevel=1 echo 192.168.0.%%f active

if %%f==25 echo 10 percent done
if %%f==55 echo 20 percent done
if %%f==75 echo 30 percent done
if %%f==100 echo 40 percent done
if %%f==125 echo 50 percent done
if %%f==150 echo 60 percent done
if %%f==175 echo 70 percent done
if %%f==200 echo 80 percent done
if %%f==225 echo 90 percent done
if %%f==254 echo Scan completed
)

:end

 

That was a good waste of an hour to build and debug... But it was fun.

 

 

 

 

ADDENDUM

The batch file calls another one I have called the errorlevel checker. It's a dirty workaround for DOS's strange handling of errorlevels (you have to check them downwards from 255 (max) because any compare of errorlevel seems to be an Equal or Greater operator. In this case it's used to capture the response from the PING command (errorlevel 1 is no reply; 0 is reply received).

 

@echo off
echo.
echo Muinztar's Error level finder - Mk II.
rem run program
call %1 %2 %3 %4 %5 %6 %7 %8 %9

for /l %%f in (255, -1, 0) do (
if errorlevel=%%f echo Errorlevel=%%f
)

Ed: Incorporated edmund's suggestion

Edited by Muinztar

Share this post


Link to post
Share on other sites

Perhaps you might like to use for /L to save on typing up all those numbers?

 

FOR /L %variable IN (start,step,end) DO command [command-parameters]
	The set is a sequence of numbers from start to end, by step amount.
	So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
	generate the sequence (5 4 3 2 1)

Or you could use nmap.

Share this post


Link to post
Share on other sites

Well, now there's a nice way to optimise. I wasn't aware of the sequence ability in a for loop. Is that a WinXP era change or has it been always available?

 

Also - yeah, I *could* use nmap, but it was fun making this. Oh, and for the record I did the numbers using excel's auto fill and made copypasta, so it wasn't much effort at all :)

Share this post


Link to post
Share on other sites

You could have in fact, wrote the whole thing in excel, using excel functions to generate the required numbers, then exported to .txt, the renamed to .bat! That would be bonus points :D

 

*dares you*

Share this post


Link to post
Share on other sites

Is that actually possible without excel exporting it and not making it so the logic won't be in sequence?

Share this post


Link to post
Share on other sites

The reason "IF" seems to work strangely when checking errorlevels in NT/2K/XP is that IF itself returns an errorlevel.

 

So, instead of

 

IF "%ERRORLEVEL%" == "1" THEN GOTO ERR1
IF "%ERRORLEVEL%" == "2" THEN GOTO ERR2
IF "%ERRORLEVEL%" == "3" THEN GOTO ERR3

You need to do:

 

SET ERRLEV=%%ERRORLEVEL%%
IF "ERRLEV" == "1" THEN GOTO ERR1
etc

The double percent signs are to indicate late resolution of the variable.

 

%VARIABLE% will be resolved during preprocessing.

%%VARIABLE%% will be resolved "just in time".

 

I don't specifically remember if the double-percent thing is necessary for ERRORLEVEL... I'd have to go find my notes.

Share this post


Link to post
Share on other sites

^ hmm

Maybe I should optimise my original batch with that. I think I've wasted enough time on it so far though. :)

 

Is that actually possible without excel exporting it and not making it so the logic won't be in sequence?

It is if you save it as text format (tab delimited)

 

Then drop it into word/wordpad/edit+ etc and do a find/replace sweep. Takes about 20 seconds.

Share this post


Link to post
Share on other sites

public static void main(String[] args) {		
		if(args.length==4)
			try {System.out.println(InetAddress.getByName(args[0]+"."+args[1]+"."+args[2]+"."+args[3]).getHostName());}
			catch(Exception e){}
		else 
			for(String[] i=new String[]{"0"},a=new String[args.length+1];i[0]!="255" && ((a[args.length]=i[0])==i[0]);i[0]=new Integer(Integer.parseInt(i[0])+1).toString())
				for(int j = 0;j<args.length;a[j] = args[j++])
					if(j==args.length-1)main(a);	
		
	}

recursive main functions ftw

 

i hate java

Edited by cobwebs in the sky

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×