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
|
package sshpakg
import (
"bufio"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"syscall"
"github.com/shiena/ansicolor"
"golang.org/x/crypto/ssh"
)
// DialWithKey starts a client connection to the given SSH server with key authmethod.
func DialWithKey(addr, user, keyfile string) (*ssh.Client, error) {
key, err := ioutil.ReadFile(keyfile)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return ssh.Dial("tcp", addr, config)
}
// DialWithKeyWithPassphrase same as DialWithKey but with a passphrase to decrypt the private key
func DialWithKeyWithPassphrase(addr, user, keyfile string, passphrase string) (*ssh.Client, error) {
key, err := ioutil.ReadFile(keyfile)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKeyWithPassphrase(key, []byte(passphrase))
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return ssh.Dial("tcp", addr, config)
}
// DialWithPasswd starts a client connection to the given SSH server with passwd authmethod.
func DialWithPasswd(addr, user, passwd string) (*ssh.Client, error) {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(passwd),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return ssh.Dial("tcp", addr, config)
}
// InitSSHSession initial an ssh session
func InitSSHSession(conn *ssh.Client) error {
session, err := conn.NewSession()
if err != nil {
return err
}
defer session.Close()
session.Stdout = ansicolor.NewAnsiColorWriter(os.Stdout)
session.Stderr = ansicolor.NewAnsiColorWriter(os.Stderr)
sysinput, _ := session.StdinPipe()
if err := session.RequestPty("vt100", 80, 40, ssh.TerminalModes{
ssh.ECHO: 0,
ssh.IGNCR: 1,
ssh.ECHOCTL: 0,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}); err != nil {
return err
}
if err := session.Shell(); err != nil {
return err
}
ctrlC := make(chan os.Signal, 1)
signal.Notify(ctrlC, syscall.SIGINT)
go func() {
for {
<-ctrlC
fmt.Println("^C")
// for some reason OpenSSH dose not support Signal method, so ...
// i have to do this in old school way
sysinput.Write([]byte("\x03"))
}
}()
go func() {
for {
inputReader := bufio.NewReader(os.Stdin)
str, _ := inputReader.ReadString('\n')
fmt.Fprint(sysinput, str)
}
}()
// exit ...
if err := session.Wait(); err != nil {
if e, ok := err.(*ssh.ExitError); ok {
switch e.ExitStatus() {
case 130:
return nil
}
}
return err
}
return nil
}
|