batch file help

T

tim

i wrote a batch file that scans ip addresses and makes a
log of all the active ip addresses. which would look
something like this
__________________________________________________________

starting ip = 127.0.0.0
ending ip = 127.0.0.10

active ip addresses:

127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4


my question is how can i make a statment in this text
file that says 5 ip addresses out of the 10 scanned are
active?
 
M

Matthias Tacke

tim said:
i wrote a batch file that scans ip addresses and makes a
log of all the active ip addresses. which would look
something like this
__________________________________________________________

starting ip = 127.0.0.0
ending ip = 127.0.0.10

active ip addresses:

127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4


my question is how can i make a statment in this text
file that says 5 ip addresses out of the 10 scanned are
active?

Just to be correct, that are 5 out of 11. I suggest to include counters
which increases in case of all tries resp, successful ?ping?.

set /A try+=1
set /A success+=1

To be more specific you should post the relevant part.

HTH
 
J

Jerold Schulman

i wrote a batch file that scans ip addresses and makes a
log of all the active ip addresses. which would look
something like this
__________________________________________________________

starting ip = 127.0.0.0
ending ip = 127.0.0.10

active ip addresses:

127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4


my question is how can i make a statment in this text
file that says 5 ip addresses out of the 10 scanned are
active?


@echo off
setlocal ENABLEDELAYEDEXPANSION
set IPS=127.0.0.
set fnd=findstr /L /I /C:"Reply from"
set /a cnt=0
for /L %%I in (0,1,10) do (
for /f "Tokens=*" %%a in ('ping -n 1 %IPS%%%I^|%fnd%') do (
set /a cnt=!cnt! + 1
)
)
@echo Active address is %cnt%
endlocal

Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
T

tim

-----Original Message-----


Just to be correct, that are 5 out of 11. I suggest to include counters
which increases in case of all tries resp, successful ? ping?.

set /A try+=1
set /A success+=1

To be more specific you should post the relevant part.

HTH
--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
.
here is the batch file

@echo off
color 1e
title IP SCANNER
cls
cd\
c:
cd %userprofile%\desktop
cls
goto start



:error3
cls
echo.
echo.
echo.
echo INVALID ENTRY. . . . . PLEASE TRY AGAIN . . . . .
echo.
echo.
echo.
echo ***** NONE OF THE OCTETS CAN BE LEFT BLANK *****
goto start
:error2
cls
echo.
echo.
echo.
echo INVALID ENTRY. . . . . PLEASE TRY AGAIN . . . . .
echo.
echo.
echo.
echo ***** THE STARTING IP ADDRESS CAN NOT GREATER THAN
THE ENDING IP ADDRESS *****
goto start
:error
cls
echo.
echo.
echo.
echo INVALID ENTRY. . . . . PLEASE TRY AGAIN . . . . .
echo.
echo.
echo.
echo ***** ALL OF THE OCTETS HAVE TO BE NUMBERS BETWEEN 1
AND 255 *****
:start
echo.
echo.
echo.
echo ENTER THE STARTING IP ADDRESS
echo.
echo.
echo.
set /p s1=ENTER THE 1ST OCTET:
echo.
set /p s2=ENTER THE 2ND OCTET:
echo.
set /p s3=ENTER THE 3RD OCTET:
echo.
SET /P S4=ENTER THE 4TH OCTET:
echo.
if "%s1%" == "" goto error3
if "%s2%" == "" goto error3
if "%s3%" == "" goto error3
if "%s4%" == "" goto error3
if %s1% GTR 255 goto error
if %s2% GTR 255 goto error
if %s3% GTR 255 goto error
if %s4% GTR 255 goto error
echo.
echo.
echo.
echo %s1%.%s2%.%s3%.%s4%
echo.
echo.
echo.
set /p answer=IS THIS IP ADDRESS CORRECT? %s1%.%s2%.%s3%.%
s4% (Y/N):
if /I %answer% EQU n cls && goto start
if /I %answer% NEQ y cls && goto start
if /I %answer% EQU y cls && goto start1
cls



:error4
cls
echo.
echo.
echo.
echo INVALID ENTRY. . . . . PLEASE TRY AGAIN . . . . .
echo.
echo.
echo.
echo ***** NONE OF THE OCTETS CAN BE LEFT BLANK *****
goto start1
:error1
cls
echo.
echo.
echo.
echo + + + INVALID ENTRY. . . PLEASE TRY AGAIN. . . + + +
echo.
echo.
echo.
echo ***** ALL OF THE OCTETS HAVE TO BE NUMBERS BETWEEN 1
AND 255 *****
:start1
echo.
echo.
echo.
echo ENTER THE ENDING IP ADDRESS
echo.
echo.
echo.
set /p e1=ENTER THE 1ST OCTET:
echo.
set /p e2=ENTER THE 2ND OCTET:
echo.
set /p e3=ENTER THE 3RD OCTET:
echo.
SET /P e4=ENTER THE 4TH OCTET:
echo.
if "%e1%" == "" goto error4
if "%e2%" == "" goto error4
if "%e3%" == "" goto error4
if "%e4%" == "" goto error4
if %e1% GTR 255 goto error1
if %e2% GTR 255 goto error1
if %e3% GTR 255 goto error1
if %e4% GTR 255 goto error1
if %s1% GTR %e1% goto error2
if %s2% GTR %e2% goto error2
if %s3% GTR %e3% goto error2
if %s4% GTR %e4% goto error2
echo.
echo.
echo.
echo %e1%.%e2%.%e3%.%e4%
echo.
echo.
echo.
set /p answer1=IS THIS IP ADDRESS CORRECT? %e1%.%e2%.%
e3%.%e4% (Y/N):
if /I %answer1% EQU n cls && goto start1
if /I %answer1% NEQ y cls && goto start1
if /I %answer1% EQU y cls && goto scan



