-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (53 loc) · 1.1 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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"time"
)
func main() {
var sshBinaryPath string
if runtime.GOOS == "windows" {
sshBinaryPath = `C:\Program Files\Git\usr\bin\ssh.exe`
} else {
sshBinaryPath = "/usr/bin/ssh"
}
if len(os.Args) > 1 && !strings.Contains(os.Args[1], "@") {
fmt.Print("Username: ")
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
username := strings.TrimSpace(scanner.Text())
if len(username) == 0 {
os.Exit(1)
}
os.Args[1] = username + "@" + os.Args[1]
} else {
fmt.Println()
os.Exit(1)
}
}
signalChannel := make(chan os.Signal)
for {
cmd := exec.Command(sshBinaryPath, os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
signal.Notify(signalChannel, os.Interrupt)
err := cmd.Run()
signal.Stop(signalChannel)
if err == nil || len(os.Args) == 1 {
break
}
if exitError, ok := err.(*exec.ExitError); ok {
if exitError.ProcessState.ExitCode() == 512 {
break
}
}
fmt.Printf("Reconnecting... (%s)\n", err)
time.Sleep(1 * time.Second)
}
}