create me cmd command that will detect when internet connection is offline ,main focus is to detect when internet connection is lost and what time it is and duration internet is offline and make sound alarm with message popup to notify user only popup message 1 time when offline and again when online ,make sure there line display test when offline and when online and time in cmd ,also make it recheck internet every 5 sec or so and make sure this cmd is running efficiently and less impact on system and best for this task.
Assistant
Certainly! Below is a Windows Command Prompt script that monitors your internet connection status. It checks the connection every 5 seconds, detects when the internet is offline or back online, logs the time and duration, and provides a popup message with a sound alert when the status changes.
batch
@echo off
setlocal EnableDelayedExpansion
REM Initialize variables
set "status=unknown"
set "offlineTime="
:begin
REM Check internet connectivity by pinging a reliable server
ping -n 1 www.google.com >nul
if errorlevel 1 (
set "currentStatus=offline"
) else (
set "currentStatus=online"
)
REM If the status has changed, update and notify the user
if not "%currentStatus%"=="%status%" (
set "status=%currentStatus%"
echo %date% %time% - Internet is now %status%
if "%status%"=="offline" (
REM Record the time when the internet went offline
set "offlineTime=%time%"
REM Play a sound and show a popup message
powershell -c "(New-Object Media.SoundPlayer 'C:\Windows\Media\Windows Exclamation.wav').PlaySync()"
powershell -c "& {Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.Interaction]::MsgBox('Internet is now offline', 'OKOnly,SystemModal,Information', 'Connection Status')}"
) else (
REM Calculate the offline duration
call :calculateDuration "%offlineTime%" "%time%"
REM Play a sound and show a popup message with duration
powershell -c "(New-Object Media.SoundPlayer 'C:\Windows\Media\Windows Ding.wav').PlaySync()"
powershell -c "& {Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.Interaction]::MsgBox('Internet is back online.`nOffline duration: %duration% seconds', 'OKOnly,SystemModal,Information', 'Connection Status')}"
)
) else (
REM Log the current status without notification
echo %date% %time% - Internet is still %status%
)
REM Wait for 5 seconds before rechecking
timeout /t 5 /nobreak >nul
goto begin
:calculateDuration
REM Function to calculate the duration between two times in seconds
set "startTime=%~1"
set "endTime=%~2"
REM Convert start time to seconds
for /F "tokens=1-3 delims=:." %%a in ("%startTime%") do (
set /A "startSecs=%%a*3600 + %%b*60 + %%c"
)
REM Convert end time to seconds
for /F "tokens=1-3 delims=:." %%a in ("%endTime%") do (
set /A "endSecs=%%a*3600 + %%b*60 + %%c"
)
REM Calculate duration
set /A "duration=endSecs - startSecs"
if %duration% LSS 0 (
set /A "duration+=86400"
)
exit /b
How the Script Works:
Internet Connectivity Check: The script pings www.google.com once every 5 seconds.
If the ping is successful, the internet is considered online.
If the ping fails, the internet is considered offline.
Status Change Detection: The script keeps track of the previous status.
When a change is detected (from online to offline or vice versa), it triggers notifications.
Notifications:
Sound Alert: Plays a sound using PowerShell.
Offline: Windows Exclamation.wav
Online: Windows Ding.wav
Popup Message: Displays a message box using PowerShell with the status change and, if back online, the offline duration.
Logging: Each check logs the date, time, and current status in the Command Prompt window.
Efficiency: Uses timeout for efficient waiting periods without excessive resource usage.
Instructions:
Save the Script:
Open Notepad or any text editor.
Copy and paste the script into the editor.
Save the file with a .bat extension, for example, InternetMonitor.bat.
Run the Script:
Double-click the .bat file to start the script.
A Command Prompt window will open and begin monitoring your internet connection.
Permissions:
Ensure you have the necessary permissions to run PowerShell scripts and display message boxes.
If prompted by Windows Defender or your antivirus software, allow the script to run.
Customization:
Ping Address: You can change www.google.com to another reliable server if preferred.
Sound Files: Modify the sound file paths if you want to use different sounds.
Check Interval: Adjust the timeout /t 5 command to change the check frequency (in seconds).
Notes:
PowerShell Usage: The script uses PowerShell commands to play sounds and display popup messages. This ensures compatibility and reduces the need for external tools.
Time Calculation:
The :calculateDuration function computes the time difference in seconds.
It accounts for cases where the offline period crosses midnight.
The script assumes the time format is HH:MM:SS using a 24-hour clock.
Efficiency:
The use of timeout reduces CPU usage compared to alternative methods like a ping loop.
The script avoids unnecessary resource consumption by checking connectivity at defined intervals.
Example Output:
code
2023-10-05 14:23:10.45 - Internet is now online
2023-10-05 14:23:15.49 - Internet is still online
2023-10-05 14:23:20.53 - Internet is now offline
2023-10-05 14:23:25.57 - Internet is still offline
2023-10-05 14:23:30.60 - Internet is now online
Troubleshooting:
If the popup messages or sounds do not work, ensure that: