Skip to content

Commit

Permalink
Merge pull request #267 from moul/dev/moul/wip
Browse files Browse the repository at this point in the history
Add ping
  • Loading branch information
moul authored Oct 24, 2017
2 parents 7ec0fc9 + 922c2d7 commit e134ab2
Show file tree
Hide file tree
Showing 1,763 changed files with 90,339 additions and 126,042 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ AUTHOR(S):
Manfred Touron <https://github.com/moul/advanced-ssh-config>

COMMANDS:
ping Send packets to the SSH server and display statistics
info Display system-wide information
config Manage ssh and assh configuration
sockets Manage control sockets
Expand Down Expand Up @@ -713,6 +714,23 @@ Create a master control sockets.
$ assh sockets master
```

#### `assh ping`

Send packets to the SSH server and display stats.

```console
$ assh ping -c 4 localhost
PING localhost (127.0.0.1) PORT 22 (ssh) PROTO tcp
Connected to 127.0.0.1: seq=0 time=321µs protocol=tcp port=22
Connected to 127.0.0.1: seq=1 time=501µs protocol=tcp port=22
Connected to 127.0.0.1: seq=2 time=550µs protocol=tcp port=22
Connected to 127.0.0.1: seq=3 time=641µs protocol=tcp port=22

--- localhost assh ping statistics ---
4 packets transmitted, 4 packets received, 0.00% packet loss
round-trip min/avg/max = 321µs/503.25µs/641µs
```

## Install

Get the latest version using GO (recommended way):
Expand Down Expand Up @@ -827,6 +845,7 @@ Host *
* Add possibility to disable automatic configuration rewrite ([#239](https://github.com/moul/advanced-ssh-config/issues/239))
* Add `BeforeConfigWrite` and `AfterConfigWrite` new hooks ([#239](https://github.com/moul/advanced-ssh-config/issues/239))
* Generate full assh binary path in ~/.ssh/config ([#148](https://github.com/moul/advanced-ssh-config/issues/148))
* Initial version of `assh ping` command

[Full commits list](https://github.com/moul/advanced-ssh-config/compare/v2.6.0...master)

Expand Down
49 changes: 20 additions & 29 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ var Commands = []cli.Command{
},
},
},
{
Name: "ping",
Usage: "Send packets to the SSH server and display statistics",
Action: cmdPing,
Flags: []cli.Flag{
cli.IntFlag{
Name: "port, p",
Usage: "SSH destination port",
},
cli.UintFlag{
Name: "count, c",
Usage: "Stop after sending 'count' packets",
},
cli.Float64Flag{
Name: "wait, i",
Usage: "Wait 'wait' seconds between sending each packet",
Value: 1,
},
cli.BoolFlag{
Name: "o",
Usage: "Exit successfully after receiving one reply packet",
},
cli.Float64Flag{
Name: "waittime, W",
Usage: "Time in seconds to wait for a reply for each packet sent",
Value: 1,
},
},
},
/*
{
Name: "info",
Expand Down
92 changes: 92 additions & 0 deletions pkg/commands/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package commands

import (
"fmt"
"net"
"time"

"github.com/urfave/cli"

"github.com/moul/advanced-ssh-config/pkg/config"
. "github.com/moul/advanced-ssh-config/pkg/logger"
)

func cmdPing(c *cli.Context) error {
if len(c.Args()) < 1 {
Logger.Fatalf("assh: \"ping\" requires exactly 1 argument. See 'assh ping --help'.")
}

conf, err := config.Open(c.GlobalString("config"))
if err != nil {
Logger.Fatalf("Cannot open configuration file: %v", err)
}
if err = conf.LoadKnownHosts(); err != nil {
Logger.Debugf("Failed to load assh known_hosts: %v", err)
}
target := c.Args()[0]
host, err := computeHost(target, c.Int("port"), conf)
if err != nil {
Logger.Fatalf("Cannot get host '%s': %v", target, err)
}

if len(host.Gateways) > 0 {
Logger.Fatalf("assh \"ping\" is not working with gateways (yet).")
}
if host.ProxyCommand != "" {
Logger.Fatalf("assh \"ping\" is not working with custom ProxyCommand (yet).")
}

portName := "ssh"
if host.Port != "22" {
// fixme: resolve port name
portName = "unknown"
}
proto := "tcp"
fmt.Printf("PING %s (%s) PORT %s (%s) PROTO %s\n", target, host.HostName, host.Port, portName, proto)
dest := fmt.Sprintf("%s:%s", host.HostName, host.Port)
count := c.Uint("count")
transmittedPackets := 0
receivedPackets := 0
minRoundtrip := time.Duration(0)
maxRoundtrip := time.Duration(0)
totalRoundtrip := time.Duration(0)
for seq := uint(0); count == 0 || seq < count; seq++ {
if seq > 0 {
time.Sleep(time.Duration(c.Float64("wait")) * time.Second)
}
start := time.Now()
conn, err := net.DialTimeout(proto, dest, time.Second*time.Duration(c.Float64("waittime")))
transmittedPackets++
duration := time.Now().Sub(start)
totalRoundtrip += duration
if minRoundtrip == 0 || minRoundtrip > duration {
minRoundtrip = duration
}
if maxRoundtrip < duration {
maxRoundtrip = duration
}
if err == nil {
defer conn.Close()
}
if err == nil {
receivedPackets++
fmt.Printf("Connected to %s: seq=%d time=%v protocol=%s port=%s\n", host.HostName, seq, duration, proto, host.Port)
if c.Bool("o") {
goto stats
}
} else {
// FIXME: switch on error type
fmt.Printf("Request timeout for seq %d (%v)\n", seq, err)
}
}

// FIXME: catch Ctrl+C

stats:
fmt.Printf("\n--- %s assh ping statistics ---\n", target)
packetLossRatio := float64(transmittedPackets-receivedPackets) / float64(transmittedPackets) * 100
fmt.Printf("%d packets transmitted, %d packets received, %.2f%% packet loss\n", transmittedPackets, receivedPackets, packetLossRatio)
avgRoundtrip := totalRoundtrip / time.Duration(transmittedPackets)
fmt.Printf("round-trip min/avg/max = %v/%v/%v\n", minRoundtrip, avgRoundtrip, maxRoundtrip)
return nil
}
23 changes: 23 additions & 0 deletions vendor/github.com/Masterminds/sprig/date.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/Masterminds/sprig/date_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions vendor/github.com/Masterminds/sprig/dict.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e134ab2

Please sign in to comment.