/
ssh.go
97 lines (80 loc) · 2.38 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
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
// Deprecated package in favour of simple fork ssh client on host
package ssh
import (
"fmt"
"net"
"os"
"time"
"github.com/pabateman/kubectl-nsenter/internal/config"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/term"
)
func ExecSSHCommand(cfg *config.Config, cmd string) error {
sshSession, err := getSSHSession(cfg)
if err != nil {
return fmt.Errorf("failed to build ssh session: %v", err)
}
// nolint:errcheck
defer sshSession.Close()
previousState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return errors.Wrap(err, "failed to make tty")
}
// nolint:errcheck
defer term.Restore(int(os.Stdin.Fd()), previousState)
ttyWidth, ttyHeight, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
return errors.Wrap(err, "failed to get tty size")
}
modes := ssh.TerminalModes{
ssh.ECHO: 1,
}
err = sshSession.RequestPty("xterm", ttyHeight, ttyWidth, modes)
if err != nil {
return errors.Wrap(err, "failed to request pty")
}
sshSession.Stdout = os.Stdout
sshSession.Stderr = os.Stderr
sshSession.Stdin = os.Stdin
err = sshSession.Run(cmd)
if err != nil {
return fmt.Errorf("remote shell exited with non zero code: %v", err)
}
return nil
}
func requestPassword(user, host string) (string, error) {
fmt.Printf("%s@%s's password: ", user, host)
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", errors.New("failed to read password")
}
return string(password), nil
}
func getSSHSession(cfg *config.Config) (*ssh.Session, error) {
sshConfig := &ssh.ClientConfig{
User: cfg.SSHUser,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 10 * time.Second,
}
if cfg.SSHRequirePassword {
password, err := requestPassword(cfg.SSHUser, cfg.SSHHost)
if err != nil {
return nil, errors.New("failed to request password")
}
sshConfig.Auth = []ssh.AuthMethod{ssh.Password(password)}
} else {
agentConnection, err := net.Dial("unix", cfg.SSHSocketPath)
if err != nil {
return nil, err
}
sshConfig.Auth = []ssh.AuthMethod{ssh.PublicKeysCallback(agent.NewClient(agentConnection).Signers)}
}
sshHost := net.JoinHostPort(cfg.SSHHost, cfg.SSHPort)
sshClient, err := ssh.Dial("tcp", sshHost, sshConfig)
if err != nil {
return nil, errors.WithMessagef(err, "can't dial node %s@%s\n", cfg.SSHUser, cfg.SSHHost)
}
return sshClient.NewSession()
}