-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcmd.go
More file actions
171 lines (139 loc) · 3.06 KB
/
cmd.go
File metadata and controls
171 lines (139 loc) · 3.06 KB
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
// Copyright (c) 2026 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package sshlib
import (
"io"
"log"
"net"
"os"
"golang.org/x/crypto/ssh"
)
// Command connect and run command over ssh.
// Output data is processed by channel because it is executed in parallel. If specification is troublesome, it is good to generate and process session from ssh package.
func (c *Connect) Command(command string) (err error) {
if c.isControlClient() {
req := controlRequest{
Type: controlRequestCommand,
Command: command,
Options: c.controlSessionOptions(c.TTY),
}
return c.runControlCommand(req)
}
// create session
if c.Session == nil {
c.Session, err = c.CreateSession()
if err != nil {
return
}
}
defer func() { c.Session = nil }()
// Set Stdin
switch {
case c.Stdin != nil:
w, _ := c.Session.StdinPipe()
go io.Copy(w, c.Stdin)
case c.PtyRelayTty != nil:
c.Session.Stdin = c.PtyRelayTty
default:
stdin := GetStdin()
c.Session.Stdin = stdin
}
// Set Stdout
switch {
case c.Stdout != nil:
or, _ := c.Session.StdoutPipe()
go io.Copy(c.Stdout, or)
case c.PtyRelayTty != nil:
c.Session.Stdout = c.PtyRelayTty
default:
c.Session.Stdout = os.Stdout
}
// Set Stderr
switch {
case c.Stderr != nil:
er, _ := c.Session.StderrPipe()
go io.Copy(c.Stderr, er)
case c.PtyRelayTty != nil:
c.Session.Stderr = c.PtyRelayTty
default:
c.Session.Stderr = os.Stderr
}
// setup options
err = c.setOption(c.Session)
if err != nil {
return
}
err = c.Session.Start(command)
if err != nil {
return
}
stopKeepAlive := c.startSessionKeepAlive(c.Session)
defer stopKeepAlive()
err = c.Session.Wait()
return
}
func (c *Connect) runControlCommand(req controlRequest) error {
resp, err := c.requestControl(req)
if err != nil {
return err
}
conn, err := net.Dial("unix", resp.StreamPath)
if err != nil {
return err
}
defer conn.Close()
writer := &lockedFrameWriter{w: conn}
stdin := io.Reader(GetStdin())
if c.Stdin != nil {
stdin = c.Stdin
} else if c.PtyRelayTty != nil {
stdin = c.PtyRelayTty
}
stdout := io.Writer(os.Stdout)
if c.Stdout != nil {
stdout = c.Stdout
} else if c.PtyRelayTty != nil {
stdout = c.PtyRelayTty
}
stderr := io.Writer(os.Stderr)
if c.Stderr != nil {
stderr = c.Stderr
} else if c.PtyRelayTty != nil {
stderr = c.PtyRelayTty
}
if c.logging {
stdout, stderr, err = c.logWriters(stdout, stderr)
if err != nil {
return err
}
}
go c.copyControlInput(writer, stdin)
if req.Options.TTY {
_ = c.sendControlWindowSize(writer)
go c.watchControlWindowSize(writer)
}
return c.copyControlOutput(conn, stdout, stderr)
}
func (c *Connect) setOption(session *ssh.Session) (err error) {
// Request tty
if c.TTY {
err = RequestTty(session)
if err != nil {
return err
}
}
// ssh agent forwarding
if c.ForwardAgent {
c.ForwardSshAgent(session)
}
// x11 forwarding
if c.ForwardX11 {
err = c.X11Forward(session)
if err != nil {
log.Println(err)
}
err = nil
}
return
}