How to log ping condition

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of areriff areriff 2 months, 1 week ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #47743
    Profile photo of areriff
    areriff
    Participant

    Hi, I'm PS noob. I am also a noob C++ programmer (I know the basics).

    Is it possible to ping two servers indefinitely while only displaying/log ONLY when the latency is more than 100ms?

    I have two monitors.
    I'm gaming and sometime having bad connection to the game server.
    The server IP will be either 103.28.54.1 or 103.10.124.1
    currently I open two Command Prompt windows and do :

    ping 103.28.54.1 -t
    ping 103.10.124.1 -t
    

    separately in each window

    So I look regularly to the CMD windows to check if there are any latency issues.

    It would be convenience if the output only display when latency is above certain threshold and would be nicer if I can play a WAV file when the threshold is reached so that I could stay away from the fight for a while if latency is bad 🙂

    I looked into PowerShell and think it could solve this little problem. So I started learning it today 🙂
    I tried some IF statements and few FOR loops. Doesn't work (doesn't know how to make it work).
    Is possible to do that with the condition mentioned? How?

    #47764
    Profile photo of Trevor Freedland
    Trevor Freedland
    Participant

    Hi Areriff,

    This seems like it may fit your needs:

    $server = '8.8.8.8'
    $responseThreshold = 0
    $processName = 'notepad++'
    $soundFile = 'c:\WINDOWS\Media\notify.wav'
    
    $loopOn = $true
    
    while ($loopOn -eq $true)
    {
        $ping = Test-Connection -ComputerName $server -count 1 | Select-Object ResponseTime
        $timeStamp = (Get-Date).ToShortTimeString()
        Write-Output "$timeStamp : $($ping.responsetime) ms"
        
        #Check response time against threshold, play sound if greater than or equal to.
        if ($ping.ResponseTime -ge $responseThreshold)
        {
            $sound = New-Object System.Media.SoundPlayer
            $sound.SoundLocation = $soundFile
            $sound.Play()
        }
        
        #Check if process is still running, if not, exit loop.
        try { Get-Process -Name $processName -ErrorAction Stop | Out-Null}
        catch { $loopOn = $false }
        Start-Sleep 1
    }
    

    You can enter the site specific settings at the top including server address and response threshold (in ms).
    I thought you probably want this to run only while your game is running, so it checks for the process, and exits the loop if it is not found. I used notepad++ for testing, so be sure to update that variable with the name of the game process. The datetime and response time will update in the console so you can see spikes and other activity if desired.

    #47769
    Profile photo of random commandline
    random commandline
    Participant

    This is a test.

    Do {
    Test-Connection google.com | Select-Object Address, 
    IPV4Address, ResponseTime, ResponseTimeToLive | 
    Where-Object {$_.ResponseTime -gt '100'}} While ($true)
    
    #47779
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Trevor's code looks much better than this, but a quick bash :

    $devices = 'www.yahoo.com', 'www.google.com'
    
    $hash = @()
    while($true){
        $devices | foreach {
            
            if ((Test-Connection $_ -count 2).ReponseTime -le 100) {
                $hash += @{ Normal= "$_ less than 100ms" } 
            }else{   
                $hash += @{ WARNING="$_ More than 100ms" } 
            }
                
                $Hash | foreach { [PSCustomObject]@{
                    
                    Status = $_.keys | % { "$_" }
                    Device = $_.Values | % { "$_" }
                    }
                } 
            }
        }
    
    #47783
    Profile photo of areriff
    areriff
    Participant

    Great, thanks. I'll study the suggestions and try it.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.