1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
"github.com/juju/errors"
11
"github.com/juju/utils/ssh"
13
"github.com/juju/juju/cmd/modelcmd"
16
var usageSSHSummary = `
17
Initiates an SSH session or executes a command on a Juju machine.`[1:]
19
var usageSSHDetails = `
20
The machine is identified by the <target> argument which is either a 'unit
21
name' or a 'machine id'. Both are obtained in the output to "juju status". If
22
'user' is specified then the connection is made to that user account;
23
otherwise, the default 'ubuntu' account, created by Juju, is used.
25
The optional command is executed on the remote machine. Any output is sent back
26
to the user. Screen-based programs require the default of '--pty=true'.
28
The SSH host keys of the target are verified. The --no-host-key-checks option
29
can be used to disable these checks. Use of this option is not recommended as
30
it opens up the possibility of a man-in-the-middle attack.
37
Connect to machine 1 and run command 'uname -a':
41
Connect to a mysql unit:
45
Connect to a jenkins unit as user jenkins:
47
juju ssh jenkins@jenkins/0
52
func newSSHCommand() cmd.Command {
53
return modelcmd.Wrap(&sshCommand{})
56
// sshCommand is responsible for launching a ssh shell on a given unit or machine.
57
type sshCommand struct {
61
func (c *sshCommand) Info() *cmd.Info {
64
Args: "<[user@]target> [command]",
65
Purpose: usageSSHSummary,
70
func (c *sshCommand) Init(args []string) error {
72
return fmt.Errorf("no target name specified")
74
c.Target, c.Args = args[0], args[1:]
78
// Run resolves c.Target to a machine, to the address of a i
79
// machine or unit forks ssh passing any arguments provided.
80
func (c *sshCommand) Run(ctx *cmd.Context) error {
83
return errors.Trace(err)
87
target, err := c.resolveTarget(c.Target)
92
options, err := c.getSSHOptions(c.pty, target)
97
cmd := ssh.Command(target.userHost(), c.Args, options)
99
cmd.Stdout = ctx.Stdout
100
cmd.Stderr = ctx.Stderr