-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathssh-cmd.go
More file actions
64 lines (59 loc) · 1.58 KB
/
ssh-cmd.go
File metadata and controls
64 lines (59 loc) · 1.58 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
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/appleboy/easyssh-proxy"
"github.com/ogier/pflag"
)
func main() {
// Create MakeConfig instance with remote username, server address and path to private key.
server := pflag.StringP("server", "s", "localhost", "Server to SSH To.")
scriptFile := pflag.StringP("script", "S", "", "Script to run on remote server")
username := pflag.StringP("user", "u", os.Getenv("USER"), "Username")
keyfile := pflag.StringP("key", "k", os.Getenv("HOME")+"/.ssh/id_rsa", "SSH Key")
help := pflag.BoolP("help", "h", false, "Help")
pflag.Parse()
if *help {
pflag.PrintDefaults()
os.Exit(1)
}
ssh := &easyssh.MakeConfig{
Server: *server,
User: *username,
KeyPath: *keyfile,
Port: "22",
Timeout: 60 * time.Second,
}
content, err := ioutil.ReadFile(*scriptFile)
// Call Run method with command you want to run on remote server.
stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(string(content), 60*time.Second)
// 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")
}
}
}