mirror of
https://github.com/rancher/cli.git
synced 2026-02-05 09:48:36 +01:00
Update ssh to accept users and pass through all args
Problem: SSH command does not accept a user or allow passing through of commands Solution: Add support for -l flag as well as <user>@<node> syntax Pass through all following args to SSH
This commit is contained in:
committed by
Darren Shepherd
parent
1191bbb06e
commit
6e2f325052
@@ -26,7 +26,7 @@ Examples:
|
||||
# Show workloads in the current context
|
||||
$ rancher ps
|
||||
|
||||
#Show workloads in a specific project and output the results in yaml
|
||||
# Show workloads in a specific project and output the results in yaml
|
||||
$ rancher ps --project projectFoo --format yaml
|
||||
`,
|
||||
Action: psLs,
|
||||
|
||||
53
cmd/ssh.go
53
cmd/ssh.go
@@ -11,6 +11,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rancher/cli/cliclient"
|
||||
@@ -18,36 +19,66 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
const sshDescription = `For any nodes created through Rancher using docker-machine,
|
||||
you can SSH into the node. This is not supported for any custom nodes.`
|
||||
const sshDescription = `
|
||||
For any nodes created through Rancher using docker-machine,
|
||||
you can SSH into the node. This is not supported for any custom nodes.
|
||||
|
||||
Examples:
|
||||
# SSH into a node by ID/name
|
||||
$ rancher ssh nodeFoo
|
||||
|
||||
# SSH into a node by name but specify the username to use
|
||||
$ rancher ssh -l user1 nodeFoo
|
||||
|
||||
# SSH into a node by specifying user and node using the @ syntax while adding a command to run
|
||||
$ rancher ssh user1@nodeFoo env
|
||||
`
|
||||
|
||||
func SSHCommand() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "ssh",
|
||||
Usage: "SSH into a node",
|
||||
Description: sshDescription,
|
||||
ArgsUsage: "[NODEID NODENAME]",
|
||||
ArgsUsage: "[NODE_ID/NODE_NAME]",
|
||||
Action: nodeSSH,
|
||||
Flags: []cli.Flag{},
|
||||
UsageText: "potato",
|
||||
SkipFlagParsing: true,
|
||||
}
|
||||
}
|
||||
|
||||
func nodeSSH(ctx *cli.Context) error {
|
||||
if ctx.NArg() == 0 {
|
||||
return errors.New("node ID is required")
|
||||
return cli.ShowCommandHelp(ctx, "ssh")
|
||||
}
|
||||
|
||||
args := ctx.Args()
|
||||
if len(args) > 0 && (args[0] == "-h" || args[0] == "--help") {
|
||||
return cli.ShowCommandHelp(ctx, "ssh")
|
||||
}
|
||||
|
||||
// ssh nodeName or ssh -l user nodeName or ssh user@nodeName
|
||||
var user string
|
||||
var nodeName string
|
||||
if strings.Contains(args[0], "@") {
|
||||
pieces := strings.Split(args[0], "@")
|
||||
user = pieces[0]
|
||||
nodeName = pieces[1]
|
||||
args = args[1:]
|
||||
} else if args[0] == "-l" {
|
||||
user = args[1]
|
||||
nodeName = args[2]
|
||||
args = args[3:]
|
||||
} else {
|
||||
nodeName = args[0]
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
c, err := GetClient(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resource, err := Lookup(c, args[0], "node")
|
||||
resource, err := Lookup(c, nodeName, "node")
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
@@ -57,15 +88,19 @@ func nodeSSH(ctx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if user == "" {
|
||||
user = sshNode.SshUser
|
||||
}
|
||||
|
||||
key, err := getSSHKey(c, sshNode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return processExitCode(callSSH(key, sshNode.IPAddress, sshNode.SshUser))
|
||||
return processExitCode(callSSH(key, sshNode.IPAddress, user, args))
|
||||
}
|
||||
|
||||
func callSSH(content []byte, ip string, user string) error {
|
||||
func callSSH(content []byte, ip string, user string, args []string) error {
|
||||
dest := fmt.Sprintf("%s@%s", user, ip)
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "ssh")
|
||||
@@ -87,7 +122,7 @@ func callSSH(content []byte, ip string, user string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := exec.Command("ssh", append([]string{"-i", tmpfile.Name()}, dest)...)
|
||||
cmd := exec.Command("ssh", append([]string{"-i", tmpfile.Name(), dest}, args...)...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
Reference in New Issue
Block a user