-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_ssh.go
95 lines (76 loc) · 1.92 KB
/
exec_ssh.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
package cicd
import (
"bytes"
"log"
"os"
"strings"
"golang.org/x/crypto/ssh"
)
func CreateSSHKeysSSHConfig(username string, pubkeyfile string) *ssh.ClientConfig {
key, err := os.ReadFile(pubkeyfile)
if err != nil {
log.Fatalf("unable to read private key: %v", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatalf("unable to parse private key: %v", err)
}
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return config
}
func CreateUserPasswordSSHConfig(username string, password string) *ssh.ClientConfig {
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return config
}
func ExecSSH(host string, config *ssh.ClientConfig, command string) (string, string, int) {
client := sshDial(host, config)
if client == nil {
return "", "", 4
}
session, err := client.NewSession()
if err != nil {
return "", err.Error(), 3
}
defer session.Close()
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
if err := session.RequestPty("linux", 80, 40, modes); err != nil {
return "", err.Error(), 2
}
var stdout bytes.Buffer
var stderr bytes.Buffer
session.Stdout = &stdout
session.Stderr = &stderr
err = session.Run(command)
if err != nil {
return stdout.String(), stderr.String(), 1
}
return stdout.String(), stderr.String(), 0
}
func sshDial(host string, config *ssh.ClientConfig) *ssh.Client {
if !strings.Contains(host, ":") {
host += ":22"
}
client, err := ssh.Dial("tcp", host, config)
if err != nil {
return nil
}
return client
}