-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (56 loc) · 1.13 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/ibice/go-rsh"
)
var (
port = flag.Uint("p", 22222, "server port")
addr = flag.String("a", "127.0.0.1", "server address")
remoteExitCode = flag.Bool("e", false, "use exit code of remote process")
command string
args []string
)
func parseArgs() {
flag.Parse()
if port == nil || *port == 0 {
log.Fatal("-p is required")
}
if *port > 65535 {
log.Fatal("Invalid port: ")
}
if addr == nil || *addr == "" {
log.Fatal("-a is required")
}
// Parse remote command arguments
var argsAfterDash []string
for i, arg := range os.Args {
if arg == "--" {
argsAfterDash = os.Args[i+1:]
break
}
}
if len(argsAfterDash) > 0 {
command = argsAfterDash[0]
if len(argsAfterDash) > 1 {
args = argsAfterDash[1:]
}
}
}
func main() {
parseArgs()
client := rsh.NewClientInsecure(fmt.Sprintf("%s:%d", *addr, *port))
opts := &rsh.ExecOptions{
Command: command,
Args: args,
}
exitCode, err := client.Exec(opts)
if err != nil {
log.Fatalf("Exec: %v", err)
}
if exitCode != nil && *remoteExitCode {
os.Exit(*exitCode)
}
}