/
main.go
93 lines (85 loc) · 1.87 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
package main
import (
"strconv"
"time"
"github.com/hashicorp/go-plugin"
"github.com/mmcquillan/hex-plugin"
"golang.org/x/crypto/ssh"
)
type HexSsh struct {
}
func (g *HexSsh) Perform(args hexplugin.Arguments) (resp hexplugin.Response) {
// initialize return values
var output = ""
var success = true
// setup the server connection
serverconn := true
clientconn := &ssh.ClientConfig{
User: args.Config["login"],
Auth: []ssh.AuthMethod{
ssh.Password(args.Config["pass"]),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
port := "22"
if args.Config["port"] != "" {
port = args.Config["port"]
}
limit, err := strconv.Atoi(args.Config["retry"])
if err != nil {
limit = 1
}
retryCounter := 0
for retryCounter < limit {
client, err := ssh.Dial("tcp", args.Config["server"]+":"+port, clientconn)
if err != nil {
output = err.Error()
success = false
}
if client == nil {
serverconn = false
} else {
defer client.Close()
session, err := client.NewSession()
if err != nil {
output = err.Error()
success = false
}
if session == nil {
serverconn = false
} else {
defer session.Close()
b, err := session.CombinedOutput(args.Command)
output = string(b[:])
if err != nil {
output = output + "\n" + err.Error()
success = false
}
}
}
if serverconn {
retryCounter = limit
} else {
retryCounter += 1
time.Sleep(time.Duration(3*retryCounter) * time.Second)
}
}
if !serverconn {
output = "ERROR - Cannot connect to server " + args.Config["server"]
success = false
}
resp = hexplugin.Response{
Output: output,
Success: success,
}
return resp
}
func main() {
var pluginMap = map[string]plugin.Plugin{
"action": &hexplugin.HexPlugin{Impl: &HexSsh{}},
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: hexplugin.GetHandshakeConfig(),
Plugins: pluginMap,
})
}