Skip to content
This repository has been archived by the owner on Nov 9, 2020. It is now read-only.

Commit

Permalink
Enables ssh via Go for Windows OpenSSH server (#1643)
Browse files Browse the repository at this point in the history
This commit enables ssh via Go for the Windows OpenSSH server.
  • Loading branch information
venilnoronha authored Jul 27, 2017
1 parent 4de33cf commit 96e104e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
20 changes: 0 additions & 20 deletions tests/utils/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,10 @@ package ssh

import (
"log"
"os"
"os/exec"
"strings"
)

const (
sshKeyOptPath = "-i /root/.ssh/id_rsa"
)

// InvokeCommand - can be consumed by test directly to invoke
// any command on the remote host.
// ip: remote machine address to execute on the machine
// cmd: A command string to be executed on the remote host as per
func InvokeCommand(ip, cmd string) (string, error) {
sshKeyOpt := strings.Split(os.Getenv("SSH_KEY_OPT"), " ")
if sshKeyOpt == nil {
sshKeyOpt = strings.Split(sshKeyOptPath, " ")
}
sshIdentity := []string{sshKeyOpt[0], sshKeyOpt[1], "-q", "-kTax", "-o StrictHostKeyChecking=no"}

out, err := exec.Command("/usr/bin/ssh", append(sshIdentity, "root@"+ip, cmd)...).CombinedOutput()
return strings.TrimSpace(string(out[:])), err
}

// InvokeCommandLocally - can be consumed by test directly to invoke
// any command locally.
// cmd: A command string to be executed on the remote host as per
Expand Down
39 changes: 39 additions & 0 deletions tests/utils/ssh/ssh_win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Exposes util to invoke remote commands on windows hosts via ssh.

// +build winutil

package ssh

import (
"fmt"
"os/exec"
"strings"
)

// sshTemplate is the ssh command template for Windows hosts.
const sshTemplate = "/usr/bin/ssh -q -o StrictHostKeyChecking=no root@%s '%s'; exit"

// InvokeCommand invokes the given command on the given host via ssh.
func InvokeCommand(ip, cmdStr string) (string, error) {
// OpenSSH sessions terminate sporadically when a pty isn't allocated.
// The -t flag doesn't work with OpenSSH on Windows, so we wrap the ssh call
// within a bash session as a workaround, so that a pty is created.
cmd := exec.Command("/bin/bash")
cmd.Stdin = strings.NewReader(fmt.Sprintf(sshTemplate, ip, cmdStr))
out, err := cmd.CombinedOutput()
return strings.TrimSpace(string(out[:])), err
}
43 changes: 43 additions & 0 deletions tests/utils/ssh/sshdefault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Exposes util to invoke remote commands on non-windows hosts via ssh.

// +build !winutil

package ssh

import (
"os"
"os/exec"
"strings"
)

// sshKeyOptPath is the option to specify the rsa key while connecting via ssh.
const sshKeyOptPath = "-i /root/.ssh/id_rsa"

// InvokeCommand - can be consumed by test directly to invoke
// any command on the remote host.
// ip: remote machine address to execute on the machine
// cmd: A command string to be executed on the remote host as per
func InvokeCommand(ip, cmd string) (string, error) {
sshKeyOpt := strings.Split(os.Getenv("SSH_KEY_OPT"), " ")
if sshKeyOpt == nil {
sshKeyOpt = strings.Split(sshKeyOptPath, " ")
}
sshIdentity := []string{sshKeyOpt[0], sshKeyOpt[1], "-q", "-kTax", "-o StrictHostKeyChecking=no"}

out, err := exec.Command("/usr/bin/ssh", append(sshIdentity, "root@"+ip, cmd)...).CombinedOutput()
return strings.TrimSpace(string(out[:])), err
}

0 comments on commit 96e104e

Please sign in to comment.