-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
47 lines (42 loc) · 999 Bytes
/
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
package main
import (
gossh "golang.org/x/crypto/ssh"
// "github.com/golang/crypto/terminal"
"net"
)
type Cli struct {
user string
pwd string
addr string
client *gossh.Client
session *gossh.Session
LastResult string
}
func (c *Cli) Connect() (*Cli, error) {
config := &gossh.ClientConfig{}
config.SetDefaults()
config.User = c.user
config.Auth = []gossh.AuthMethod{gossh.Password(c.pwd)}
config.HostKeyCallback = func(hostname string, remote net.Addr, key gossh.PublicKey) error { return nil }
client, err := gossh.Dial("tcp", c.addr, config)
if nil != err {
return c, err
}
c.client = client
return c, nil
}
func (c Cli) Run(shell string) (string, error) {
if c.client == nil {
if _, err := c.Connect(); err != nil {
return "", err
}
}
session, err := c.client.NewSession()
if err != nil {
return "", err
}
defer session.Close()
buf, err := session.CombinedOutput(shell)
c.LastResult = string(buf)
return c.LastResult, err
}