From 600d9c57e0f04484facafc30e3e25de03b45f812 Mon Sep 17 00:00:00 2001 From: Max Krieger Date: Mon, 10 Mar 2025 12:19:29 +0100 Subject: [PATCH] first commit --- README.md | 43 +++++++++++++++ example_output.txt | 29 ++++++++++ packetloss_tester.ps1 | 122 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 README.md create mode 100644 example_output.txt create mode 100644 packetloss_tester.ps1 diff --git a/README.md b/README.md new file mode 100644 index 0000000..2df6e02 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# Easy Packet Loss Tracker + +## Overview +This PowerShell script helps you monitor packet loss on your network by continuously pinging a specified target (website or IP address) and providing real-time updates on successful responses and timeouts. + +## Parameters +The script accepts the following parameters: + +- **-target**: Specifies the target website or IP address to ping. Defaults to "google.com" if not specified. +- **-count**: Specifies the number of times to ping the target. Defaults to 900 if not specified. +- **-OnFailure** (scriptblock, optional): A script block to execute when a ping attempt fails (i.e., when a timeout occurs). This script block will be executed in the context of the script. If not provided, no action will be taken on failure. +- **-LogFile**: Specifies the log file path. + + +## Usage +1. Download or clone the script to your local machine. +2. Open a PowerShell terminal. +3. Navigate to the directory where the script is located. +4. Run the script with optional parameters: + ```powershell + .\packetloss_tester.ps1 -target "example.com" -count 1000 -OnFailure $failureAction +5. Log to a file with: + ```powershell + .\packetloss_tester.ps1 -LogFile "C:\logs\packetloss.txt" + +## Output +Please see example_output.txt for a full example run. + +At the end of the execution you will get a summary: + ``` + ===== Zusammenfassung ===== + From: 2025-03-10 10:11:42 || To: 2025-03-10 10:28:57 + Total Packets sent: 900 + Packet loss: 3.11% + Total timeouts: 28 + + == Alle Timeouts: = + [18/900] 2024-02-22 10:12:00 + [29/900] 2024-02-22 10:12:15 + ... + [866/900] 2024-02-22 10:28:14 + [867/900] 2024-02-22 10:28:19 + =================== diff --git a/example_output.txt b/example_output.txt new file mode 100644 index 0000000..793bc2c --- /dev/null +++ b/example_output.txt @@ -0,0 +1,29 @@ +2025-03-10 12:17:51 - Pinging vc.voipoperator.de ... +[1/10] 2025-03-10 12:17:51 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[2/10] 2025-03-10 12:17:52 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[3/10] 2025-03-10 12:17:54 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[4/10] 2025-03-10 12:17:55 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[5/10] 2025-03-10 12:17:56 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[6/10] 2025-03-10 12:17:57 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[7/10] 2025-03-10 12:17:58 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[8/10] 2025-03-10 12:17:59 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[9/10] 2025-03-10 12:18:00 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 +[10/10] 2025-03-10 12:18:01 - Reply from vc.voipoperator.de: bytes=32 time=20ms TTL=80 + _ _ _ ______ _ _ _ +| | | | | | | ___ (_) (_) (_) +| |_| | ___| | _____ | |_/ /_ _ _ __ ___ ___ ___ _ ____ ___ ___ ___ +| _ |/ _ \ |/ / _ \ | ___ \ | | | '__/ _ \/ __|/ _ \ '__\ \ / / |/ __/ _ \ +| | | | __/ < (_) | | |_/ / |_| | | | (_) \__ \ __/ | \ V /| | (_| __/ +\_| |_/\___|_|\_\___/ \____/ \__,_|_| \___/|___/\___|_| \_/ |_|\___\___| + +Packetloss Tester +by Max Krieger +heko-bs.de + +===== Zusammenfassung ===== +From: 2025-03-10 12:17:51 || To: 2025-03-10 12:18:01 +Total Packets sent: 10 +Packet loss: 0% +Total timeouts: 0 +== Alle Timeouts: == +=================== diff --git a/packetloss_tester.ps1 b/packetloss_tester.ps1 new file mode 100644 index 0000000..f3abe5c --- /dev/null +++ b/packetloss_tester.ps1 @@ -0,0 +1,122 @@ +<# +.SYNOPSIS + This script helps you monitor packet loss on your network by continuously pinging a specified target (website or IP address) + and providing real-time updates on successful responses and timeouts. +.PARAMETER target + Specifies the target website or IP address to ping. Defaults to "vc.voipoperator.de" if not specified. +.PARAMETER count + Specifies the number of times to ping the target. Defaults to 900 if not specified. +.PARAMETER OnFailure + (scriptblock, optional): A script block to execute when a ping attempt fails (i.e., when a timeout occurs). This script block will be executed in the context of the script. If not provided, no action will be taken on failure. +.PARAMETER LogFile + Specifies the log file path. Example: -LogFile "C:\logs\packetloss.txt" +.INPUTS + None +.OUTPUTS + Just output on screen, unless redirected to a file +.NOTES + Version: 1.1 + Author: Heko Büroservice GmbH & Co. KG. + Creation Date: 2025-03-10 +.EXAMPLE + .\packetloss_tester.ps1 -target "example.com" -count 1000 -OnFailure $failureAction -LogFile "C:\logs\packetloss.txt" +#> +param ( + [string]$target = "vc.voipoperator.de", + [int]$count = 900, + [scriptblock]$OnFailure, + [string]$LogFile # Optionaler Parameter für Logging +) + +$successCount = 0 +$timeoutCount = 0 +$processed = 0 +$allTimeouts = @() +$start = Get-Date -Format "yyyy-MM-dd HH:mm:ss" +$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + +Function Write-Log { + param ( + [string]$message + ) + if ($LogFile) { + # In Log-Datei schreiben + Add-Content -Path $LogFile -Value $message + } + # In Konsole ausgeben + Write-Host $message +} + +Function Invoke-Intro { + Write-Log " _ _ _ ______ _ _ _ " + Write-Log "| | | | | | | ___ (_) (_) (_) " + Write-Log "| |_| | ___| | _____ | |_/ /_ _ _ __ ___ ___ ___ _ ____ ___ ___ ___ " + Write-Log "| _ |/ _ \ |/ / _ \ | ___ \ | | | '__/ _ \/ __|/ _ \ '__\ \ / / |/ __/ _ \" + Write-Log "| | | | __/ < (_) | | |_/ / |_| | | | (_) \__ \ __/ | \ V /| | (_| __/" + Write-Log "\_| |_/\___|_|\_\___/ \____/ \__,_|_| \___/|___/\___|_| \_/ |_|\___\___|" + Write-Log "" + Write-Log "Packetloss Tester" + Write-Log "by Max Krieger" + Write-Log "heko-bs.de" + Write-Log "" +} + +Write-Log "$timestamp - Pinging $target ..." + +try { + trap [System.Management.Automation.PipelineStoppedException] { + Write-Log "`nSkript wurde abgebrochen. Zusammenfassung wird nicht ausgeführt." + exit + } + + for ($processed = 0; $processed -lt $count; $processed++) { + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + $pingResult = Test-Connection -ComputerName $target -Count 1 -Quiet + + if ($pingResult) { + $successCount++ + $pingReply = Test-Connection -ComputerName $target -Count 1 + if ($pingReply -ne $null) { + Write-Log "[$($processed+1)/$count] $timestamp - Reply from $($pingReply.Address): bytes=$($pingReply.BufferSize) time=$($pingReply.ResponseTime)ms TTL=$($pingReply.TimeToLive)" + } + } else { + $timeoutCount++ + Write-Log "[$($processed+1)/$count] $timestamp - Request timed out" + $allTimeouts += "[$($processed+1)/$count] $timestamp" + + if ($OnFailure) { + Invoke-Command -ScriptBlock $OnFailure + } + } + + Start-Sleep -Seconds 1 # Pause für 1 Sekunde + } +} catch { + Write-Log "`nEin Fehler ist aufgetreten: $_" +} finally { + if ($? -eq $false) { + Write-Log "`nSkript wurde abgebrochen oder ein Fehler ist aufgetreten. Keine Zusammenfassung." + exit + } + + $packetLoss = [Math]::Round(($timeoutCount / $processed) * 100, 2) + $totalTimeouts = $timeoutCount + Invoke-Intro + + Write-Log "===== Zusammenfassung =====" + Write-Log "From: $start || To: $timestamp" + Write-Log "Total Packets sent: $processed" + Write-Log "Packet loss: $packetLoss%" + Write-Log "Total timeouts: $totalTimeouts" + Write-Log "== Alle Timeouts: ==" + foreach ($timeout in $allTimeouts) { + Write-Log $timeout + } + Write-Log "===================" + + # Warte auf Tastendruck, nur wenn keine Datei geschrieben wird + if (-not $LogFile) { + Write-Host "Beliebige Taste zum Beenden drücken..." + $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') + } +} \ No newline at end of file