-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssh.go
67 lines (63 loc) · 1.6 KB
/
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
package main
import (
"fmt"
"github.com/weakiwi/gosshtool"
"io/ioutil"
"log"
"os"
)
func makeConnection(sc *sshconfig) (sshclient *gosshtool.SSHClient, err error) {
pkey := os.Getenv("PKEY")
if pkey == "" {
pkey = "/root/.ssh/id_rsa"
}
key, err := ioutil.ReadFile(pkey)
if err != nil {
log.Fatalf("Unable to read private key: %v", err)
return nil, err
}
pkey = string(key)
config2 := &gosshtool.SSHClientConfig{
User: sc.user,
Privatekey: pkey,
Host: sc.address,
DialTimeoutSecond: 5,
}
sshclient = gosshtool.NewSSHClient(config2)
return sshclient, nil
}
func sshexecWithoutConnect(sshclient *gosshtool.SSHClient, command string, done chan string) {
stdout, stderr, _, err := sshclient.Cmd(command, nil, nil, 0)
if err != nil {
waitgroup.Done()
log.Println("sshexecWithoutConnect sshclient.Cmd error: ", err)
done <- fmt.Sprintf(stderr)
return
}
waitgroup.Done()
done <- fmt.Sprintf("%s[%s]%s\n%s", CLR_R, sshclient.SSHClientConfig.Host, CLR_N, stdout)
return
}
func scpexecWithoutConnection(client *gosshtool.SSHClient, srcfile string, destfile string, done chan string) {
f, err := os.Open(srcfile)
if err != nil {
return
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return
}
stdout, stderr, err := client.TransferData(destfile, data)
if err != nil {
log.Printf(stderr)
}
stdout, stderr, _, err = client.Cmd("md5sum "+destfile, nil, nil, 0)
if err != nil {
waitgroup.Done()
done <- fmt.Sprintf(stderr)
}
waitgroup.Done()
done <- fmt.Sprintf("%s[%s]%s\n%s", CLR_R, client.SSHClientConfig.Host, CLR_N, stdout)
return
}