-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshclient.go
More file actions
150 lines (128 loc) · 2.56 KB
/
sshclient.go
File metadata and controls
150 lines (128 loc) · 2.56 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package sshclient
import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
)
type SshClient struct {
User string
Server string
Key string
Port string
Password string
AuthType string
sclient *ssh.Client
}
func NewSshClient(server, user string) *SshClient {
sc := SshClient{
User: user,
Server: server,
Port: "22",
AuthType: "PublicKey",
Key: "/.ssh/id_rsa",
}
return &sc
}
func getKeyFile(keypath string) (ssh.Signer, error) {
usr, err := user.Current()
if err != nil {
return nil, err
}
file := usr.HomeDir + keypath
buf, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
pubkey, err := ssh.ParsePrivateKey(buf)
if err != nil {
return nil, err
}
return pubkey, nil
}
func (client *SshClient) connect() error {
pubkey, err := getKeyFile(client.Key)
if err != nil {
return err
}
config := &ssh.ClientConfig{User: client.User}
if client.AuthType == "Password" {
config.Auth = []ssh.AuthMethod{ssh.Password(client.Password)}
} else {
config.Auth = []ssh.AuthMethod{ssh.PublicKeys(pubkey)}
}
sc, err := ssh.Dial("tcp", client.Server+":"+client.Port, config)
if err != nil {
return err
}
client.sclient = sc
return nil
}
func (client *SshClient) session() (*ssh.Session, error) {
if client.sclient == nil {
if err := client.connect(); err != nil {
return nil, err
}
}
sc := client.sclient
session, err := sc.NewSession()
if err != nil {
return nil, err
}
return session, nil
}
func (client *SshClient) Run(command string) (string, error) {
session, err := client.session()
if err != nil {
return "", err
}
defer session.Close()
var b bytes.Buffer
session.Stdout = &b
err = session.Run(command)
if err != nil {
return "", err
}
return b.String(), nil
}
func (client *SshClient) Scp(sourceFile string) error {
session, err := client.session()
if err != nil {
return err
}
defer session.Close()
targetFile := filepath.Base(sourceFile)
src, srcErr := os.Open(sourceFile)
if srcErr != nil {
return srcErr
}
srcStat, statErr := src.Stat()
if statErr != nil {
return statErr
}
go func() {
w, _ := session.StdinPipe()
fmt.Fprintln(w, "C0644", srcStat.Size(), targetFile)
if srcStat.Size() > 0 {
io.Copy(w, src)
fmt.Fprint(w, "\x00")
w.Close()
} else {
fmt.Fprint(w, "\x00")
w.Close()
}
}()
if err := session.Run(fmt.Sprintf("scp -t %s", targetFile)); err != nil {
return err
}
return nil
}
func (client *SshClient) Close() {
if client.sclient != nil {
client.sclient.Close()
}
}