github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/commands/ssh.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "github.com/juju/utils/ssh" 10 11 "github.com/juju/juju/cmd/modelcmd" 12 ) 13 14 var usageSSHSummary = ` 15 Initiates an SSH session or executes a command on a Juju machine.`[1:] 16 17 var usageSSHDetails = ` 18 The machine is identified by the <target> argument which is either a 'unit 19 name' or a 'machine id'. Both are obtained in the output to "juju status". If 20 'user' is specified then the connection is made to that user account; 21 otherwise, the default 'ubuntu' account, created by Juju, is used. 22 23 The optional command is executed on the remote machine. Any output is sent back 24 to the user. Screen-based programs require the default of '--pty=true'. 25 26 The SSH host keys of the target are verified. The --no-host-key-checks option 27 can be used to disable these checks. Use of this option is not recommended as 28 it opens up the possibility of a man-in-the-middle attack. 29 30 Examples: 31 Connect to machine 0: 32 33 juju ssh 0 34 35 Connect to machine 1 and run command 'uname -a': 36 37 juju ssh 1 uname -a 38 39 Connect to a mysql unit: 40 41 juju ssh mysql/0 42 43 Connect to a jenkins unit as user jenkins: 44 45 juju ssh jenkins@jenkins/0 46 47 See also: 48 scp` 49 50 func newSSHCommand() cmd.Command { 51 return modelcmd.Wrap(&sshCommand{}) 52 } 53 54 // sshCommand is responsible for launching a ssh shell on a given unit or machine. 55 type sshCommand struct { 56 SSHCommon 57 } 58 59 func (c *sshCommand) Info() *cmd.Info { 60 return &cmd.Info{ 61 Name: "ssh", 62 Args: "<[user@]target> [command]", 63 Purpose: usageSSHSummary, 64 Doc: usageSSHDetails, 65 } 66 } 67 68 func (c *sshCommand) Init(args []string) error { 69 if len(args) == 0 { 70 return errors.Errorf("no target name specified") 71 } 72 c.Target, c.Args = args[0], args[1:] 73 return nil 74 } 75 76 // Run resolves c.Target to a machine, to the address of a i 77 // machine or unit forks ssh passing any arguments provided. 78 func (c *sshCommand) Run(ctx *cmd.Context) error { 79 err := c.initRun() 80 if err != nil { 81 return errors.Trace(err) 82 } 83 defer c.cleanupRun() 84 85 target, err := c.resolveTarget(c.Target) 86 if err != nil { 87 return err 88 } 89 90 options, err := c.getSSHOptions(c.pty, target) 91 if err != nil { 92 return err 93 } 94 95 cmd := ssh.Command(target.userHost(), c.Args, options) 96 cmd.Stdin = ctx.Stdin 97 cmd.Stdout = ctx.Stdout 98 cmd.Stderr = ctx.Stderr 99 return cmd.Run() 100 }