-
Notifications
You must be signed in to change notification settings - Fork 7
/
cmd.go
71 lines (58 loc) · 1.4 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"github.com/alexcesaro/log"
"github.com/alexcesaro/log/golog"
"github.com/jessevdk/go-flags"
)
// Command-line flag options
type Options struct {
Verbose []bool `short:"v" long:"verbose" description:"Show verbose logging."`
Number int `long:"num" description:"Number of concurrent hammers." default:"1"`
}
var logLevels = []log.Level{
log.Warning,
log.Info,
log.Debug,
}
func main() {
// Setup cli parser
options := Options{}
parser := flags.NewParser(&options, flags.Default)
parser.Usage = "[OPTIONS] hostname[:port]"
args, err := parser.ParseArgs(os.Args[1:])
if err != nil {
fmt.Println(err)
return
} else if len(args) < 1 {
fmt.Println("Invalid usage: Missing hostname.")
return
}
host := args[0]
if !strings.Contains(host, ":") {
host += ":22"
}
// Set verbosity of logger
numVerbose := len(options.Verbose)
if numVerbose > len(logLevels) {
numVerbose = len(logLevels)
}
logLevel := logLevels[numVerbose]
logger = golog.New(os.Stderr, logLevel)
// Construct interrupt handler
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
logger.Infof("Hammering: %s", host)
h := NewHammer(host, options.Number)
err = h.Start()
if err != nil {
logger.Errorf("Failed to start: %s", err)
return
}
<-sig // Wait for ^C signal
logger.Warningf("Interrupt signal detected, shutting down.")
h.Stop()
}