:scan
cls
echo. >>ip_scan_results.txt
echo IP ADDRESS SCAN WAS DONE ON %DATE% AT %TIME% BY %
USERNAME% >>ip_scan_results.txt
echo. >>ip_scan_results.txt
echo STARTING IP ADDRESS: %s1%.%s2%.%s3%.%s4%echo. >>ip_scan_results.txt
echo ENDING IP ADDRESS: %e1%.%e2%.%e3%.%e4%echo. >>ip_scan_results.txt
echo. >>ip_scan_results.txt
echo + + + THE FOLLOWING IP ADDRESSES ARE ACTIVE + + +echo. >>ip_scan_results.txt
for /L %%x in (1,1,%e4%) do ping -n 1 -w 50 -i 6 %s1%.%
s2%.%s3%.%%x | find "Reply" && echo %s1%.%s2%.%s3%.%%xstart /max ip_scan_results.txt
cls
exit
 
C

Clay Calvert

On Tue, 10 Aug 2004 15:58:46 -0700, "tim"

Using "Reply" can yield false positives in PING replies.

For example, you ping 192.168.5.200 and the answer could be:

Reply From: 10.1.2.1: Destination Host Unreachable.

" TTL=" should be in all positive responses, and it may even be
language independant.
for /L %%x in (1,1,%e4%) do ping -n 1 -w 50 -i 6 %s1%.%
s2%.%s3%.%%x | find "Reply" && echo %s1%.%s2%.%s3%.%%x

Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
M

Matthias Tacke

<long batch snipped>

Hi Tim, you put much work in that batch using only some of the cmd
specific new commands.

I'd use only one input and split input at the dots.
You're asking the user to input the whole ending address while using
only the last octet.
You can remove some redundancy by using "call :internalsub" for error
messages and input.
Since IP addresses are 32bit integers it's not that difficult to convert
the octets to a single number and back again with "set /A"
So you can have a loop with a greater range than one octet.

You can have multiple commands in a for loop by enclosing them with
parenthes.
For better readability and changeability I'd use a variable for the log
file name and prepend the redirection instead appending. I.e.

set log=ip_scan_results.txt....
start /max %log%

I'll experiment a bit with ip-math and post later :)

HTH
 
M

Mark V

In said:
@echo off
color 1e
title IP SCANNER
cls
cd\
c:
cd %userprofile%\desktop

Side note: 3 lines above could better be just
CD /D "%userprofile\desktop"

[ ]
 
M

Michael Bednarek

[snip][173 lines snipped]

A more concise solution using 4NT:

=v=== snip here ===v=
:: Get input; two command line parameters?
IFF "%1" NE "" .AND. "%2" NE "" THEN
SET ip1=%1
SET ip2=%2
ELSE
INPUT From IP: %%ip1
INPUT To IP: %%ip2
ENDIFF

:: Functions to convert between dotted decimal and decimal
FUNCTION IP2DEC=`%@EVAL[%@WORD[".",0,%1] * 2 ** 24 + %@WORD[".",1,%1] * 2 ** 16 + %@WORD[".",2,%1] * 2 ** 8 + %@WORD[".",3,%1]]`
FUNCTION DEC2IP=`%@EVAL[%1 \ 2 ** 24].%@EVAL[%1 %% 2 ** 24 \ 2 ** 16].%@EVAL[%1 %% 2 ** 16 \ 2 ** 8].%@EVAL[%1 %% 2 ** 8]`

SET p1=%@IP2DEC[%ip1]
SET p2=%@IP2DEC[%ip2]

SET bon=0
:: 4NT's DO-loop variable is limited to 2^31-1, so we can't use it for decimal IP addresses.
SET try=%@EVAL[%p2 - %p1]
DO i = 0 TO %try
SET ip=%@EVAL[%p1 + %i]
IFF %@PING[%@EVAL[%ip],1] GE 0 THEN
ECHO Got response from %@DEC2IP[%ip]
SET /A bon=bon+1
ELSE
ECHO No response from %@DEC2IP[%ip]
ENDIFF
ENDDO

