Skip to content

Commit

Permalink
fix(cli): session.Run is sensitive to quoting on Windows host
Browse files Browse the repository at this point in the history
There is a strange issue with SSH executor via session.Run
on Windows host (i.e. Windows SSH client) which seems to be
prone to failure depending on particular arrangements of quote,
double-quotes, etc. in PowerShell command.

For example, this fails

  output, err := r.run(`Enable-NetFirewallRule -DisplayGroup "Remote Desktop"`)

while this succeeds

  output, err := r.run(`Enable-NetFirewallRule -DisplayGroup 'Remote Desktop'`)

Wrapping the command with { and } also seems to solve
this problem.

Interestingly, this issue seems not reproducible Linux host.
  • Loading branch information
mloskot committed Jan 19, 2024
1 parent 43a30a9 commit 2d48619
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion experiments/swdt/pkg/connections/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ func (c *SSHConnection) Run(args string) (string, error) {

// Multiline PowerShell commands over SSH trip over newlines - only first one is executed
args = regexp.MustCompile(`\r?\n`).ReplaceAllLiteralString(args, ";")
if err := session.Run("powershell -nologo -noprofile -c " + args); err != nil {
cmd := fmt.Sprintf("powershell -nologo -noprofile -c { %s }", args)
// TODO(mloskot): Log executed commands in --verbose mode
if err := session.Run(cmd); err != nil {
return "", err
}

Expand Down
5 changes: 3 additions & 2 deletions experiments/swdt/pkg/pwsh/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ func (r *SetupRunner) EnableRDP(enable bool) error {
return nil
}

output, err := r.run(`Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0;
klog.Info(resc.Sprintf("Enabling RDP."))
output, err := r.run(`Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -name "fDenyTSConnections" -value 0;
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"`)
if err != nil {
return err
}
klog.Info(resc.Sprintf("Enabling RDP. %s", output))
klog.Info(resc.Sprintf(output))
return nil
}

0 comments on commit 2d48619

Please sign in to comment.