mirror of
https://github.com/containers/podman.git
synced 2025-07-15 03:02:52 +08:00
Merge pull request #11617 from ashley-cui/ssh
[NO TESTS NEEDED] Add username flag for machine ssh
This commit is contained in:
@ -5,6 +5,7 @@ package machine
|
|||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/containers/common/pkg/completion"
|
||||||
"github.com/containers/common/pkg/config"
|
"github.com/containers/common/pkg/config"
|
||||||
"github.com/containers/podman/v3/cmd/podman/registry"
|
"github.com/containers/podman/v3/cmd/podman/registry"
|
||||||
"github.com/containers/podman/v3/pkg/machine"
|
"github.com/containers/podman/v3/pkg/machine"
|
||||||
@ -15,7 +16,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
sshCmd = &cobra.Command{
|
sshCmd = &cobra.Command{
|
||||||
Use: "ssh [NAME] [COMMAND [ARG ...]]",
|
Use: "ssh [options] [NAME] [COMMAND [ARG ...]]",
|
||||||
Short: "SSH into an existing machine",
|
Short: "SSH into an existing machine",
|
||||||
Long: "SSH into a managed virtual machine ",
|
Long: "SSH into a managed virtual machine ",
|
||||||
RunE: ssh,
|
RunE: ssh,
|
||||||
@ -35,6 +36,10 @@ func init() {
|
|||||||
Command: sshCmd,
|
Command: sshCmd,
|
||||||
Parent: machineCmd,
|
Parent: machineCmd,
|
||||||
})
|
})
|
||||||
|
flags := sshCmd.Flags()
|
||||||
|
usernameFlagName := "username"
|
||||||
|
flags.StringVar(&sshOpts.Username, usernameFlagName, "", "Username to use when ssh-ing into the VM.")
|
||||||
|
_ = sshCmd.RegisterFlagCompletionFunc(usernameFlagName, completion.AutocompleteNone)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ssh(cmd *cobra.Command, args []string) error {
|
func ssh(cmd *cobra.Command, args []string) error {
|
||||||
@ -48,13 +53,6 @@ func ssh(cmd *cobra.Command, args []string) error {
|
|||||||
// Set the VM to default
|
// Set the VM to default
|
||||||
vmName := defaultMachineName
|
vmName := defaultMachineName
|
||||||
|
|
||||||
// If we're not given a VM name, use the remote username from the connection config
|
|
||||||
if len(args) == 0 {
|
|
||||||
sshOpts.Username, err = remoteConnectionUsername()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If len is greater than 0, it means we may have been
|
// If len is greater than 0, it means we may have been
|
||||||
// provided the VM name. If so, we check. The VM name,
|
// provided the VM name. If so, we check. The VM name,
|
||||||
// if provided, must be in args[0].
|
// if provided, must be in args[0].
|
||||||
@ -68,10 +66,6 @@ func ssh(cmd *cobra.Command, args []string) error {
|
|||||||
if validVM {
|
if validVM {
|
||||||
vmName = args[0]
|
vmName = args[0]
|
||||||
} else {
|
} else {
|
||||||
sshOpts.Username, err = remoteConnectionUsername()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sshOpts.Args = append(sshOpts.Args, args[0])
|
sshOpts.Args = append(sshOpts.Args, args[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,12 +77,15 @@ func ssh(cmd *cobra.Command, args []string) error {
|
|||||||
if validVM {
|
if validVM {
|
||||||
sshOpts.Args = args[1:]
|
sshOpts.Args = args[1:]
|
||||||
} else {
|
} else {
|
||||||
|
sshOpts.Args = args
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !validVM && sshOpts.Username == "" {
|
||||||
sshOpts.Username, err = remoteConnectionUsername()
|
sshOpts.Username, err = remoteConnectionUsername()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
sshOpts.Args = args
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch vmType {
|
switch vmType {
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
podman\-machine\-ssh - SSH into a virtual machine
|
podman\-machine\-ssh - SSH into a virtual machine
|
||||||
|
|
||||||
## SYNOPSIS
|
## SYNOPSIS
|
||||||
**podman machine ssh** [*name*] [*command* [*arg* ...]]
|
**podman machine ssh** [*options*] [*name*] [*command* [*arg* ...]]
|
||||||
|
|
||||||
## DESCRIPTION
|
## DESCRIPTION
|
||||||
|
|
||||||
@ -21,6 +21,10 @@ with the virtual machine is established.
|
|||||||
|
|
||||||
Print usage statement.
|
Print usage statement.
|
||||||
|
|
||||||
|
#### **--username**=*name*
|
||||||
|
|
||||||
|
Username to use when SSH-ing into the VM.
|
||||||
|
|
||||||
## EXAMPLES
|
## EXAMPLES
|
||||||
|
|
||||||
To get an interactive session with the default virtual machine:
|
To get an interactive session with the default virtual machine:
|
||||||
|
Reference in New Issue
Block a user