ó FRTc@sÛdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z de fd„ƒYZ de fd„ƒYZd e fd „ƒYZd „Zd d dd„ZdS(s The cmdshell module uses the paramiko package to create SSH connections to the servers that are represented by instance objects. The module has functions for running commands, managing files, and opening interactive shell sessions over those connections. iÿÿÿÿ(tinteractive_shellN(tStringIOt SSHClientcBs˜eZdZddddd„Zdd„Zd„Zd„Zd„Zd d d „Z d „Z d „Z d„Z d„Z d„Zd„Zd„ZRS(se This class creates a paramiko.SSHClient() object that represents a session with an SSH server. You can use the SSHClient object to send commands to the remote host and manipulate files on the remote host. :ivar server: A Server object or FakeServer object. :ivar host_key_file: The path to the user's .ssh key files. :ivar uname: The username for the SSH connection. Default = 'root'. :ivar timeout: The optional timeout variable for the TCP connection. :ivar ssh_pwd: An optional password to use for authentication or for unlocking the private key. s~/.ssh/known_hoststrootcCsž||_||_||_||_tjj|jd|ƒ|_tj ƒ|_ |j j ƒ|j j t jj|ƒƒ|j jtjƒƒ|jƒdS(Ntpassword(tservert host_key_filetunamet_timeouttparamikotRSAKeytfrom_private_key_filet ssh_key_filet_pkeyRt _ssh_clienttload_system_host_keystload_host_keystostpatht expandusertset_missing_host_key_policyt AutoAddPolicytconnect(tselfRRRttimeouttssh_pwd((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyt__init__3s      icCsd}x||kry9|jj|jjd|jd|jd|jƒdSWq tjk r£}|j \}}|dkrd GHt j d ƒ|d 7}q ‚q t j k rÞd |jjGHd GHtdƒ|d 7}q tk r dGHt j d ƒ|d 7}q Xq WdGHdS(s³ Connect to an SSH server and authenticate with it. :type num_retries: int :param num_retries: The maximum number of connection attempts. itusernametpkeyRNi3i=ios/SSH Connection refused, will retry in 5 secondsiis:%s has an entry in ~/.ssh/known_hosts and it doesn't matchsCEdit that file to remove the entry and then hit return to try againsHit Enter when readys8Unexpected Error from SSH Connection, retry in 5 secondss"Could not establish SSH connection(i3i=io(RRRthostnameRR RtsocketterrortargsttimetsleepR tBadHostKeyExceptiont raw_inputtEOFError(Rt num_retriestretrytxxx_todo_changemetvaluetmessage((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyRBs2          cCs |jjƒS(s£ Open an SFTP session on the SSH server. :rtype: :class:`paramiko.sftp_client.SFTPClient` :return: An SFTP client object. (Rt open_sftp(R((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR+dscCs |jƒ}|j||ƒdS(s€ Open an SFTP session on the remote host, and copy a file from the remote host to the specified path on the local host. :type src: string :param src: The path to the target file on the remote host. :type dst: string :param dst: The path on your local host where you want to store the file. N(R+tget(Rtsrctdstt sftp_client((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pytget_filems cCs |jƒ}|j||ƒdS(s€ Open an SFTP session on the remote host, and copy a file from the local host to the specified path on the remote host. :type src: string :param src: The path to the target file on your local host. :type dst: string :param dst: The path on the remote host where you want to store the file. N(R+tput(RR-R.R/((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pytput_file|s triÿÿÿÿcCs|jƒ}|j|||ƒS(sü Open an SFTP session to the remote host, and open a file on that host. :type filename: string :param filename: The path to the file on the remote host. :type mode: string :param mode: The file interaction mode. :type bufsize: integer :param bufsize: The file buffer size. :rtype: :class:`paramiko.sftp_file.SFTPFile` :return: A paramiko proxy object for a file on the remote server. (R+topen(RtfilenametmodetbufsizeR/((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR4‹s cCs|jƒ}|j|ƒS(s= List all of the files and subdirectories at the specified path on the remote host. :type path: string :param path: The base path from which to obtain the list. :rtype: list :return: A list of files and subdirectories at the specified path. (R+tlistdir(RRR/((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR8Ÿs cCs.|jd|ƒ}|djdƒr*dSdS(s› Check the specified path on the remote host to determine if it is a directory. :type path: string :param path: The path to the directory that you want to check. :rtype: integer :return: If the path is a directory, the function returns 1. If the path is a file or an invalid path, the function returns 0. s[ -d %s ] || echo "FALSE"itFALSEi(trunt startswith(RRtstatus((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pytisdir­s cCs.|jd|ƒ}|djdƒr*dSdS(sX Check the remote host for the specified path, or a file at the specified path. This function returns 1 if the path or the file exist on the remote host, and returns 0 if the path or the file does not exist on the remote host. :type path: string :param path: The path to the directory or file that you want to check. :rtype: integer :return: If the path or the file exist, the function returns 1. If the path or the file do not exist on the remote host, the function returns 0. s[ -a %s ] || echo "FALSE"iR9i(R:R;(RRR<((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pytexists¿scCs|jjƒ}t|ƒdS(sJ Start an interactive shell session with the remote host. N(Rt invoke_shellR(Rtchannel((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pytshellÔscCsØtjjd||jjfƒd}y|jj|ƒ}Wntjk rXd}nX|dj ƒ}|dj ƒ}|dj ƒ|dj ƒ|dj ƒtjjd|ƒtjjd|ƒ|||fS(s^ Run a command on the remote host. :type command: string :param command: The command that you want to send to the remote host. :rtype: tuple :return: This function returns a tuple that contains an integer status, the stdout from the command, and the stderr from the command. srunning:%s on %siiis stdout: %ss stderr: %s( tbototlogtdebugRt instance_idRt exec_commandR t SSHExceptiontreadtclose(RtcommandR<tttstd_outtstd_err((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR:Ûs  cCsPtjjd||jjfƒ|jjƒjƒ}|jƒ|j |ƒ|S(s8 Request a pseudo-terminal from a server, and execute a command on that server. :type command: string :param command: The command that you want to run on the remote host. :rtype: :class:`paramiko.channel.Channel` :return: An open channel object. srunning:%s on %s( RBRCRDRRERt get_transportt open_sessiontget_ptyRF(RRJR@((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pytrun_ptyös   cCs*|jjƒ}|jƒ|jjƒdS(sQ Close an SSH session and any open channels that are tied to it. N(RRNRIRtreset_cmdshell(Rt transport((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyRIs N(t__name__t __module__t__doc__tNoneRRR+R0R2R4R8R=R>RAR:RQRI(((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR&s  "        t LocalClientcBseeZdZd dd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z RS( sÅ :ivar server: A Server object or FakeServer object. :ivar host_key_file: The path to the user's .ssh key files. :ivar uname: The username for the SSH connection. Default = 'root'. RcCs||_||_||_dS(N(RRR(RRRR((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyRs  cCstj||ƒdS(s< Copy a file from one directory to another. N(tshutiltcopyfile(RR-R.((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR0scCstj||ƒdS(s< Copy a file from one directory to another. N(RYRZ(RR-R.((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR2 scCs tj|ƒS(sâ List all of the files and subdirectories at the specified path. :rtype: list :return: Return a list containing the names of the entries in the directory given by path. (RR8(RR((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR8&scCstjj|ƒS(s² Check the specified path to determine if it is a directory. :rtype: boolean :return: Returns True if the path is an existing directory. (RRR=(RR((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR=0scCstjj|ƒS(s Check for the specified path, or check a file at the specified path. :rtype: boolean :return: If the path or the file exist, the function returns True. (RRR>(RR((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR>9scCstdƒ‚dS(Ns$shell not supported with LocalClient(tNotImplementedError(R((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyRABsc Csêtjjd|jƒtƒ}tj|jdtdtjdtjdtjƒ}xQ|j ƒd kr¦t j dƒ|j ƒ}|j|dƒ|j|dƒqVWtjj|jƒƒtjjd|jƒƒ|j|jƒfS( sÿ Open a subprocess and run a command on the local host. :rtype: tuple :return: This function returns a tuple that contains an integer status and a string with the combined stdout and stderr output. s running:%sRAtstdintstdouttstderriis output: %sN(RBRCtinfoRJRt subprocesstPopentTruetPIPEtpollRWR!R"t communicatetwritetgetvaluet returncode(Rtlog_fptprocessRK((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyR:Es   cCsdS(N((R((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyRIZsN( RTRURVRWRR0R2R8R=R>RAR:RI(((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyRXs    t FakeServercBseZdZd„ZRS(se This object has a subset of the variables that are normally in a :class:`boto.manage.server.Server` object. You can use this FakeServer object to create a :class:`boto.manage.SSHClient` object if you don't have a real Server object. :ivar instance: A boto Instance object. :ivar ssh_key_file: The path to the SSH key file. cCs1||_||_|j|_|jj|_dS(N(tinstanceR tdns_nameRtidRE(RRlR ((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyRgs   (RTRURVR(((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyRk]s cCs?tjjdddƒ}||jkr1t|ƒSt|ƒSdS(s Connect to the specified server. :return: If the server is local, the function returns a :class:`boto.manage.cmdshell.LocalClient` object. If the server is remote, the function returns a :class:`boto.manage.cmdshell.SSHClient` object. tInstances instance-idN(RBtconfigR,RWRERXR(RRE((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pytstartms  s~/.ssh/known_hostsRcCs"t||ƒ}t||||ƒS(s Create and return an SSHClient object given an instance object. :type instance: :class`boto.ec2.instance.Instance` object :param instance: The instance object. :type ssh_key_file: string :param ssh_key_file: A path to the private key file that is used to log into the instance. :type host_key_file: string :param host_key_file: A path to the known_hosts file used by the SSH client. Defaults to ~/.ssh/known_hosts :type user_name: string :param user_name: The username to use when logging into the instance. Defaults to root. :type ssh_pwd: string :param ssh_pwd: The passphrase, if any, associated with private key. (RkR(RlR Rt user_nameRts((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pytsshclient_from_instance|s(RVtboto.mashups.interactiveRRBRR!RYR RR`t boto.compatRtobjectRRXRkRqRWRt(((sA/opt/freeware/lib/python2.7/site-packages/boto/manage/cmdshell.pyts       éN