ECHO Tried: %@EVAL[%try + 1], %bon responded.
=^=== snip here ===^=

4NT's @ping and other functions are documented at
<http://jpsoft.com/help/functioncats.htm>.

Screenscrape:
$ pping.btm 10.0.1.214 10.0.1.220
No response from 10.0.1.214
Got response from 10.0.1.215
Got response from 10.0.1.216
No response from 10.0.1.217
Got response from 10.0.1.218
Got response from 10.0.1.219
No response from 10.0.1.220
Tried: 7, 4 responded.

and:
$ pping 216.239.37.92 216.239.37.104
Got response from 216.239.37.92
No response from 216.239.37.93
No response from 216.239.37.94
No response from 216.239.37.95
No response from 216.239.37.96
No response from 216.239.37.97
No response from 216.239.37.98
Got response from 216.239.37.99
No response from 216.239.37.100
No response from 216.239.37.101
No response from 216.239.37.102
No response from 216.239.37.103
Got response from 216.239.37.104
Tried: 13, 3 responded.
 
M

Matthias Tacke

Michael said:
[snip][173 lines snipped]

A more concise solution using 4NT:
Hello Michael,
I had the same idea converting the ip number to an integer and
experienced the same problems with the cmd.exe for using a signed 32bit
integer for math. Since I prefer free solutions I used conset from Frank
P. Westlake which uses 64bit integers.
Took me some time to get rid of strange behaviour while using delayed
expansion, after disabling all was fine in w2k.

My batch output resembles more the op's one. There are functions inside
conset which look like your 4nt code, I still have to dive in there.

FWIW Here is my batch requiring conset http://gearbox.maem.umr.edu/fwu/

<snip>
::MyIPScanner.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal&cls
set log="%USERPROFILE%\Desktop\ip_scan_results.txt"
echo.=========================== MyIP scanner =======================
:S_IP Start IP input
set "MiS="
set /P MiS=Enter dotted start IP address i.e. 192.168.1.1 :
if {%MiS%}=={} echo bye, bye&exit /B
:: The following line will call the sub with input-dots replaced
:: by spaces giving %1..%4 in the sub with the var name appended (%5)
Call :CheckIP %MiS:.= % MiS||(echo %MiS% isn't a valid IP&goto :S_IP)
:E_IP End IP Input
set /P MiE=Enter dotted end IP address i.e. 192.168.1.9 :
Call :CheckIP %MiE:.= % MiE||(echo %MiE% isn't a valid IP&goto :E_IP)
:: Calculate the range by subtracting start IP int from end IP int
:: Due to 32bit signed int use of set we need conset (64bit integers)
conset /A "MiRange=MiEI-MiSI"
If %MiRange% LSS 0 (echo.Start greater End IP!&goto :S_IP)
if %MiRange% GTR 1000 (echo Range greater than 1000&goto :S_IP)
Call :LogHeader
::set Mi&pause Just for debugging purposes
set /A MiPing=0,MiLost=0,MiIntermittent=0
for /L %%A in (0 1 %MiRange%) do call :MyPing %MiSI% %%A
:: Output summary to log filestart "" /MAX %log%
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:CheckIP 4 octets (by val) and MiS/MiE (by ref)
if "%~5" EQU "" echo Not 4 octets! &exit /B 1
if %5 NEQ MiS if %5 NEQ MiE echo Not 4 octets! &exit /B 1
:: Get the input value to MiC (make val from ref) & reset integer
setlocal&call set "MiC=%%%5%%"&set /A MiCI=0
:: Build 32bit integer by shiffting Octets to the proper place.
:: reversed byte order has to be taken into account
for /L %%A in (1 1 4) do (
call conset /AX "MiCI+=(%%%%A &255)<<(8*(4-%%A))")
::Calc IP octets from 32bit Int
for /L %%A in (1 1 4) do (
call conset /A "MiC%%A=(MiCI&255<<(8*(4-%%A)))>>(8*(4-%%A))")
:: Compare numbers with input - if equal IP is well formed
if "%MiC%" NEQ "%MiC1%.%MiC2%.%MiC3%.%MiC4%" exit /B 1
endlocal&set "%5I=%MiCI%"&exit /B 0
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:MyPing offset cnt (by val)
:: Decode 32bit integer to 4 octets
for /L %%A in (1 1 4) do (
conset /A "O%%A=((%1+%2)&(255<<8*(4-%%A)))>>8*(4-%%A)")
set IP=%O1%.%O2%.%O3%.%O4%
echo Checking %IP%
for /f "tokens=3,5,7,8 delims==, " %%A in (
'ping -n 1 -w 50 -n 6 %IP%^|find "("') do (
IF %%A EQU %%B (set /A MiPing+=1
echo %IP% ok>>%log%
) else (if %%A EQU %%C (set /A MiLost+=1
) else (set /A MiIntermittent+=1
echo %IP% lost %%D>>%log%
) ) )
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:LogHeadergoto :eof
::MyIPScanner.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top