PLEASE bear in mind that I'm in the EARLY stages of learning Batch Scripting. I suspect it's not just a single bug in my script which is accountable for its strange behaviour. The test scenario is as follows. There is a file named netips.txt
, containing a list of IP addresses on my LAN, to be pinged e.g.
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.33
192.168.1.34
192.168.1.35
When the script (bottom) is executed, the desired result is as follows: The user is prompted for a cycle interval and whether to receive a text file ipalert.txt
, at the end of each cycle. Each IP address in netips.txt
is then pinged, a pop-up msgbox for each IP which is Online
ONLY should appear displaying the IP before disappearing after 3 seconds. A beep should accompany each pop-up. The whole process (cycle) should then be repeated according to the user interval choice.
What currently happens is (I'm not sure why), all IP's pop-up as Online
along with a
' ' is not recognized as an internal or external command, operable program or batch file.
in the CMD window. The ipalert.txt
log displays:
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
Not all IP's should appear this way (some should be Offline
). Can anyone please help me understand what is happening, and what I must do to correct this?
@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
UPDATE
I've altered the LOOP sub (see hereunder) with some improvement, the correct IP's now pop-up as Online
, however the ipalert.txt
now ONLY displays those IP's, and not the Offline
ones aswell...?
: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 (
for /f "tokens=*" %%a in ('ping -n 1 %%i ^| find "TTL="') do (
if errorlevel 0 (
echo %%i Online >> c:\ipalert.txt
echo %%i Online > c:\ipnotify.txt
BELL
msg "%username%" /time:3 < c:\ipnotify.txt
del /q c:\ipnotify.txt
)
if errorlevel 1 (
echo %%i Offline >> c:\ipalert.txt
)
)
)