-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (63 loc) · 1.76 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
package main
import (
"fmt"
"path/filepath"
"strings"
"golang.org/x/crypto/ssh"
"gopkg.in/ini.v1"
// "io/ioutil"
"log"
"os"
)
func main() {
ex, err := os.Executable()
if err != nil {
panic(err)
}
expath := filepath.Dir(ex)
fileconfig := expath + "/config.ini"
cfg, err := ini.Load(fileconfig)
if err != nil {
fmt.Println("Faild to read ini file: ", err)
os.Exit(1)
}
SSH_ADDRESS := cfg.Section("settings").Key("address").String() + ":22"
SSH_USERNAME := cfg.Section("settings").Key("user").String()
SSH_PASSWORD := cfg.Section("settings").Key("password").String()
SHELL_SCRIPT := cfg.Section("commands").Key("shellScript").String()
sshConfig := &ssh.ClientConfig{
User: SSH_USERNAME,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
ssh.Password(SSH_PASSWORD),
},
}
fmt.Println("auto ssh to: ", cfg.Section("settings").Key("address").String())
client, err := ssh.Dial("tcp", SSH_ADDRESS, sshConfig)
if client != nil {
defer client.Close()
}
if err != nil {
log.Fatal("Failed to dial. " + err.Error())
}
commandshell := strings.Split(SHELL_SCRIPT, ",")
for _, num := range commandshell{
session, err := client.NewSession()
if session != nil {
defer session.Close()
}
if err != nil {
log.Fatal("Failed to create session. " + err.Error())
}
session.Stdin = os.Stdin
session.Stdout = os.Stdout
session.Stderr = os.Stderr
err = session.Run(num)
if err != nil {
log.Fatal("Command execution error. " + err.Error())
}
fmt.Println("=============================================")
fmt.Println(" DONE ")
fmt.Println("=============================================")
}
}