/
remote_exec.go
68 lines (59 loc) · 1.32 KB
/
remote_exec.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
/*
* Created: 2020-04-11 16:51:05
* Author : Win-Man
* Email : gang.shen0423@gmail.com
* -----
* Last Modified:
* Modified By:
* -----
* Description:
*/
package linux
import (
"fmt"
"log"
"time"
"golang.org/x/crypto/ssh"
)
type Myssh struct {
Host string
User string
Password string
Port int
Type string
KeyPath string
ConnectTimeout int64
MyClient *ssh.Client
MySession *ssh.Session
}
func (this *Myssh) ExecCommand(cmd string) (res []byte, err error) {
config := &ssh.ClientConfig{
Timeout: time.Second,
User: this.User,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
if this.Type == "password" {
config.Auth = []ssh.AuthMethod{ssh.Password(this.Password)}
} else {
log.Fatalf("Auth failed %s", this.Type)
}
// dial
addr := fmt.Sprintf("%s:%d", this.Host, this.Port)
this.MyClient, err = ssh.Dial("tcp", addr, config)
if err != nil {
log.Fatal("Create ssh client failed", err)
}
defer this.MyClient.Close()
// create session
this.MySession, err = this.MyClient.NewSession()
if err != nil {
log.Fatal("Create ssh session failed", err)
}
defer this.MySession.Close()
// exec command
res, err = this.MySession.CombinedOutput(cmd)
if err != nil {
log.Fatal("Exec command failed", err)
}
return res, err
}