Search This Blog

Wednesday, May 20, 2020

automated - timestamped ping and tracert with logs (powershell)

Recently I had to run ping tests to provide to my internet provider... TPG.... I have an issue where in the evening my latency spikes (goes from around 7-10ms to up over 200ms, which makes gaming a joke and non event) from around 19:00 to 22:00... This has never happened before, so is abnormal..

In the past I have had no real issue with the support, however this time it has been slow... and a real waste of time... I had already turned off wifi and disconnected all devices except my test machine.. The issue still existed... the increased latency was appearing on the first hop to the TPG gateway from my router.. and only in the evening.. by 22:30 the issue was going away.
They wanted me to run various traceroutes, but the main one to TPG.COM.AU, to show that the latency appeared within the tpg network... which it clearly did as it was the first hop... my gateway..

Multiple times I provided them the same information, and then it took some convincing to get access to an engineer... although I am still waiting on that call... over a week later...

So.. I then decided I wanted to automate this to run a constant ping and regular tracerts.. which were timestamped...

I already have a page that detailed how to created a timestamped ping... so I used this as the basis o a couple of scripts I would schedule.

The ping would run hourly, running a ping for 59 minutes... (3540 successfull pings (1 ping a second)) (there are issues with this and it occurs with timeouts...as they are 4 seconds..).. However this will be better than not having it at all, and potentially something I could fix.

The tracert would run every 5 minutes, with dnslookup enabled to ensure the host details were recorded..

Scripts are below but logging and stdout look like the following, the logs will be created in the same folder as the script and with a timestamped name. ping

2020-05-20 20:36:01 :  2020-05-20T20:36:01.9175593+10:00: 
2020-05-20 20:36:01 :  2020-05-20T20:36:01.9378688+10:00: Pinging tpg.com.au [203.26.27.38] with 32 bytes of data:
2020-05-20 20:36:02 :  2020-05-20T20:36:02.0514975+10:00: Reply from 203.26.27.38: bytes=32 time=134ms TTL=125
2020-05-20 20:36:03 :  2020-05-20T20:36:03.0439801+10:00: Reply from 203.26.27.38: bytes=32 time=125ms TTL=125
tracert

2020-05-20 22:03:01 :  2020-05-20T22:03:01.8844980+10:00: 
2020-05-20 22:03:01 :  2020-05-20T22:03:01.9053306+10:00: Tracing route to tpg.com.au [203.26.27.38]
2020-05-20 22:03:01 :  2020-05-20T22:03:01.9083060+10:00: over a maximum of 30 hops:
2020-05-20 22:03:01 :  2020-05-20T22:03:01.9112846+10:00: 
2020-05-20 22:03:06 :  2020-05-20T22:03:06.4692141+10:00:   1    <1 1.1.1.1="" 1="" 2-2-2-2-static.tpgi.com.au="" 2020-05-20="" 2020-05-20t22:03:12.1730325="" 2020-05-20t22:03:13.2970152="" 2020-05-20t22:03:14.4112638="" 2020-05-20t22:03:15.6023222="" 2020-05-20t22:03:16.7043576="" 2020-05-20t22:03:17.8930681="" 2020-05-20t22:03:17.8970367="" 2020-05-20t22:03:17.9005089="" 22:03:12="" 22:03:13="" 22:03:14="" 22:03:15="" 22:03:16="" 22:03:17="" 2="" 3-3-3-3-s.tpgi.com.au="" 37="" 3="" 4-4-4-4-s.static.tpgi.com.au="" 41="" 43="" 4="" 5-5-5-5-s.tpgi.com.au="" 53="" 54="" 55="" 56="" 57="" 58="" 59="" 5="" 60="" 6="" 77="" 78="" 79="" 7="" 80="" 85="" 86="" :="" code="" complete.="" gateway="" ms="" router="" trace="" www.tpg.com.au="">
And here are both scripts...

ping (pingTPGwithTimestamp.ps1)

3 input parameters
  • $pingDestination (default tpg.com.au) 
  • $pingAmount (default 4) 
  • $Logging (switch) 

Here is the command I run
".\pingTPGwithTimestamp.ps1" -logging -pingDestination "tpg.com.au" -pingAmount 3540

How to configure Taskscheduler to run the powershell script
Program\Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 
Add arguments: -ExecutionPolicy Bypass -WindowStyle Minimized -File ".\pingTPGwithTimestamp.ps1" -logging -pingDestination "tpg.com.au" -pingAmount 3540 
Start in: C:\Powershell\~Home\TPG (do not put quotes around this it will fail to run)



[CmdletBinding()]
param( 
        [Parameter(Mandatory=$false)] [AllowEmptyString()] [string] $pingDestination ='tpg.com.au',
        [Parameter(Mandatory=$false)] [AllowEmptyString()] [int16] $pingAmount = 4,
        [switch] $Logging
    )

$FormatENumerationLimit=-1;

function timestamp ($log)
{
    Process {
            $resultPing = "$(Get-Date -Format o): $_";
            write-host $resultPing;
            if ($Log)
            {
                LogWrite $resultPing;
            }
    }
}


function Get-ScriptDirectory
{
 #Determine the folder in which the script lives.
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

#needed to find the location of the script.
$scriptPath = Get-ScriptDirectory;
$scriptName = $MyInvocation.MyCommand.Name;

$LogfileTimeStampName = (Get-Date).toString("yyyyMMddHHmmss");
$Logfile = "$scriptPath\$($scriptName)_$LogfileTimeStampName.log";

Function LogWrite
{
   Param ([string]$logstring);
   $LogStamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss");
   Add-content $Logfile -value "$LogStamp :  $logstring";
}

PING.EXE $pingDestination -n $pingAmount | timestamp($logging.IsPresent)

tracert(tracertTPGwithTimestamp.ps1)

2 input parameters
  • $dnslookup (switch) 
  • $Logging (switch) 
Here is the command I run
".\tracertTPGwithTimestamp.ps1" -logging  -dnslookup

How to configure Taskscheduler to run the powershell script
Program\Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 
Add arguments: -ExecutionPolicy Bypass -windowstyle Minimized -File ".\tracertTPGwithTimestamp.ps1" -logging  -dnslookup
Start in: C:\Powershell\~Home\TPG (do not put quotes around this it will fail to run)




[CmdletBinding()]
param( 
        [Parameter(Mandatory=$false)] [AllowEmptyString()] [string] $tracertDestination ='tpg.com.au',
        [switch] $dnslookup,
        [switch] $Logging
    )

$FormatENumerationLimit=-1;

function timestamp ($log)
{
    Process {
            $resulttracert = "$(Get-Date -Format o): $_";
            write-host $resulttracert;
            if ($Log)
            {
                LogWrite $resulttracert;
            }
    }
}


function Get-ScriptDirectory
{
 #Determine the folder in which the script lives.
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

#needed to find the location of the script.
$scriptPath = Get-ScriptDirectory;
$scriptName = $MyInvocation.MyCommand.Name;

$LogfileTimeStampName = (Get-Date).toString("yyyyMMddHHmmss");
$Logfile = "$scriptPath\$($scriptName)_$LogfileTimeStampName.log";

Function LogWrite
{
   Param ([string]$logstring);
   $LogStamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss");
   Add-content $Logfile -value "$LogStamp :  $logstring";
}

if ($dnslookup)
{
    tracert.exe $tracertDestination | timestamp($logging.IsPresent)
}
else {
    tracert.exe -d $tracertDestination | timestamp($logging.IsPresent)
}


Share/Bookmark

No comments:

Post a Comment