-
Notifications
You must be signed in to change notification settings - Fork 4
/
winssh.go
83 lines (66 loc) · 1.83 KB
/
winssh.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
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"os/exec"
"strconv"
// "golang.org/x/crypto/ssh"
"github.com/docker/machine/libmachine/ssh"
)
var path_winssh = "C:/Windows/System32/OpenSSH/ssh.exe"
func exec_winssh(params CloudShellEnv) {
key, err := env_get_ssh_pkey()
if err != nil {
fmt.Println("\nTip: Run the command: \"gcloud alpha cloud-shell ssh --dry-run\" to setup Cloud Shell SSH keys")
return
}
sshUsername := params.SshUsername
sshHost := params.SshHost
sshPort := fmt.Sprint(params.SshPort)
sshUrl := sshUsername + "@" + sshHost
if config.Debug == true {
fmt.Println(key)
fmt.Println(sshUsername)
fmt.Println(sshHost)
fmt.Println(sshPort)
fmt.Println(sshUrl)
}
if config.Debug == true {
fmt.Println("cmd.exe /C start " + path_winssh + " " + sshUrl + " -p " + sshPort + " -i " + key)
}
cmd := exec.Command("cmd.exe", "/C", "start", path_winssh, sshUrl, "-p", sshPort, "-i", key)
err = cmd.Start()
if err != nil {
fmt.Println(err)
return
}
}
func exec_inline_ssh(params CloudShellEnv) {
key, err := env_get_ssh_pkey()
if err != nil {
fmt.Println("\nTip: Run the command: \"gcloud alpha cloud-shell ssh --dry-run\" to setup Cloud Shell SSH keys")
return
}
sshUsername := params.SshUsername
sshHost := params.SshHost
sshPort := fmt.Sprint(params.SshPort)
sshUrl := sshUsername + "@" + sshHost
if config.Debug == true {
fmt.Println(key)
fmt.Println(sshUsername)
fmt.Println(sshHost)
fmt.Println(sshPort)
fmt.Println(sshUrl)
}
auth := ssh.Auth{Keys: []string{key}}
sshPortInt, err := strconv.Atoi(sshPort)
client, err := ssh.NewClient(sshUsername, sshHost, sshPortInt, &auth)
if err != nil {
fmt.Errorf("Failed to create new client - %s", err)
return
}
err = client.Shell()
if err != nil && err.Error() != "exit status 255" {
fmt.Errorf("Failed to request shell - %s", err)
return
}
}