-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathssh_execute.go
173 lines (145 loc) · 3.76 KB
/
ssh_execute.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
164
165
166
167
168
169
170
171
172
173
package terminal
import (
"bytes"
"flag"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"golang.org/x/net/websocket"
)
func linuxSSH(ws *websocket.Conn, args []string, charset, wd string, timeout time.Duration) {
log.Println("begin to execute ssh:", args)
// [ssh -batch -pw 8498b2c7 [email protected] -m /var/lib/tpt/etc/scripts/abc.sh]
flagSet := flag.NewFlagSet("test", flag.ContinueOnError)
_ = flagSet.Bool("batch", false, "")
pw := flagSet.String("pw", "", "")
idFile := flagSet.String("i", "", "")
if err := flagSet.Parse(args); err != nil {
io.WriteString(ws, "parse arguments error: "+err.Error())
return
}
if len(flagSet.Args()) == 0 {
io.WriteString(ws, "parse arguments error: command is missing")
return
}
if args = flagSet.Args(); len(args) == 3 && args[1] == "-m" {
bs, err := ioutil.ReadFile(args[2])
if err != nil {
io.WriteString(ws, "parse arguments error: command is missing")
return
}
bs = bytes.TrimSpace(bs)
if len(bs) == 0 {
io.WriteString(ws, args[2]+" is empty")
return
}
args = []string{args[0], string(bs)}
}
if *idFile != "" {
args = append([]string{"-i", *idFile, "-o", "StrictHostKeyChecking=no"}, args...)
} else {
args = append([]string{"-o", "StrictHostKeyChecking=no"}, args...)
}
var output io.Writer = decodeBy(charset, ws)
var cmd *exec.Cmd
if *pw != "" {
cmd = exec.Command("sshpass", append([]string{"-p", *pw, "ssh"}, args...)...)
} else {
cmd = exec.Command("ssh", args...)
}
if "" != wd {
cmd.Dir = wd
}
cmd.Stdin = ws
cmd.Stderr = output
cmd.Stdout = output
if err := cmd.Start(); err != nil {
io.WriteString(ws, err.Error())
return
}
go func() {
defer recover()
cmd.Process.Wait()
ws.Close()
}()
timer := time.AfterFunc(timeout, func() {
defer recover()
cmd.Process.Kill()
})
if err := cmd.Wait(); err != nil {
io.WriteString(ws, err.Error())
}
timer.Stop()
ws.Close()
}
func Plink(ws *websocket.Conn) {
defer ws.Close()
hostname := ws.Request().URL.Query().Get("hostname")
port := ws.Request().URL.Query().Get("port")
if port != "" {
hostname = net.JoinHostPort(hostname, port)
}
user := ws.Request().URL.Query().Get("user")
pwd := ws.Request().URL.Query().Get("password")
// columns := toInt(ws.Request().URL.Query().Get("columns"), 120)
// rows := toInt(ws.Request().URL.Query().Get("rows"), 80)
charset := ws.Request().URL.Query().Get("charset")
if "" == charset {
if "windows" == runtime.GOOS {
charset = "GB18030"
} else {
charset = "UTF-8"
}
}
pa := "plink"
if c, ok := Commands[pa]; ok {
pa = c
}
cmd := exec.Command(pa, "-pw", pwd, user+"@"+hostname)
var combinedOut io.Writer = decodeBy(charset, ws)
cmd.Stdout = combinedOut
cmd.Stderr = combinedOut
cmd.Stdin = ws
if *is_debug || "true" == strings.ToLower(ws.Request().URL.Query().Get("debug")) {
dump_out, err := os.OpenFile(filepath.Join(LogDir, strings.Replace(hostname, ":", "_", -1)+".dump_ssh_out.txt"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if nil == err {
combinedOut = io.MultiWriter(dump_out, combinedOut)
} else {
defer dump_out.Close()
}
dump_in, err := os.OpenFile(filepath.Join(LogDir, strings.Replace(hostname, ":", "_", -1)+".dump_ssh_in.txt"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if nil != err {
dump_in = nil
} else {
defer dump_in.Close()
}
cmd.Stdout = combinedOut
cmd.Stderr = combinedOut
cmd.Stdin = warp(ws, dump_in)
}
if err := cmd.Start(); err != nil {
io.WriteString(ws, err.Error())
return
}
go func() {
defer recover()
cmd.Process.Wait()
ws.Close()
}()
timer := time.AfterFunc(1*time.Hour, func() {
defer recover()
cmd.Process.Kill()
})
if err := cmd.Wait(); err != nil {
io.WriteString(ws, err.Error())
}
timer.Stop()
ws.Close()
}