-
Notifications
You must be signed in to change notification settings - Fork 79
/
pingup.bat
98 lines (79 loc) · 3.33 KB
/
pingup.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
:: Purpose: Simple script to check if a host is online and turn the window green (up) or red (down) based on the result. Also logs the result to a file
:: Requirements: Windows XP and up
:: Author: reddit.com/user/vocatus ( vocatus.gate at gmail ) // PGP key: 0x07d1490f82a211a2
:: Version: 1.2.1 ! Fix date not correctly updating at start of each check
:: 1.2.0 * Rework CUR_DATE to use function we call instead of static conversion
:: 1.1.0 * Reworked CUR_DATE variable to handle more than one Date/Time format
:: Can now handle all Windows date formats
:: 1.0.0 Initial write
::::::::::
:: Prep :: -- Don't change anything in this section
::::::::::
@echo off
set SCRIPT_VERSION=1.2.1
set SCRIPT_UPDATED=2020-07-16
cls
call :set_cur_date
:::::::::::::::
:: VARIABLES :: -- Set these to your desired values
:::::::::::::::
:: Set host to check here
::set HOST=72.201.99.26
set HOST=8.8.4.4
set DISPLAY_NAME=GoogleDNS
set LOGPATH=%SystemDrive%\logs
set LOGFILE=pingup_%DISPLAY_NAME%.log
set PINGS_PER_CHECK=3
set RECHECK_COOLDOWN_DELAY=30
:: make the log directory if it doesn't exist
if not exist "%LOGPATH%" mkdir "%LOGPATH%" >nul
:::::::::::::
:: EXECUTE ::
:::::::::::::
echo %CUR_DATE% %TIME% Initializng PINGUP monitoring script
echo Executing as %USERDOMAIN%\%USERNAME% on '%COMPUTERNAME%'
echo Monitoring: %HOST% ^(%DISPLAY_NAME%^)
echo Logging to: %LOGPATH%\%LOGFILE%
echo Pings per check: %PINGS_PER_CHECK%
echo Recheck cooldown: %RECHECK_COOLDOWN_DELAY%
:: This block creates the log entries
echo %CUR_DATE% %TIME% Initializng PINGUP monitoring script >> "%LOGPATH%\%LOGFILE%"
echo Executing as %USERDOMAIN%\%USERNAME% on '%COMPUTERNAME%' >> "%LOGPATH%\%LOGFILE%"
echo Monitoring: %HOST% (%DISPLAY_NAME%) >> "%LOGPATH%\%LOGFILE%"
echo Logging to: %LOGPATH%\%LOGFILE% >> "%LOGPATH%\%LOGFILE%"
echo Pings per check: %PINGS_PER_CHECK% >> "%LOGPATH%\%LOGFILE%"
echo Recheck cooldown: %RECHECK_COOLDOWN_DELAY% >> "%LOGPATH%\%LOGFILE%"
echo.
echo %CUR_DATE% %TIME% Performing initial test...
echo %CUR_DATE% %TIME% Performing initial test... >> "%LOGPATH%\%LOGFILE%"
echo.
:start
call :set_cur_date
ping %HOST% -n %PINGS_PER_CHECK% | find /i "TTL" > nul
:::::::::::::
:: HOST UP ::
:::::::::::::
:: Host is UP: Black text on green background
if %ERRORLEVEL%==0 (
title UP: %HOST%
color a0
echo %CUR_DATE% %TIME% %DISPLAY_NAME% ^(%HOST%^) up.
echo %CUR_DATE% %TIME% %DISPLAY_NAME% ^(%HOST%^) up. >> "%LOGPATH%\%LOGFILE%"
) ELSE (
REM Host is DOWN: Black text on red background
title DWN: %HOST%
color c0
echo %CUR_DATE% %TIME% ! %DISPLAY_NAME% ^(%HOST%^) down.
echo %CUR_DATE% %TIME% ! %DISPLAY_NAME% ^(%HOST%^) down. >> "%LOGPATH%\%LOGFILE%"
)
:: Cooldown until next check
ping localhost -n %RECHECK_COOLDOWN_DELAY% >NUL
goto start
:::::::::::::::
:: FUNCTIONS ::
:::::::::::::::
:: Get the date into ISO 8601 standard format (yyyy-mm-dd) so we can use it
:set_cur_date
for /f %%a in ('WMIC OS GET LocalDateTime ^| find "."') DO set DTS=%%a
set CUR_DATE=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%
goto :eof