forked from kkirsche/go-scp
/
sshClient.go
92 lines (80 loc) · 2.18 KB
/
sshClient.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package goScp
import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"io/ioutil"
"log"
"net"
"os"
)
func getAgent() (agent.Agent, error) {
agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
return agent.NewClient(agentConn), err
}
func withAgentSshConfig(username string) *ssh.ClientConfig {
agent, err := getAgent()
if err != nil {
log.Println("Failed to connect to SSH_AUTH_SOCK:", err)
os.Exit(1)
}
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agent.Signers),
},
}
return config
}
func withoutAgentSshConfig(username string, sshKeyFile SshKeyfile) *ssh.ClientConfig {
keyFilePath := fmt.Sprintf("%s/%s", sshKeyFile.Path, sshKeyFile.Filename)
keyFileContents, err := ioutil.ReadFile(keyFilePath)
if err != nil {
log.Print(err)
os.Exit(1)
}
signer, err := ssh.ParsePrivateKey(keyFileContents)
if err != nil {
log.Print(err)
os.Exit(1)
}
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}
return config
}
func Connect(sshKeyFile SshKeyfile, sshCredentials SshCredentials, remoteMachine RemoteMachine, usingSshAgent bool) (*ssh.Client, error) {
// An SSH client is represented with a ClientConn.
//
// To authenticate with the remote server you must pass at least one
// implementation of AuthMethod via the Auth field in ClientConfig.
var config *ssh.ClientConfig
if usingSshAgent {
config = withAgentSshConfig(sshCredentials.Username)
} else {
config = withoutAgentSshConfig(sshCredentials.Username, sshKeyFile)
}
client, err := ssh.Dial("tcp", remoteMachine.Host+":"+remoteMachine.Port, config)
return client, err
}
func ExecuteCommand(client ssh.Client, cmd string) (string, error) {
// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
session, err := client.NewSession()
if err != nil {
return "", err
}
defer session.Close()
// Once a Session is created, you can execute a single command on
// the remote side using the Run method.
var b bytes.Buffer
session.Stdout = &b
if err := session.Run(cmd); err != nil {
return "", err
}
return b.String(), nil
}