0

I am attempting to modify a Bat file that i found online for my needs.

@ECHO Off
set startTime=%500%
ECHO Starting the IP Scan
FOR /L %%i IN (1,1,2) DO @(
ECHO Pinging IP Range: 10.163.%%i._
FOR /L %%z IN (1,1,10) DO @(
echo Pinging IP: 10.163.%%i.15
ping -n 1 -w 500 10.163.%%i.15 | FIND /i "Reply">>IPScanResults.txt
)
)
ECHO Run Time = %startTime% to %time%>>IPScanResults.txt
ECHO *** IP Scan Complete. ***
ECHO Check the file called IPScanResults.txt to see the results
ECHO Run Time = %startTime% to %time%

The issue at hand... we have network printers in hundreds of offices nationwide. Our company uses a general standard with limited variances. All printers end in a 4th octet of 15, or 16. the variable will be the 2nd and 3rd octet of the IP. I can make a 2nd script for the 2nd octet variables, my big hurdle is the third octet and the output. For instance....

I need to ping, and identify a response from any machine within a 10.163.X.15 or 10.163.X.16 IP address and have it export to a txt file with its response. The range of X being 1-254. a single ping response is all that is needed, I do not need it to respond with 4-5 responses as its only a "are you there" test. I would then edit this for our networks with 10.162.x.x and 10.160.x.x

I am not very familiar with scripting and would love to learn more about how to accomplish this. Thanks!

Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
  • What is `set startTime=%500%` for? Never use variable names consisting of numeric figures only, because `%` followed by a digit has got another meaning -- type `call /?` for information... – aschipfl Mar 23 '16 at 22:39

1 Answers1

0

You are almost there, you just never use the variable %%z of the inner for /L loop:

@ECHO OFF
SET startTime=%time%
ECHO Starting the IP Scan
FOR /L %%i IN (1,1,2) DO (
    ECHO Pinging IP Range: 10.163.%%i._
    FOR /L %%z IN (1,1,10) DO (
        ECHO Pinging IP: 10.163.%%i.%%z
        PING -n 1 -w 500 10.163.%%i.%%z | FIND /i "Reply">>"IPScanResults.txt"
    )
)
ECHO Run Time = %startTime% to %time%>>"IPScanResults.txt"
ECHO *** IP Scan Complete. ***
ECHO Check the file called "IPScanResults.txt" to see the results
ECHO Run Time = %startTime% to %time%

I used this variable as the fourth octet (rather than the constant 15), so the console output is this:

Starting the IP Scan
Pinging IP Range: 10.163.1._
Pinging IP: 10.163.1.1
Pinging IP: 10.163.1.2
  ...
Pinging IP: 10.163.1.10
Pinging IP Range: 10.163.2._
Pinging IP: 10.163.2.1
Pinging IP: 10.163.2.2
  ...
Pinging IP: 10.163.2.10
*** IP Scan Complete. ***
Check the file called "IPScanResults.txt" to see the results
Run Time =  0:05:07.24 to  0:05:17.16

...and the content of the log file IPScanResults.txt might look like this:

Reply from 10.163.1.10: bytes=32 time<1ms TTL=128
Run Time =  0:05:07.24 to  0:05:17.16

In addition, I indented the for /L loops to improve legibility, and I put quotation marks around the log file name, so it might be changed so that it even contains white-spaces without any trouble.

Furthermore, I exchanged %500% by %time%, because I assumed you wanted to store the start time. %500% is an invalid variable name, because % followed by a numeric figure (%5 in this case) means script argument expansion -- see call /? for more information on that.


Hint:
If you do not have continuous values for one of the octets, for instance, 160, 162, 163, you can use a standard for loop instead of for /L:

FOR %%j IN (160 162 163) DO (
    PING -n 1 -w 500 10.%%j.1.15
)

Anyway, I would accomplish your task with the following script most probably due to its flexibility:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Define constants here:
set "LOGFILE=IPScanResults.txt"
rem Define IP octets here:
set "OCTET1=10"          & rem (a single value)
set "OCTET2=160 162 163" & rem (space-separated list of values)
set "OCTET3=1,1,254"     & rem (comma-separated range (min.,step,max.))
set "OCTET4=15,1,16"     & rem (comma-separated range (min.,step,max.))

rem Prepare `for` switch `/L` dynamically:
for /L %%O in (1,1,4) do (
    if not "!OCTET%%O!"=="!OCTET%%O:,=!" set "SWITCH%%O=/L"
)

set "STARTTIME=%TIME%"
if exist "%LOGFILE%" (>> "%LOGFILE%" echo()
echo Starting the IP Scan
for %SWITCH1% %%I in (%OCTET1%) do (
    for %SWITCH2% %%J in (%OCTET2%) do (
        echo Pinging IP Range: %%I.%%J.__.__
        for %SWITCH3% %%K in (%OCTET3%) do (
            for %SWITCH4% %%L in (%OCTET4%) do (
                echo Pinging IP: %%I.%%J.%%K.%%L
                ping -n 1 -w 500 %%I.%%J.%%K.%%L | >> "%LOGFILE%" find "TTL"
            )
        )
    )
)
>> "%LOGFILE%" echo Run Time = %STARTTIME% -- %TIME%
echo *** IP Scan Complete. ***
echo Check the file called "%LOGFILE%" to see the results.
echo Run Time = %STARTTIME% -- %TIME%

endlocal
exit /B
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • This is fantastic! Is there any way to automatically add a header for a CSV file? – Matt Hannis Mar 25 '16 at 13:41
  • Which "CSV file"? are you talking about the log file `IPScanResults.txt`? I guess you want to write a headline the first time it is written (so when it is created initially), but you do not want the headline if the file is appended to, right? if so, you could just extend the line `if exist "%LOGFILE%" (>> "%LOGFILE%" echo()`, which inserts an empty line (`echo(`) in case the file already exists; so just append _SPACE_ + `else echo(Header&echo(` to add a header with the text `Header` followed by an empty line, for instance... – aschipfl Mar 25 '16 at 23:30
  • Heres what I modified from your script already...but its not coming out like I expected... set "STARTTIME=%TIME%" if exist "%LOGFILE%" (>> "IPScanResults.txt" echo (header&echo() echo Starting the IP Scan for %SWITCH1% %%I in (%OCTET1%) do ( – Matt Hannis Mar 28 '16 at 14:48
  • Hard to tell what's wrong as in comments, code formatting is poor; perhaps the _space_ between `echo` and `(header` should be removed... – aschipfl Mar 28 '16 at 16:41
  • Yea sorry about formatting. getting use to the comment sections. ill try the format change suggested. Thanks so much. – Matt Hannis Mar 30 '16 at 13:53