Source File
session.go
Belonging Package
golang.org/x/crypto/ssh
package ssh
const (
SIGABRT Signal = "ABRT"
SIGALRM Signal = "ALRM"
SIGFPE Signal = "FPE"
SIGHUP Signal = "HUP"
SIGILL Signal = "ILL"
SIGINT Signal = "INT"
SIGKILL Signal = "KILL"
SIGPIPE Signal = "PIPE"
SIGQUIT Signal = "QUIT"
SIGSEGV Signal = "SEGV"
SIGTERM Signal = "TERM"
SIGUSR1 Signal = "USR1"
SIGUSR2 Signal = "USR2"
)
var signals = map[Signal]int{
SIGABRT: 6,
SIGALRM: 14,
SIGFPE: 8,
SIGHUP: 1,
SIGILL: 4,
SIGINT: 2,
SIGKILL: 9,
SIGPIPE: 13,
SIGQUIT: 3,
SIGSEGV: 11,
SIGTERM: 15,
}
type TerminalModes map[uint8]uint32
const (
tty_OP_END = 0
VINTR = 1
VQUIT = 2
VERASE = 3
VKILL = 4
VEOF = 5
VEOL = 6
VEOL2 = 7
VSTART = 8
VSTOP = 9
VSUSP = 10
VDSUSP = 11
VREPRINT = 12
VWERASE = 13
VLNEXT = 14
VFLUSH = 15
VSWTCH = 16
VSTATUS = 17
VDISCARD = 18
IGNPAR = 30
PARMRK = 31
INPCK = 32
ISTRIP = 33
INLCR = 34
IGNCR = 35
ICRNL = 36
IUCLC = 37
IXON = 38
IXANY = 39
IXOFF = 40
IMAXBEL = 41
ISIG = 50
ICANON = 51
XCASE = 52
ECHO = 53
ECHOE = 54
ECHOK = 55
ECHONL = 56
NOFLSH = 57
TOSTOP = 58
IEXTEN = 59
ECHOCTL = 60
ECHOKE = 61
PENDIN = 62
OPOST = 70
OLCUC = 71
ONLCR = 72
OCRNL = 73
ONOCR = 74
ONLRET = 75
CS7 = 90
CS8 = 91
PARENB = 92
PARODD = 93
TTY_OP_ISPEED = 128
TTY_OP_OSPEED = 129
)
stdinPipeWriter io.WriteCloser
exitStatus chan error
}
type setenvRequest struct {
Name string
Value string
}
func ( *Session) ( string, , int, TerminalModes) error {
var []byte
for , := range {
:= struct {
byte
uint32
}{, }
= append(, Marshal(&)...)
}
= append(, tty_OP_END)
:= ptyRequestMsg{
Term: ,
Columns: uint32(),
Rows: uint32(),
Width: uint32( * 8),
Height: uint32( * 8),
Modelist: string(),
}
, := .ch.SendRequest("pty-req", true, Marshal(&))
if == nil && ! {
= errors.New("ssh: pty-req failed")
}
return
}
type subsystemRequestMsg struct {
Subsystem string
}
func ( *Session) ( string) ([]byte, error) {
if .Stdout != nil {
return nil, errors.New("ssh: Stdout already set")
}
var bytes.Buffer
.Stdout = &
:= .Run()
return .Bytes(),
}
type singleWriter struct {
b bytes.Buffer
mu sync.Mutex
}
func ( *singleWriter) ( []byte) (int, error) {
.mu.Lock()
defer .mu.Unlock()
return .b.Write()
}
func ( *Session) () error {
if .started {
return errors.New("ssh: session already started")
}
, := .ch.SendRequest("shell", true, nil)
if == nil && ! {
return errors.New("ssh: could not start shell")
}
if != nil {
return
}
return .start()
}
func ( *Session) () error {
.started = true
type func(*Session)
for , := range []{(*Session).stdin, (*Session).stdout, (*Session).stderr} {
()
}
.errors = make(chan error, len(.copyFuncs))
for , := range .copyFuncs {
go func( func() error) {
.errors <- ()
}()
}
return nil
}
func ( *Session) () error {
if !.started {
return errors.New("ssh: session not started")
}
:= <-.exitStatus
if .stdinPipeWriter != nil {
.stdinPipeWriter.Close()
}
var error
for range .copyFuncs {
if := <-.errors; != nil && == nil {
=
}
}
if != nil {
return
}
return
}
func ( *Session) ( <-chan *Request) error {
type ExitMissingError struct{}
func ( *ExitMissingError) () string {
return "wait: remote command exited without exit status or exit signal"
}
func ( *Session) () {
if .stdinpipe {
return
}
var io.Reader
if .Stdin == nil {
= new(bytes.Buffer)
} else {
, := io.Pipe()
go func() {
, := io.Copy(, .Stdin)
.CloseWithError()
}()
, .stdinPipeWriter = ,
}
.copyFuncs = append(.copyFuncs, func() error {
, := io.Copy(.ch, )
if := .ch.CloseWrite(); == nil && != io.EOF {
=
}
return
})
}
func ( *Session) () {
if .stdoutpipe {
return
}
if .Stdout == nil {
.Stdout = ioutil.Discard
}
.copyFuncs = append(.copyFuncs, func() error {
, := io.Copy(.Stdout, .ch)
return
})
}
func ( *Session) () {
if .stderrpipe {
return
}
if .Stderr == nil {
.Stderr = ioutil.Discard
}
.copyFuncs = append(.copyFuncs, func() error {
, := io.Copy(.Stderr, .ch.Stderr())
return
})
}
type sessionStdin struct {
io.Writer
ch Channel
}
func ( *sessionStdin) () error {
return .ch.CloseWrite()
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |