upgrade easyssh to 1.1.6 (#81)

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-05-16 15:45:00 +08:00 committed by GitHub
parent 26b3d47ee2
commit b5b13e8b72
4 changed files with 84 additions and 28 deletions

View File

@ -92,12 +92,12 @@ func (p Plugin) Exec() error {
errChannel <- err
} else {
// read from the output channel until the done signal is passed
stillGoing := true
isTimeout := true
for stillGoing {
loop:
for {
select {
case isTimeout = <-doneChan:
stillGoing = false
break loop
case outline := <-stdoutChan:
p.log(host, "out:", outline)
case errline := <-stderrChan:

View File

@ -1,6 +1,11 @@
# easyssh-proxy
[![GoDoc](https://godoc.org/github.com/appleboy/easyssh-proxy?status.svg)](https://godoc.org/github.com/appleboy/easyssh-proxy) [![Build Status](http://drone.wu-boy.com/api/badges/appleboy/easyssh-proxy/status.svg)](http://drone.wu-boy.com/appleboy/easyssh-proxy) [![codecov](https://codecov.io/gh/appleboy/easyssh-proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/easyssh-proxy) [![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/easyssh-proxy)](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy) [![Sourcegraph](https://sourcegraph.com/github.com/appleboy/easyssh-proxy/-/badge.svg)](https://sourcegraph.com/github.com/appleboy/easyssh-proxy?badge)
[![GoDoc](https://godoc.org/github.com/appleboy/easyssh-proxy?status.svg)](https://godoc.org/github.com/appleboy/easyssh-proxy)
[![Build Status](http://drone.wu-boy.com/api/badges/appleboy/easyssh-proxy/status.svg)](http://drone.wu-boy.com/appleboy/easyssh-proxy)
[![codecov](https://codecov.io/gh/appleboy/easyssh-proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/easyssh-proxy)
[![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/easyssh-proxy)](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy)
[![Sourcegraph](https://sourcegraph.com/github.com/appleboy/easyssh-proxy/-/badge.svg)](https://sourcegraph.com/github.com/appleboy/easyssh-proxy?badge)
[![Release](https://github-release-version.herokuapp.com/github/appleboy/easyssh-proxy/release.svg?style=flat)](https://github.com/appleboy/easyssh-proxy/releases/latest)
easyssh-proxy provides a simple implementation of some SSH protocol features in Go.
@ -124,3 +129,53 @@ See [example/proxy/proxy.go](./example/proxy/proxy.go)
},
}
```
### SSH Stream Log
See [example/stream/stream.go](./example/stream/stream.go)
[embedmd]:# (example/stream/stream.go go /func/ /^}$/)
```go
func main() {
// Create MakeConfig instance with remote username, server address and path to private key.
ssh := &easyssh.MakeConfig{
Server: "localhost",
User: "drone-scp",
KeyPath: "./tests/.ssh/id_rsa",
Port: "22",
Timeout: 60 * time.Second,
}
// Call Run method with command you want to run on remote server.
stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream("for i in {1..5}; do echo ${i}; sleep 1; done; exit 2;", 60)
// Handle errors
if err != nil {
panic("Can't run remote command: " + err.Error())
} else {
// read from the output channel until the done signal is passed
isTimeout := true
loop:
for {
select {
case isTimeout = <-doneChan:
break loop
case outline := <-stdoutChan:
fmt.Println("out:", outline)
case errline := <-stderrChan:
fmt.Println("err:", errline)
case err = <-errChan:
}
}
// get exit code or command error.
if err != nil {
fmt.Println("err: " + err.Error())
}
// command time out
if !isTimeout {
fmt.Println("Error: command timeout")
}
}
}
```

View File

@ -156,25 +156,31 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
// Stream returns one channel that combines the stdout and stderr of the command
// as it is run on the remote machine, and another that sends true when the
// command is done. The sessions and channels will then be closed.
func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan string, stderr chan string, done chan bool, errChan chan error, err error) {
func (ssh_conf *MakeConfig) Stream(command string, timeout int) (<-chan string, <-chan string, <-chan bool, <-chan error, error) {
// continuously send the command's output over the channel
stdoutChan := make(chan string)
stderrChan := make(chan string)
doneChan := make(chan bool)
errChan := make(chan error)
// connect to remote host
session, err := ssh_conf.connect()
if err != nil {
return stdout, stderr, done, errChan, err
return stdoutChan, stderrChan, doneChan, errChan, err
}
// defer session.Close()
// connect to both outputs (they are of type io.Reader)
outReader, err := session.StdoutPipe()
if err != nil {
return stdout, stderr, done, errChan, err
return stdoutChan, stderrChan, doneChan, errChan, err
}
errReader, err := session.StderrPipe()
if err != nil {
return stdout, stderr, done, errChan, err
return stdoutChan, stderrChan, doneChan, errChan, err
}
err = session.Start(command)
if err != nil {
return stdout, stderr, done, errChan, err
return stdoutChan, stderrChan, doneChan, errChan, err
}
// combine outputs, create a line-by-line scanner
@ -182,16 +188,11 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan str
stderrReader := io.MultiReader(errReader)
stdoutScanner := bufio.NewScanner(stdoutReader)
stderrScanner := bufio.NewScanner(stderrReader)
// continuously send the command's output over the channel
stdoutChan := make(chan string)
stderrChan := make(chan string)
done = make(chan bool)
errChan = make(chan error)
go func(stdoutScanner, stderrScanner *bufio.Scanner, stdoutChan, stderrChan chan string, done chan bool, errChan chan error) {
go func(stdoutScanner, stderrScanner *bufio.Scanner, stdoutChan, stderrChan chan string, doneChan chan bool, errChan chan error) {
defer close(stdoutChan)
defer close(stderrChan)
defer close(done)
defer close(doneChan)
defer close(errChan)
defer session.Close()
@ -212,15 +213,15 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan str
select {
case <-res:
errChan <- session.Wait()
done <- true
doneChan <- true
case <-timeoutChan:
stderrChan <- "Run Command Timeout!"
errChan <- nil
done <- false
doneChan <- false
}
}(stdoutScanner, stderrScanner, stdoutChan, stderrChan, done, errChan)
}(stdoutScanner, stderrScanner, stdoutChan, stderrChan, doneChan, errChan)
return stdoutChan, stderrChan, done, errChan, err
return stdoutChan, stderrChan, doneChan, errChan, err
}
// Run command on remote machine and returns its stdout as a string
@ -230,11 +231,11 @@ func (ssh_conf *MakeConfig) Run(command string, timeout int) (outStr string, err
return outStr, errStr, isTimeout, err
}
// read from the output channel until the done signal is passed
stillGoing := true
for stillGoing {
loop:
for {
select {
case isTimeout = <-doneChan:
stillGoing = false
break loop
case outline := <-stdoutChan:
if outline != "" {
outStr += outline + "\n"

10
vendor/vendor.json vendored
View File

@ -3,12 +3,12 @@
"ignore": "test",
"package": [
{
"checksumSHA1": "YgrgNVNBf7Ro0f3KuiHewOCHrwo=",
"checksumSHA1": "EcF7T9tPEMMJfuRdPBB3NdRUg4c=",
"path": "github.com/appleboy/easyssh-proxy",
"revision": "14882d1d04ac6a85700586997695fcd936470e86",
"revisionTime": "2017-05-11T07:07:30Z",
"version": "1.1.5",
"versionExact": "1.1.5"
"revision": "33d87eae3a018c3312e32cc4eb4578d5a563aabd",
"revisionTime": "2017-05-16T07:22:25Z",
"version": "1.1.6",
"versionExact": "1.1.6"
},
{
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",