Debian Codegraph @

example/stream/stream.go

raw
1 package main
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/appleboy/easyssh-proxy"
8 )
9
10 func main() {
11 // Create MakeConfig instance with remote username, server address and path to private key.
12 ssh := &easyssh.MakeConfig{
13 Server: "localhost",
14 User: "drone-scp",
15 KeyPath: "./tests/.ssh/id_rsa",
16 Port: "22",
17 Timeout: 60 * time.Second,
18 }
19
20 // Call Run method with command you want to run on remote server.
21 stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream("for i in {1..5}; do echo ${i}; sleep 1; done; exit 2;", 60*time.Second)
22 // Handle errors
23 if err != nil {
24 panic("Can't run remote command: " + err.Error())
25 }
26 // read from the output channel until the done signal is passed
27 isTimeout := true
28 loop:
29 for {
30 select {
31 case isTimeout = <-doneChan:
32 break loop
33 case outline := <-stdoutChan:
34 fmt.Println("out:", outline)
35 case errline := <-stderrChan:
36 fmt.Println("err:", errline)
37 case err = <-errChan:
38 }
39 }
40
41 // get exit code or command error.
42 if err != nil {
43 panic("err: " + err.Error())
44 }
45
46 // command time out
47 if !isTimeout {
48 fmt.Println("Error: command timeout")
49 }
50 }