Skip to content

Commit 3b9201e

Browse files
committed
add support for setting the destination hyper-v socket service id
1 parent c6433f7 commit 3b9201e

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ In a PowerShell session, execute:
2121
-vmid ((Get-VM vm1).ID) `
2222
-command 'ps -efww --forest'
2323
```
24+
25+
This can also be executed inside a Guest Hyper-V VM, to target a local service:
26+
27+
```powershell
28+
# NB the available service-id GUIDs are listed in the Hyper-V Host registry key at:
29+
# HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices
30+
.\use-ssh-over-hyper-v-socket.exe `
31+
-username vagrant `
32+
-password vagrant `
33+
-vmid localhost `
34+
-service-id '0000138A-FACB-11E6-BD58-64006A7986D3' `
35+
-command 'whoami.exe /all'
36+
```

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
sshUsernameFlag = flag.String("username", "vagrant", "ssh username")
2020
sshPasswordFlag = flag.String("password", "", "ssh password")
2121
sshPortFlag = flag.Uint("port", 22, "ssh port")
22+
sshServiceIDFlag = flag.String("service-id", "", "the destination hyper-v socket Service ID (GUID).\nwhen this is used, the -port parameter is ignored.")
2223
commandStdinFlag = flag.String("stdin", "", "data to pass into the command stdin")
2324
commandFlag = flag.String("command", "ps -efww --forest", "command to execute")
2425
version = "0.0.0-dev"
@@ -76,10 +77,18 @@ func executeCommand(stdin string, command string) (int, string, error) {
7677
log.Fatalf("Failed to parse VM ID: %v", err)
7778
}
7879
}
80+
var serviceID guid.GUID
81+
if *sshServiceIDFlag == "" {
82+
serviceID = winio.VsockServiceID(uint32(*sshPortFlag))
83+
} else {
84+
serviceID, err = guid.FromString(*sshServiceIDFlag)
85+
if err != nil {
86+
log.Fatalf("Failed to parse service ID: %v", err)
87+
}
7988
}
8089
addr := &winio.HvsockAddr{
8190
VMID: vmid,
82-
ServiceID: winio.VsockServiceID(uint32(*sshPortFlag)),
91+
ServiceID: serviceID,
8392
}
8493
conn, err := winio.Dial(context.Background(), addr)
8594
if err != nil {

0 commit comments

Comments
 (0)