/
RemoteJobExecutor.go
185 lines (171 loc) · 4.12 KB
/
RemoteJobExecutor.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"net"
"os"
"strings"
"sync"
"time"
"golang.org/x/crypto/ssh"
)
const hostsFileName = "hosts.conf"
const defaultUser = "root"
const defaultPass = "root"
const defaultCmd = "shutdown -h now"
var timeout, _ = time.ParseDuration("5s")
// Host information wrapper
type Host struct {
Name string
IP string
User string
Password string
CMD string
}
func (h *Host) String() string {
return fmt.Sprintf("Name: %s, IP: %s, User: %s, Password: %s, CMD: %s", h.Name, h.IP, h.User, h.Password, h.CMD)
}
// NewHost create a new host with default user, pass and command
func NewHost(name, ip string) *Host {
return &Host{name, ip, defaultUser, defaultPass, defaultCmd}
}
// NewHostFromRecord create a host from a comma string array
func NewHostFromRecord(record []string) (*Host, error) {
items := len(record)
if record == nil || items < 2 {
return nil, fmt.Errorf("Empty or not valid record: %s", record)
}
host := NewHost(strings.TrimSpace(record[0]), strings.TrimSpace(record[1]))
if items >= 3 {
host.User = strings.TrimSpace(record[2])
}
if items >= 4 {
host.Password = strings.TrimSpace(record[3])
}
if items >= 5 {
host.CMD = strings.TrimSpace(strings.Join(record[4:], ","))
}
return host, nil
}
func check(e error) {
if e != nil {
panic(e)
}
}
// ReadFromFile read conf files and create hosts
func ReadFromFile(filePath *string) []*Host {
file, err := os.Open(*filePath)
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
hosts := make([]*Host, 0)
for scanner.Scan() {
record := strings.TrimSpace(scanner.Text())
// skip comments
if strings.HasPrefix(record, "#") {
continue
}
records := strings.Split(record, ",")
host, err := NewHostFromRecord(records)
if err == nil {
hosts = append(hosts, host)
}
}
return hosts
}
// ExecuteCmd connect to remote host and execute the command
func ExecuteCmd(host *Host, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
sshConfig := &ssh.ClientConfig{
User: host.User,
Auth: []ssh.AuthMethod{
ssh.Password(host.Password),
ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
answers := make([]string, len(questions))
for i := range answers {
answers[i] = host.Password
}
return answers, nil
}),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: timeout,
}
sshConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host.IP, 22), sshConfig)
if err != nil {
fmt.Printf("%s> %s\n", host.IP, err)
return
}
session, err := client.NewSession()
if err != nil {
fmt.Printf("%s> %s\n", host.IP, err)
client.Close()
return
}
session.Run(host.CMD)
fmt.Printf("Executed: %s (%s) [%s]\n", host.Name, host.IP, host.CMD)
session.Close()
}
func externalIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}
func main() {
hostFile := flag.String("f", hostsFileName, "The conf file")
flag.Parse()
hosts := ReadFromFile(hostFile)
var wg sync.WaitGroup
var localhost *Host = nil
localip, err := externalIP()
for _, host := range hosts {
if err == nil && (host.IP == localip || host.IP == "localhost" || host.IP == "0.0.0.0" || host.IP == "127.0.0.1") {
localhost = host
continue
}
wg.Add(1)
go ExecuteCmd(host, &wg)
}
wg.Wait()
if localhost != nil {
fmt.Println("Executing on localhost...")
ExecuteCmd(localhost, nil)
}
fmt.Println("All task executed.")
}