Advertisement
Devanov

SlowPingTestV3.bat

Apr 14th, 2024 (edited)
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.72 KB | None | 0 0
  1. :: |---------------------------------------------------------------------------------------------------|
  2. :: | SPT3 - Slow Ping Test (Version 3)                                             Created By: Devanov |
  3. :: |---------------------------------------------------------------------------------------------------|
  4. :: |
  5. :: | 1. What is the script meant for?
  6. :: |
  7. :: |    To let run for extended periods (>30 days) on a windows device,
  8. :: |    to monitor connections to pre-defined targets via ICMP-Ping requests
  9. :: |    with the focus on logging occurances in a continous log-file.
  10. :: |
  11. :: | 2. Why is the script CMD-based?
  12. :: |
  13. :: |    The creator of this script is no programmer.
  14. :: |    Furthermore, the script was meant to run with minimal requirements within windows OS.
  15. :: |    It only relied on CMD, with later additions incorporating basic Powershell elements.
  16. :: |    The focus of the script remained the logging and therefore the output, CMD "ping" generated.
  17. :: |
  18. :: | 3. Why am I getting an Access-Error on startup?
  19. :: |
  20. :: |    Before the script starts into the loop, it issues a request to resync the NTP (time) server.
  21. :: |    This step is to ensure that the timestamps within the log-file are correct for the timezone.
  22. :: |    The Time-Resync however requires administrative permissions.
  23. :: |    It is suggested to execute as Admin, but not required.
  24. :: |    The output of the issued command (wether successful or not) will be logged too.
  25. :: |    Note: Before issuing this command, the script tries to start the w32time service required for NTP (silent).
  26. :: |
  27. :: | 4. What happens when the script is aborted/restarted? What if it is moved or the log deleted?
  28. :: |
  29. :: |    As the script only adds to the (existing) SlowPingTest.log file, there will be no data loss upon exiting.
  30. :: |    But the script does not restart on its own if cancelled.
  31. :: |    The script will always search for a log-file in the directory it is located in;
  32. :: |    if not file is present, it will generate one. If one is present, it just adds content to it.
  33. :: |
  34. :: | 5. I want to change the ping-targets or the display name in the log.
  35. :: |
  36. :: |    Right click the .bat file and open in Editor (or Notepad++).
  37. :: |    change values within the "set" commands. EXAMPLE:
  38. :: |    set "var1=YourFirstTargetName" (Keep character-count same for var1,2,3 for log-readability)
  39. :: |    set "srv1=YourFirstTargetAddress" (Can be IP or DNS)
  40. :: |
  41. :: | 6. I want to suppress the output in CLI (for RAM savings or silent executions).
  42. :: |
  43. :: |    Remove the following part of each "single-long-line" argument:
  44. :: |    & powershell -command "Get-Content %cd%\SlowPingTest.log | Select-Object -Last 1"
  45. :: |    ------------
  46. :: |    Change this:
  47. :: |    findstr "%timeout% %error%" "%SPTTEMP%" >nul && (set /p "= %date% / %time% / %var1% / " <nul >> "%cd%\SlowPingTest.log" & findstr "%timeout% %error%" "%SPTTEMP%" >> "%cd%\SlowPingTest.log" & powershell -command "Get-Content %cd%\SlowPingTest.log | Select-Object -Last 1")
  48. :: |    To this (Please keep in mind to adjust %var1% if copy-pasting):
  49. :: |    findstr "%timeout% %error%" "%SPTTEMP%" >nul && (set /p "= %date% / %time% / %var1% / " <nul >> "%cd%\SlowPingTest.log" & findstr "%timeout% %error%" "%SPTTEMP%" >> "%cd%\SlowPingTest.log")
  50. :: |
  51. :: |---------------------------------------------------------------------------------------------------|
  52. :: | Date Of Update: 14.04.2024                                                   Original: 02.02.2023 |
  53. :: |---------------------------------------------------------------------------------------------------|
  54.  
  55. @echo off
  56. cd "%~dp0"
  57.  
  58. set "var1=Cloudflare"
  59. set "srv1=1.1.1.1"
  60.  
  61. set "var2=Google-DNS"
  62. set "srv2=8.8.8.8"
  63.  
  64. set "var3=Google.com"
  65. set "srv3=google.com"
  66.  
  67. set cooldown=0
  68. :: Time delay between ping-request from srv1>2 and srv2>3 (default: 0) // Warning: Increasing this can cause issues if time between cycles isn't increased as well
  69. :: Time delay between issued "cylces" of 3 requests is manually set at the last powershell-line of the script (logic: 60/15=4 -- 0,15,30,45) // Default: 15 (Alternative: 30,60)
  70.  
  71. set "success=TTL"
  72. set "timeout=Anforderung Zielhost Zielnetz"
  73. set "error=Fehler"
  74. set "ntp=erfolgreich Fehler"
  75. :: Please adjust variables based on OS language (Default: German)
  76.  
  77. set "SPTTEMP=%cd%\SPT.temp"
  78.  
  79. echo ----------------
  80. echo SlowPingTest.bat
  81. echo ----------------
  82. echo =====================================================================================>> %cd%\SlowPingTest.log
  83. sc start w32time
  84. echo | set /p=%date% / %time% / RESYNC-NTP />> %cd%\SlowPingTest.log & @w32tm /resync | findstr "%ntp%" >> %cd%\SlowPingTest.log
  85. powershell -command "Get-Content %cd%\SlowPingTest.log | Select-Object -Last 1"
  86.  
  87. :loop
  88.  
  89. setlocal
  90. @ping %srv1% -l 0 -n 1 > "%SPTTEMP%"
  91. findstr "%timeout% %error%" "%SPTTEMP%" >nul && (set /p "= %date% / %time% / %var1% / " <nul >> "%cd%\SlowPingTest.log" & findstr "%timeout% %error%" "%SPTTEMP%" >> "%cd%\SlowPingTest.log" & powershell -command "Get-Content %cd%\SlowPingTest.log | Select-Object -Last 1")
  92. endlocal
  93.  
  94. timeout %cooldown% /nobreak>nul
  95. setlocal
  96. @ping %srv2% -l 0 -n 1 > "%SPTTEMP%"
  97. findstr "%timeout% %error%" "%SPTTEMP%" >nul && (set /p "= %date% / %time% / %var2% / " <nul >> "%cd%\SlowPingTest.log" & findstr "%timeout% %error%" "%SPTTEMP%" >> "%cd%\SlowPingTest.log" & powershell -command "Get-Content %cd%\SlowPingTest.log | Select-Object -Last 1")
  98. endlocal
  99.  
  100. timeout %cooldown% /nobreak>nul
  101. setlocal
  102. @ping %srv3% -l 0 -n 1 > "%SPTTEMP%"
  103. findstr "%timeout% %error%" "%SPTTEMP%" >nul && (set /p "= %date% / %time% / %var3% / " <nul >> "%cd%\SlowPingTest.log" & findstr "%timeout% %error%" "%SPTTEMP%" >> "%cd%\SlowPingTest.log" & powershell -command "Get-Content %cd%\SlowPingTest.log | Select-Object -Last 1")
  104. endlocal
  105.  
  106. powershell -Command "$waitSeconds = 15 - (Get-Date).Second %% 15; Start-Sleep -Seconds $waitSeconds"
  107.  
  108. goto loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement