This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enables ssh via Go for Windows OpenSSH server (#1643)
This commit enables ssh via Go for the Windows OpenSSH server.
- Loading branch information
1 parent
4de33cf
commit 96e104e
Showing
3 changed files
with
82 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |