-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
54 lines (49 loc) · 1.15 KB
/
client.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
package sshtunnel
import (
"fmt"
"github.com/ssst0n3/awesome_libs/awesome_error"
"golang.org/x/crypto/ssh"
)
type Config struct {
Ip string `json:"ip"`
Port uint `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
}
type Client struct {
Config Config
Client *ssh.Client
}
func GetClient(config Config) (client Client, err error) {
client = Client{
Config: config,
}
sshConfig := &ssh.ClientConfig{
User: config.Username,
Auth: []ssh.AuthMethod{
ssh.Password(config.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
//HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", config.Ip, config.Port), sshConfig)
if err != nil {
awesome_error.CheckErr(err)
return
}
client.Client = conn
return
}
func (c Client) Execute(cmd string) (output []byte, err error) {
session, err := c.Client.NewSession()
if err != nil {
awesome_error.CheckErr(err)
return
}
output, err = session.CombinedOutput(cmd)
if err != nil {
awesome_error.CheckErr(err)
return
}
return
}