create a CMD command or script that does the following: 1.Continuously monitor internet…
- User
create a CMD command or script that does the following:
1.Continuously monitor internet connectivity. 2.Display messages in the CMD window: When first run bat cmd show online status: "[Current Time ,without milliseconds] Internet is Online" When offline: "[Current Time ,without milliseconds] Internet is OFFLINE" When back online: "OFFLINE duration is [offline duration]" Show a popup message with a sound alarm when the internet goes offline it will keep alarm until user click close popup. The popup message should only appear once when the internet goes offline, not repeatedly while it's offline. The CMD window should only display status changes (going offline or coming back online), not continuously repeat the same status. Track the duration of the offline period and display it when the internet comes back online. The script should run continuously, monitoring the internet status and updating only when there's a change ,explain what you understand first.
- Assistant
Understood. You would like me to create a Windows CMD script that continuously monitors internet connectivity and displays status updates in the CMD window only when there is a change in status (either going offline or coming back online). The script should:
- When first run, display the current time (without milliseconds) and indicate that the internet is online.
- When the internet goes offline:
- Display a message in the CMD window with the current time stating that the internet is OFFLINE.
- Show a popup message with a sound alarm that persists until the user closes it. The popup should only appear once when the internet goes offline, not repeatedly while it's offline.
- When the internet comes back online:
- Display the duration of the offline period in the CMD window (e.g., "OFFLINE duration is [offline duration]").
- Only display status changes in the CMD window, not continuously repeat the same status.
- Continuously monitor the internet status and update only when there's a change.
To achieve this, the script will:
- Use a loop to continuously check internet connectivity by pinging a reliable host (e.g., 8.8.8.8).
- Use variables to keep track of the current status (online/offline) and the time when the status changes.
- Use a PowerShell command within the script to display the popup message with a sound alarm when the internet goes offline.
- Calculate the offline duration when the internet comes back online.
- Ensure that the popup message only appears once during an offline period.
Now, I'll create the CMD script that meets these requirements.
Batch Script:
MonitorInternet.cmd@echo off setlocal enableextensions enabledelayedexpansion :: Initial status set "status=online" set "prevStatus=online" set "offlineStart=" set "offlineDuration=" :: Function to get current time without milliseconds :CurrentTime for /F "tokens=1-2 delims=." %%a in ("%time%") do set "currentTime=%%a" goto :eof :: Function to show popup with sound using PowerShell :ShowOfflinePopup powershell -Command "& { Add-Type -AssemblyName PresentationFramework; Add-Type -AssemblyName System.Media; $player = New-Object System.Media.SoundPlayer; $player.SoundLocation = 'C:\Windows\Media\Alarm03.wav'; $player.PlayLooping(); [System.Windows.MessageBox]::Show('Internet is OFFLINE','Connection Lost',[System.Windows.MessageBoxButton]::OK,[System.Windows.MessageBoxImage]::Warning); $player.Stop(); }" goto :eof echo Monitoring internet connectivity... call :CurrentTime echo [%currentTime%] Internet is Online :Loop :: Ping a reliable host to check internet connectivity ping -n 1 8.8.8.8 >nul 2>&1 if %errorlevel%==0 ( set "status=online" ) else ( set "status=offline" ) :: Check for status change if not "%status%"=="%prevStatus%" ( call :CurrentTime if "%status%"=="offline" ( echo [%currentTime%] Internet is OFFLINE set "offlineStart=%date% %time%" start "" cmd /c call :ShowOfflinePopup ) else ( :: Calculate offline duration for /F "tokens=1-2" %%a in ("%offlineStart%") do ( set "startDate=%%a" set "startTime=%%b" ) for /F "tokens=1-2" %%a in ("%date% %time%") do ( set "endDate=%%a" set "endTime=%%b" ) :: Convert times to seconds since midnight for /F "tokens=1-3 delims=:." %%h in ("%startTime%") do ( set /A "startSeconds=%%h*3600+%%i*60+%%j" ) for /F "tokens=1-3 delims=:." %%h in ("%endTime%") do ( set /A "endSeconds=%%h*3600+%%i*60+%%j" ) :: Calculate duration set /A "durationSeconds=endSeconds - startSeconds" if %durationSeconds% LSS 0 ( :: Crossed midnight set /A "durationSeconds=durationSeconds+86400" ) set /A "hours=durationSeconds/3600" set /A "minutes=(durationSeconds%%3600)/60" set /A "seconds=durationSeconds%%60" :: Format duration set "offlineDuration=%hours% hours %minutes% minutes %seconds% seconds" echo OFFLINE duration is %offlineDuration% set "offlineStart=" ) set "prevStatus=%status%" ) :: Wait before next check timeout /t 5 >nul goto :Loop
Explanation of the Script
-
Initialization:
statuskeeps track of the current internet status (onlineoroffline).prevStatuskeeps track of the previous status to detect changes.offlineStartrecords the timestamp when the internet goes offline.offlineDurationwill hold the duration of the offline period.
-
Function
:CurrentTime:- Extracts the current time without milliseconds and stores it in the
currentTimevariable. - Uses a
forloop to parse the%time%system variable.
- Extracts the current time without milliseconds and stores it in the
-
Function
:ShowOfflinePopup:- Uses PowerShell to create a popup message with a looping sound alarm.
- The alarm sound is from a default Windows sound file (
Alarm03.wav), but you can change the path to any.wavfile you prefer. - The sound plays in a loop until the user closes the popup.
- Note: This function is called in a new command window using
start "" cmd /c call :ShowOfflinePopupto prevent blocking the main script.
-
Main Loop (
:Loop):- Pings Google's DNS server (
8.8.8.8) once (-n 1) without displaying output. - Checks
%errorlevel%to determine if the ping was successful.- If
errorlevelis0, the internet is considered online. - Otherwise, it's offline.
- If
- If there's a change in status (
%status%is not equal to%prevStatus%):- Updates the
currentTime. - If the internet just went offline:
- Displays a message with the current time stating that the internet is OFFLINE.
- Records the offline start time.
- Calls the
:ShowOfflinePopupfunction to show the popup with sound.
- If the internet just came back online:
- Calculates the offline duration by:
- Parsing the start and end times.
- Converting times to seconds since midnight.
- Calculating the difference in seconds.
- Formatting the duration into hours, minutes, and seconds.
- Displays the offline duration.
- Resets the
offlineStartvariable.
- Calculates the offline duration by:
- Updates
prevStatusto the currentstatus.
- Updates the
- Pings Google's DNS server (
-
Wait Period:
- Uses
timeout /t 5 >nulto wait for 5 seconds before the next check. - Adjust the number
5to change the frequency of the checks.
- Uses
Notes
-
Sound Alarm:
- The script plays
C:\Windows\Media\Alarm03.wavas the alarm sound. Ensure this file exists on your system. - You can replace
'C:\Windows\Media\Alarm03.wav'with the path to any.wavfile you prefer.
- The script plays
-
Popup Message:
- The popup message is created using PowerShell's
[System.Windows.MessageBox]. - The message box will keep the sound alarm playing until the user clicks "OK".
- The popup message is created using PowerShell's
-
Running the Script:
- Save the script with a
.cmdextension, e.g.,MonitorInternet.cmd. - Run the script by double-clicking it or from the command prompt.
- Save the script with a
-
Permissions:
- The script may require appropriate permissions to run PowerShell commands and access sound files.
-
Continuous Monitoring:
- The script runs indefinitely until you manually stop it by closing the command window.
Customization
-
Ping Target:
- You can change
8.8.8.8to another reliable IP address or domain name if desired.
- You can change
-
Check Interval:
- Modify the
timeout /t 5line to change how often the script checks the internet connectivity.
- Modify the
-
Time Format:
- The script uses the system's time format. Ensure your computer's time settings match your desired format.
Limitations
-
Accuracy of Offline Duration:
- The duration calculation assumes that the offline period doesn't span more than one day. If the internet is offline over midnight, the script adjusts the calculation accordingly.
- For extended offline periods spanning multiple days, additional logic would be needed.
-
System Compatibility:
- The script is designed for Windows systems with PowerShell installed (which is default on most modern Windows versions).
Let me know if you need any further assistance or modifications to the script.