o ckF[9@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z Gddde Z Gddde ZGd d d e Zd d Z dddZdS)a 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. )interactive_shellN)StringIOc@seZdZdZ  d"ddZd#dd Zd d Zd d ZddZd$ddZ ddZ ddZ ddZ ddZ ddZddZd d!ZdS)% SSHClientae 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. ~/.ssh/known_hostsrootNcCsp||_||_||_||_tjj|j|d|_t |_ |j |j t j||j t|dS)N)password)server host_key_fileuname_timeoutparamikoRSAKeyfrom_private_key_file ssh_key_file_pkeyr _ssh_clientload_system_host_keysload_host_keysospath expanduserset_missing_host_key_policy AutoAddPolicyconnect)selfrr r timeoutssh_pwdr6/usr/lib/python3/dist-packages/boto/manage/cmdshell.py__init__3s    zSSHClient.__init__c Csd}||krzz|jj|jj|j|j|jdWdStjyC}z|j \}}|dvr8t dt d|d7}nWYd}~n7d}~wt jy`t d|jjt d td |d7}Yntyut d t d|d7}Ynw||kst d dS) z Connect to an SSH server and authenticate with it. :type num_retries: int :param num_retries: The maximum number of connection attempts. r)usernamepkeyrN)3=oz/SSH Connection refused, will retry in 5 secondsr z:%s has an entry in ~/.ssh/known_hosts and it doesn't matchzCEdit that file to remove the entry and then hit return to try againzHit Enter when readyz8Unexpected Error from SSH Connection, retry in 5 secondsz"Could not establish SSH connection)rrrhostnamer rr socketerrorargsprinttimesleepr BadHostKeyException raw_inputEOFError)r num_retriesretryxxx_todo_changemevaluemessagerrrrBs<          zSSHClient.connectcCs |jS)z Open an SFTP session on the SSH server. :rtype: :class:`paramiko.sftp_client.SFTPClient` :return: An SFTP client object. )r open_sftprrrrr6ds zSSHClient.open_sftpcC|}|||dS)a 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)r6getrsrcdst sftp_clientrrrget_filem zSSHClient.get_filecCr8)a 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)r6putr:rrrput_file|r?zSSHClient.put_filercCs|}||||S)a 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. )r6open)rfilenamemodebufsizer=rrrrDszSSHClient.opencCs|}||S)a= 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. )r6listdir)rrr=rrrrHs zSSHClient.listdircC$|d|}|ddrdSdS)a 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. z[ -d %s ] || echo "FALSE"r&FALSErrun startswithrrstatusrrrisdirs zSSHClient.isdircCrI)aX 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. z[ -a %s ] || echo "FALSE"r&rJrrKrNrrrexistsszSSHClient.existscCs|j}t|dS)zJ Start an interactive shell session with the remote host. N)r invoke_shellr)rchannelrrrshells  zSSHClient.shellcCstjd||jjfd}z|j|}Wn tjy"d}Ynw|d }|d }|d |d |d tjd|tjd||||fS)a^ 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. running:%s on %srr&z stdout: %sz stderr: %s) botologdebugr instance_idr exec_commandr SSHExceptionreadclose)rcommandrOtstd_outstd_errrrrrLs       z SSHClient.runcCs<tjd||jjf|j}|| ||S)a8 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. rU) rWrXrYrrZr get_transport open_sessionget_ptyr[)rr_rSrrrrun_ptys   zSSHClient.run_ptycCs |j}||jdS)zQ Close an SSH session and any open channels that are tied to it. N)rrcr^rreset_cmdshell)r transportrrrr^s zSSHClient.close)rrNN)r )rBrC)__name__ __module__ __qualname____doc__rrr6r>rArDrHrPrQrTrLrfr^rrrrr&s"  "   rc@sZeZdZdZdddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ dS) LocalClientz :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'. NrcCs||_||_||_dSN)rr r )rrr r rrrrs zLocalClient.__init__cCt||dSz< Copy a file from one directory to another. Nshutilcopyfilerr;r<rrrr>zLocalClient.get_filecCrorprqrtrrrrA ruzLocalClient.put_filecCs t|S)z 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. )rrHrrrrrrH&s zLocalClient.listdircC tj|S)z Check the specified path to determine if it is a directory. :rtype: boolean :return: Returns True if the path is an existing directory. )rrrPrvrrrrP0 zLocalClient.isdircCrw)z 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. )rrrQrvrrrrQ9rxzLocalClient.existscCstd)Nz$shell not supported with LocalClient)NotImplementedErrorr7rrrrTBszLocalClient.shellcCstjd|jt}tj|jdtjtjtjd}|dur=t d| }| |d| |d|dus tj| tjd| |j| fS)z 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. z running:%sT)rTstdinstdoutstderrNr&rz output: %s)rWrXinfor_r subprocessPopenPIPEpollr,r- communicatewritegetvalue returncode)rlog_fpprocessr`rrrrLEs   zLocalClient.runcCsdSrnrr7rrrr^ZszLocalClient.close)Nr) rirjrkrlrr>rArHrPrQrTrLr^rrrrrms     rmc@seZdZdZddZdS) FakeServerae 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. cCs"||_||_|j|_|jj|_dSrn)instancerdns_namer'idrZ)rrrrrrrgszFakeServer.__init__N)rirjrkrlrrrrrr]s rcCs*tjddd}||jkrt|St|S)a 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. Instancez instance-idN)rWconfigr9rZrmr)rrZrrrstartms rrrcCst||}t||||S)a 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. )rr)rrr user_namersrrrsshclient_from_instance|s r)rrN)rlboto.mashups.interactiverrWrr,rrr r(r~ boto.compatrobjectrrmrrrrrrrs$  jN