-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (100 loc) · 2.35 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"sync"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
type ConnResult struct {
Server string
Success bool
}
func main() {
username := flag.String("u", "", "username to use for authentication")
password := flag.String("p", "", "password to use for authentication")
serverList := flag.String("s", "", "path to server list to try out")
activateDebug := flag.Bool("d", false, "activate debug log")
flag.Parse()
if *username == "" || *password == "" || *serverList == "" {
log.Error("A parameter was left empty, aborting script")
os.Exit(1)
}
if *activateDebug {
log.SetLevel(log.DebugLevel)
}
log.Debug("Creating ssh client config")
config := &ssh.ClientConfig{
User: *username,
Auth: []ssh.AuthMethod{
ssh.Password(*password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
log.Debug("Reading server list")
servers, err := readServerList(*serverList)
if err != nil {
log.Error(err)
os.Exit(1)
}
var wg sync.WaitGroup
c := make(chan ConnResult, len(servers))
for _, server := range servers {
wg.Add(1)
go func(serverName string) {
defer wg.Done()
log.Debug("Checking connection to: ", serverName)
success := checkConnection(serverName, config)
result := ConnResult{
Server: serverName,
Success: success,
}
c <- result
log.Debug("Done checkin connection to: %v", serverName)
}(server)
}
wg.Wait()
close(c)
serverMap := make(map[string]bool)
for result := range c {
serverMap[result.Server] = result.Success
}
for _, server := range servers {
result := serverMap[server]
resultString := ""
if result {
resultString = "Success"
} else {
resultString = "Failure"
}
fmt.Printf("%v\t\t%v\n", resultString, server)
}
}
func checkConnection(server string, clientConfig *ssh.ClientConfig) bool {
client, err := ssh.Dial("tcp", server, clientConfig)
if err != nil {
return false
}
client.Close()
return true
}
func readServerList(serverPath string) ([]string, error) {
file, err := os.Open(serverPath)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if !strings.Contains(line, ":") {
line = line + ":22"
}
lines = append(lines, line)
}
return lines, scanner.Err()
}