-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (83 loc) · 2.78 KB
/
main.go
File metadata and controls
104 lines (83 loc) · 2.78 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"golang.org/x/crypto/ssh"
)
var (
versionFlag = flag.Bool("version", false, "show version")
vmidFlag = flag.String("vmid", "", "hyper-v VM ID (GUID or localhost) or Context ID (UINT or one of local, hypervisor, host)")
sshUsernameFlag = flag.String("username", "vagrant", "ssh username")
sshPasswordFlag = flag.String("password", "", "ssh password")
sshPortFlag = flag.Uint("port", 22, "ssh port")
sshServiceIDFlag = flag.String("service-id", "", "the destination hyper-v socket Service ID (GUID).\nwhen this is used, the -port parameter is ignored.")
commandStdinFlag = flag.String("stdin", "", "data to pass into the command stdin")
commandFlag = flag.String("command", "ps -efww --forest", "command to execute")
version = "0.0.0-dev"
revision = "0000000000000000000000000000000000000000"
)
func main() {
log.SetOutput(os.Stdout) // for not disturbing PowerShell...
flag.Parse()
if *versionFlag {
fmt.Printf("%s+%s\n", version, revision)
return
}
if *vmidFlag == "" {
fmt.Fprintln(os.Stderr, "ERROR: You must set the -vmid parameter.")
flag.Usage()
os.Exit(1)
}
log.Printf("Executing the %s command...", *commandFlag)
exitCode, output, err := executeCommand(*commandStdinFlag, *commandFlag)
if err != nil {
log.Fatalf("failed to execute command: %v", err)
}
log.Printf("Command ended with exit code %d and output:\n%s", exitCode, output)
os.Exit(exitCode)
}
func executeCommand(stdin string, command string) (int, string, error) {
config := &ssh.ClientConfig{
User: *sshUsernameFlag,
Auth: []ssh.AuthMethod{},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
if *sshPasswordFlag != "" {
config.Auth = append(config.Auth, ssh.Password(*sshPasswordFlag))
}
conn, addr, err := openConnection(*vmidFlag)
if err != nil {
return -1, "", fmt.Errorf("failed to open ssh connection: %w", err)
}
c, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
if err != nil {
log.Fatalf("Failed to create connection: %v", err)
}
client := ssh.NewClient(c, chans, reqs)
defer client.Close()
log.Printf("Connected from %s (%s) to %s (%s)",
client.LocalAddr(),
client.ClientVersion(),
client.RemoteAddr(),
client.ServerVersion())
log.Printf("Creating SSH session to %s...", addr)
session, err := client.NewSession()
if err != nil {
return -1, "", fmt.Errorf("failed to create session: %w", err)
}
defer session.Close()
if stdin != "" {
session.Stdin = bytes.NewBufferString(stdin)
}
output, err := session.CombinedOutput(command)
if err != nil {
if e, ok := err.(*ssh.ExitError); ok {
return e.ExitStatus(), string(output), nil
}
return -1, "", fmt.Errorf("failed to run command: %w", err)
}
return 0, string(output), nil
}