Skip to content

Commit

Permalink
ping check implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
1nv8rzim committed Sep 2, 2023
1 parent a1ff18f commit 4d543fb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/compscore/ping

go 1.20

require github.com/go-ping/ping v1.1.0

require (
github.com/google/uuid v1.2.0 // indirect
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 h1:b0LrWgu8+q7z4J+0Y3Umo5q1dL7NXBkKBWkaVkAq17E=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005 h1:pDMpM2zh2MT0kHy037cKlSby2nEhD50SYqwQk76Nm40=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
42 changes: 41 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,48 @@ package main

import (
"context"
"fmt"

"github.com/go-ping/ping"
)

func Run(ctx context.Context, target string, command string, expectedOutput string, username string, password string) (bool, string) {
return true, ""
// create ping
pinger, err := ping.NewPinger(target)
if err != nil {
return false, err.Error()
}

pinger.Count = 1
doneChan := make(chan error, 1)

// run ping
go func() {
doneChan <- pinger.Run()
}()

// handle ping output
select {
case err := <-doneChan:
if err != nil {
// error encoutnered when attempting to ping
return false, fmt.Sprintf("Encounter error: %s", err)
}

stats := pinger.Statistics()

if stats.PacketLoss == 0 {
// successful ping
return true, ""
} else {
// packet loss not 0
return false, fmt.Sprintf("Packet loss: %.2f%%", stats.PacketLoss)
}

case <-ctx.Done():
pinger.Stop()

// context exceed; timeout
return false, fmt.Sprintf("Timeout exceeded; err: %s", ctx.Err())
}
}

0 comments on commit 4d543fb

Please sign in to comment.