-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (149 loc) · 3.57 KB
/
main.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
"io"
"io/ioutil"
"os"
"time"
)
type Cli struct {
user string
pwd string // TODO 目前是明文存储,如何加密?
ip string
port string
sshClient *ssh.Client
sftpClient *sftp.Client
}
func NewSSHClient(user, pwd, ip, port string) Cli {
return Cli{
user: user,
pwd: pwd,
ip: ip,
port: port,
}
}
// 不使用 HostKey, 使用密码
func (c *Cli) getConfig_nokey() *ssh.ClientConfig {
config := &ssh.ClientConfig{
User: c.user,
Auth: []ssh.AuthMethod{
ssh.Password(c.pwd),
},
Timeout: 30 * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return config
}
func (c *Cli) Connect() error {
config := c.getConfig_nokey()
client, err := ssh.Dial("tcp", c.ip+":"+c.port, config)
if err != nil {
return fmt.Errorf("connect server error: %w", err)
}
sftp, err := sftp.NewClient(client)
if err != nil {
return fmt.Errorf("new sftp client error: %w", err)
}
c.sshClient = client
c.sftpClient = sftp
return nil
}
func (c *Cli) Run(cmd string) (string, error) {
if c.sshClient == nil {
if err := c.Connect(); err != nil {
return "", err
}
}
session, err := c.sshClient.NewSession()
if err != nil {
return "", fmt.Errorf("create new session error: %w", err)
}
defer session.Close()
buf, err := session.CombinedOutput(cmd)
return string(buf), err
}
func (c *Cli) DownloadFile(remoteFile, localFile string) (int, error) {
if c.sshClient == nil {
if err := c.Connect(); err != nil {
return -1, err
}
}
source, err := c.sftpClient.Open(remoteFile)
if err != nil {
return -1, fmt.Errorf("sftp client open file error: %w", err)
}
defer source.Close()
target, err := os.OpenFile(localFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return -1, fmt.Errorf("open local file error: %w", err)
}
defer target.Close()
n, err := io.Copy(target, source)
if err != nil {
return -1, fmt.Errorf("write file error: %w", err)
}
return int(n), nil
}
func (c *Cli) UploadFile(localFile, remoteFileName string) (int, error) {
if c.sshClient == nil {
if err := c.Connect(); err != nil {
return -1, err
}
}
file, err := os.Open(localFile)
if nil != err {
return -1, fmt.Errorf("open local file failed: %w", err)
}
defer file.Close()
ftpFile, err := c.sftpClient.Create(remoteFileName)
if nil != err {
return -1, fmt.Errorf("Create remote path failed: %w", err)
}
defer ftpFile.Close()
fileByte, err := ioutil.ReadAll(file)
if nil != err {
return -1, fmt.Errorf("read local file failed: %w", err)
}
ftpFile.Write(fileByte)
return 0, nil
}
func (c *Cli) RunTerminal(shell string, stdout, stderr io.Writer) error {
if c.sshClient == nil {
if err := c.Connect(); err != nil {
return err
}
}
session, err := c.sshClient.NewSession()
if err != nil {
return err
}
defer session.Close()
fd := int(os.Stdin.Fd())
oldState, err := terminal.MakeRaw(fd)
if err != nil {
panic(err)
}
defer terminal.Restore(fd, oldState)
session.Stdout = stdout
session.Stderr = stderr
session.Stdin = os.Stdin
termWidth, termHeight, err := terminal.GetSize(fd)
if err != nil {
panic(err)
}
// Set up terminal modes
modes := ssh.TerminalModes{
ssh.ECHO: 1, // enable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := session.RequestPty("xterm-256color", termHeight, termWidth, modes); err != nil {
return err
}
session.Run(shell)
return nil
}