如何ping并在子网中导出set ip范围的结果

时间:2016-03-23 21:21:17

标签: batch-file ping printers

我正在尝试修改我在网上找到的Bat文件以满足我的需求。

 function decryptData(data, password, iv) {
    var data = CryptoJS.enc.Base64.parse(data);
    var key = CryptoJS.SHA1(password).toString(CryptoJS.enc.Hex).substring(0, 32);
    key = CryptoJS.enc.Base64.parse(btoa(key));

    var iv  = CryptoJS.enc.Base64.parse(iv);
    var decrypted = CryptoJS.AES.decrypt({ciphertext: data}, key, {iv: iv});
    return decrypted.toString(CryptoJS.enc.Utf8);
}

手头的问题......我们在全国数百个办事处都有网络打印机。我们公司使用一般标准,差异有限。所有打印机都以15或16的第4个八位字节结束。变量将是IP的第2个和第3个八位字节。我可以为第二个八位字节变量制作第二个脚本,我的大障碍是第三个八位字节和输出。例如......

我需要ping,并在10.163.X.15或10.163.X.16 IP地址中识别来自任何计算机的响应,并将其导出到带有响应的txt文件。 X的范围是1-254。只需要一个ping响应,我不需要它响应4-5响应,因为它只是“你在那里”测试。然后我会为10.162.x.x和10.160.x.x

的网络编辑它

我对脚本编写不太熟悉,并希望了解更多有关如何实现此目的的信息。谢谢!

1 个答案:

答案 0 :(得分:0)

你几乎就在那里,你永远不会使用内部for /L循环的变量@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%

15

我将此变量用作第四个八位字节(而不是常量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 ),因此控制台输出为:

IPScanResults.txt

...日志文件Reply from 10.163.1.10: bytes=32 time<1ms TTL=128 Run Time = 0:05:07.24 to 0:05:17.16 的内容可能如下所示:

for /L

此外,我缩进%500%循环以提高易读性,并在日志文件名称周围加上引号,因此可能会更改它以便它甚至包含空格而没有任何问题。

此外,我通过%time%交换%500%,因为我认为您想存储开始时间。 %是无效的变量名称,因为%5后跟数字图(本例中为call /?)表示脚本参数展开 - 有关详细信息,请参阅160

<强>提示:
如果您没有其中一个八位字节的连续值,例如162163for,则可以使用标准for /L循环而不是{{1 }}:

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

无论如何,我很可能会因为它的灵活性而使用以下脚本完成你的任务:

@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