|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/Microsoft/go-winio" |
| 12 | + "github.com/Microsoft/go-winio/pkg/guid" |
| 13 | + "golang.org/x/crypto/ssh" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + versionFlag = flag.Bool("version", false, "show version") |
| 18 | + vmidFlag = flag.String("vmid", "", "hyper-v VM ID (GUID)") |
| 19 | + sshUsernameFlag = flag.String("username", "vagrant", "ssh username") |
| 20 | + sshPasswordFlag = flag.String("password", "", "ssh password") |
| 21 | + sshPortFlag = flag.Uint("port", 22, "ssh port") |
| 22 | + commandStdinFlag = flag.String("stdin", "", "data to pass into the command stdin") |
| 23 | + commandFlag = flag.String("command", "ps -efww --forest", "command to execute") |
| 24 | + version = "0.0.0-dev" |
| 25 | + revision = "0000000000000000000000000000000000000000" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + log.SetOutput(os.Stdout) // for not disturbing PowerShell... |
| 30 | + |
| 31 | + flag.Parse() |
| 32 | + |
| 33 | + if *versionFlag { |
| 34 | + fmt.Printf("%s+%s\n", version, revision) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + log.Printf("Executing the %s command...", *commandFlag) |
| 39 | + |
| 40 | + exitCode, output, err := executeCommand(*commandStdinFlag, *commandFlag) |
| 41 | + if err != nil { |
| 42 | + log.Fatalf("failed to execute command: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + log.Printf("Command ended with exit code %d and output:\n%s", exitCode, output) |
| 46 | + |
| 47 | + os.Exit(exitCode) |
| 48 | +} |
| 49 | + |
| 50 | +func executeCommand(stdin string, command string) (int, string, error) { |
| 51 | + config := &ssh.ClientConfig{ |
| 52 | + User: *sshUsernameFlag, |
| 53 | + Auth: []ssh.AuthMethod{}, |
| 54 | + HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 55 | + } |
| 56 | + |
| 57 | + if *sshPasswordFlag != "" { |
| 58 | + config.Auth = append(config.Auth, ssh.Password(*sshPasswordFlag)) |
| 59 | + } |
| 60 | + |
| 61 | + log.Printf("Connecting to %s on port %d...", *vmidFlag, *sshPortFlag) |
| 62 | + |
| 63 | + vmid, err := guid.FromString(*vmidFlag) |
| 64 | + if err != nil { |
| 65 | + log.Fatalf("Failed to parse VM ID: %v", err) |
| 66 | + } |
| 67 | + addr := &winio.HvsockAddr{ |
| 68 | + VMID: vmid, |
| 69 | + ServiceID: winio.VsockServiceID(uint32(*sshPortFlag)), |
| 70 | + } |
| 71 | + conn, err := winio.Dial(context.Background(), addr) |
| 72 | + if err != nil { |
| 73 | + log.Fatalf("Failed to dial: %v", err) |
| 74 | + } |
| 75 | + c, chans, reqs, err := ssh.NewClientConn(conn, addr.String(), config) |
| 76 | + if err != nil { |
| 77 | + log.Fatalf("Failed to create connection: %v", err) |
| 78 | + } |
| 79 | + client := ssh.NewClient(c, chans, reqs) |
| 80 | + defer client.Close() |
| 81 | + |
| 82 | + log.Printf("Connected from %s (%s) to %s (%s)", |
| 83 | + client.LocalAddr(), |
| 84 | + client.ClientVersion(), |
| 85 | + client.RemoteAddr(), |
| 86 | + client.ServerVersion()) |
| 87 | + |
| 88 | + log.Printf("Creating SSH session to %s...", addr) |
| 89 | + |
| 90 | + session, err := client.NewSession() |
| 91 | + if err != nil { |
| 92 | + return -1, "", fmt.Errorf("failed to create session: %w", err) |
| 93 | + } |
| 94 | + defer session.Close() |
| 95 | + |
| 96 | + if stdin != "" { |
| 97 | + session.Stdin = bytes.NewBufferString(stdin) |
| 98 | + } |
| 99 | + |
| 100 | + output, err := session.CombinedOutput(command) |
| 101 | + if err != nil { |
| 102 | + if e, ok := err.(*ssh.ExitError); ok { |
| 103 | + return e.ExitStatus(), string(output), nil |
| 104 | + } |
| 105 | + return -1, "", fmt.Errorf("failed to run command: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + return 0, string(output), nil |
| 109 | +} |
0 commit comments