Batch file 批处理ping网络IP';s在嵌套for循环迭代中返回错误结果
请记住,我正处于学习批处理脚本的早期阶段。我怀疑这不仅仅是我脚本中的一个bug导致了它的奇怪行为。测试场景如下所示。有一个名为Batch file 批处理ping网络IP';s在嵌套for循环迭代中返回错误结果,batch-file,if-statement,for-loop,popup,ping,Batch File,If Statement,For Loop,Popup,Ping,请记住,我正处于学习批处理脚本的早期阶段。我怀疑这不仅仅是我脚本中的一个bug导致了它的奇怪行为。测试场景如下所示。有一个名为netips.txt的文件,其中包含要ping的我的局域网上的IP地址列表,例如 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.33 192.168.1.34 192.168.1.35 执行脚本(底部)时,所需结果如下:提示用户输入周期间隔,以及在每个周期结束时是否接收文本文件ipalert.txt。然后pingnetips
netips.txt
的文件,其中包含要ping的我的局域网上的IP地址列表,例如
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.33
192.168.1.34
192.168.1.35
执行脚本(底部)时,所需结果如下:提示用户输入周期间隔,以及在每个周期结束时是否接收文本文件ipalert.txt
。然后pingnetips.txt
中的每个IP地址,每个IP的弹出msgbox将联机
仅显示该IP,然后在3秒钟后消失。每个弹出窗口都应伴随一声蜂鸣音。然后,应根据用户间隔选择重复整个过程(循环)
目前发生的情况是(我不知道为什么),所有的IP都会作为在线弹出
,并带有一个
无法将“”识别为内部或外部命令、可操作程序或批处理文件。
在CMD窗口中。ipalert.txt
日志显示:
15/03/2015 at 12:44:23.12
192.168.1.1 Online
192.168.1.2 Online
192.168.1.3 Online
192.168.33 Online
192.168.34 Online
192.168.35 Online
并非所有IP都应该以这种方式显示(有些IP应该是脱机的)。有谁能帮我理解发生了什么,我必须做些什么来纠正这一点
@echo off
setlocal enabledelayedexpansion
if exist c:\netips.txt goto START
echo.
echo Cannot find "C:\netips.txt"
echo.
echo Please create this file, containing a list of network IP addresses to monitor for activity
echo.
goto EOF
:START
echo.
echo [1] 5 mins
echo [2] 15 mins
echo [3] 30 mins
echo [4] 60 mins
echo.
set interval=0
choice /c 1234 /n /m "Please enter the activity notification rate:"
if errorlevel 1 set interval=300
if errorlevel 2 set interval=900
if errorlevel 3 set interval=1800
if errorlevel 4 set interval=3600
echo.
echo You will be notified every %interval% seconds
echo.
set log=0
choice /m "Would you like to automatically view the activity log after each cycle? "
if errorlevel 1 set log=Y
if errorlevel 2 set log=N
if "%log%"=="N" (
echo.
echo You can view the activity log [ipalert.txt] in the root directory
)
echo.
echo You can minimise this CMD window while it is running
echo.
echo Press any key to begin
pause>nul
:LOOP
cls
echo.
echo Executing...
echo. >>c:\ipalert.txt
echo %date% at %time% >>c:\ipalert.txt
echo. >>c:\ipalert.txt
set state=down
for /f %%i in (c:\netips.txt) do (
for /f "tokens=5,7" %%a in ('ping -n 1 %%i') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
if !state!==up (
echo %%i Online >> c:\ipalert.txt
echo %%i Online > c:\ipnotify.txt
BELL rem special character for 'beep'
msg "%username%" /time:3 < c:\ipnotify.txt
del /q c:\ipnotify.txt
) else (
echo %%i Offline >> c:\ipalert.txt
)
set state=down
)
if "%log%"=="Y" (
start notepad c:\ipalert.txt
timeout /t 6 >nul
taskkill /im notepad.exe>nul
)
timeout /t !interval! /nobreak >nul
goto LOOP
endlocal
您的脚本对我来说没有任何错误消息,但是这种错误可能是由于错误的转义特殊字符或错误的
()
括号数造成的;要定位错误位置,请强制启用ECHO
,暂时不要用cls
清除屏幕
但是,ping-n1…
返回Received=1,
即使目标主机无法访问:
我将对find
和强制IPV4
使用下一个测试:
set state=down
for /f "tokens=*" %%a in ('ping -n 1 -4 %%i^|find "TTL="') do (
set state=up
)
我不知道你的
msg
命令是什么。批处理文件中的以下命令将触发大多数PC上的默认蜂鸣声:ECHO
(^G)。要键入BELL
字符(ASCII 7),请使用CtrlG或按住Alt键键入07(数字键盘上的数字)。我不想挑剔您的努力,但使用vbscript或powershell执行ping类型操作要比批处理容易得多。读/写文件和注册表数据也一样。批处理没有什么问题,但它只是一种工具,尝试用一种语言做任何事情就像用扳手敲打钉子一样,因为扳手在螺栓上工作得很好。set“netIPs=c:\netIPs.txt”
设置“ipAlert=c:\ipAlert.txt”
:: ....
对于(0)do中的/l%%a(
cls
回音(
回音执行。。。
>>%ipAlert%(
延迟扩展
回音(
回声(!日期!时间!时间!
回音(
端部
)
(对于/f“usebackq”%%i in(“%netIPs%”)do(
ping-n 1“%%~i”| find“TTL=“>nul&&(
echo%%i在线
消息“%userName%”/时间:3“%%i联机”
) || (
echo%%i脱机
)
))>>%ipAlert%“
超时/t%间隔%/nobreak>nul
)
代码中的三个更改
goto
已被无限for
环路取代ping
操作的结果ping....
if errorlevel 1 (
rem failure
) else (
sucess
)
使用&&
(如果上一个命令未提高错误级别,则执行代码)和|
(如果上一个命令已提高错误级别,则执行代码)
请记住,“通常”errorlevel必须从较高的值到较低的值,因为如果errorlevel n
等于或高于n
,则表达式将被计算为true。谢谢您的(复数)帮助。我希望有一种方法可以将不止一个答案标记为答案。JosefZ和MC ND都解决了我脚本中的一些非常重要的错误,并且都很有启发性,最终有助于我开发一个工作版本。下面是我完成的脚本(它似乎对我起到了预期的作用,没有偏离它原来的结构)。再次感谢大家
@echo off
title Network Activity Notifier
color 0a
cls
echo.
echo ============================
echo Network Activity Notifier
echo ============================
setlocal enabledelayedexpansion
if exist c:\netips.txt goto START
echo.
echo Cannot find "C:\netips.txt"
echo.
echo Please create this file, containing a list of network IP addresses to monitor for activity
echo.
goto EOF
:START
echo.
echo [1] 5 mins
echo [2] 15 mins
echo [3] 30 mins
echo [4] 60 mins
echo.
set interval=0
choice /c 1234 /n /m "Please enter the activity notification rate:"
if errorlevel 1 set interval=300
if errorlevel 2 set interval=900
if errorlevel 3 set interval=1800
if errorlevel 4 set interval=3600
echo.
echo You will be notified every %interval% seconds
echo.
set log=0
choice /m "Would you like to automatically view the activity log after each cycle? "
if errorlevel 1 set log=Y
if errorlevel 2 set log=N
if "%log%"=="N" (
echo.
echo You can view the activity log [ipalert.txt] in the root directory
)
echo.
echo You can minimise this CMD window while it is running
echo.
echo Press any key to begin
pause>nul
:LOOP
cls
echo.
echo Executing...
echo. >>c:\ipalert.txt
echo %date% at %time% >>c:\ipalert.txt
echo. >>c:\ipalert.txt
for /f %%i in (c:\netips.txt) do (
ping -n 1 %%i | find "TTL=" >nul
if errorlevel 1 (
echo %%i Offline >> c:\ipalert.txt
) else (
echo %%i Online >> c:\ipalert.txt
echo %%i Online > c:\ipnotify.txt
echo
msg "%username%" /time:3 < c:\ipnotify.txt
del /q c:\ipnotify.txt
)
)
if "%log%"=="Y" (
start notepad c:\ipalert.txt
timeout /t 6 >nul
taskkill /im notepad.exe>nul
)
timeout /t !interval! /nobreak >nul
goto LOOP
endlocal
@echo关闭
标题网络活动通知程序
颜色0a
cls
回声。
回音============================
回送网络活动通知程序
回音============================
延迟扩展
如果存在c:\netips.txt转到开始
回声。
echo找不到“C:\netips.txt”
回声。
echo请创建此文件,其中包含要监视活动的网络IP地址列表
回声。
转到EOF
:开始
回声。
回声[1]5分钟
回声[2]15分钟
回声[3]30分钟
回声[4]60分钟
回声。
设置间隔=0
选择/c 1234/n/m“请输入活动通知率:”
如果错误级别1设置间隔=300
如果错误级别2,则设置间隔=900
如果错误级别3,则设置间隔=1800
如果错误级别4,则设置间隔=3600
回声。
echo每隔%间隔%秒将通知您一次
回声。
设置日志=0
选择/m“是否希望在每个周期后自动查看活动日志?”
如果errorlevel 1设置日志=Y
如果errorlevel 2设置日志=N
如果“%log%”=N(
回声。
echo您可以在根目录中查看活动日志[ipalert.txt]
)
回声。
echo您可以在运行时最小化此CMD窗口
回声。
按任意键开始
暂停>nul
:循环
cls
回声。
回音执行。。。
echo.>>c:\ipalert.txt
回显%date%在%time%>>c:\ipalert.txt
echo.>>c:\ipalert.txt
对于(c:\netips.txt)中的/f%%i,请执行以下操作(
ping-n 1%%i |查找“TTL=“>nul
如果错误级别为1(
echo%%i脱机>>c:\ipalert.txt
)否则(
echo%%i Online>>c:\ipalert.txt
echo%%i Online>c:\ipnotify.txt
回音
消息“%username%”/time:3nul
taskkill/im notepad.exe>n
ping....
if errorlevel 1 (
rem failure
) else (
sucess
)
@echo off
title Network Activity Notifier
color 0a
cls
echo.
echo ============================
echo Network Activity Notifier
echo ============================
setlocal enabledelayedexpansion
if exist c:\netips.txt goto START
echo.
echo Cannot find "C:\netips.txt"
echo.
echo Please create this file, containing a list of network IP addresses to monitor for activity
echo.
goto EOF
:START
echo.
echo [1] 5 mins
echo [2] 15 mins
echo [3] 30 mins
echo [4] 60 mins
echo.
set interval=0
choice /c 1234 /n /m "Please enter the activity notification rate:"
if errorlevel 1 set interval=300
if errorlevel 2 set interval=900
if errorlevel 3 set interval=1800
if errorlevel 4 set interval=3600
echo.
echo You will be notified every %interval% seconds
echo.
set log=0
choice /m "Would you like to automatically view the activity log after each cycle? "
if errorlevel 1 set log=Y
if errorlevel 2 set log=N
if "%log%"=="N" (
echo.
echo You can view the activity log [ipalert.txt] in the root directory
)
echo.
echo You can minimise this CMD window while it is running
echo.
echo Press any key to begin
pause>nul
:LOOP
cls
echo.
echo Executing...
echo. >>c:\ipalert.txt
echo %date% at %time% >>c:\ipalert.txt
echo. >>c:\ipalert.txt
for /f %%i in (c:\netips.txt) do (
ping -n 1 %%i | find "TTL=" >nul
if errorlevel 1 (
echo %%i Offline >> c:\ipalert.txt
) else (
echo %%i Online >> c:\ipalert.txt
echo %%i Online > c:\ipnotify.txt
echo
msg "%username%" /time:3 < c:\ipnotify.txt
del /q c:\ipnotify.txt
)
)
if "%log%"=="Y" (
start notepad c:\ipalert.txt
timeout /t 6 >nul
taskkill /im notepad.exe>nul
)
timeout /t !interval! /nobreak >nul
goto LOOP
endlocal