ó õùPc@s>dZddlmZddlZddlZddlZddlZddlmZddl Tddl m Z ddl m Z ddlmZdd lmZdd lmZmZdd lmZd Zd efd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdS(s L{SSHClient}. iÿÿÿÿ(thexlifyN(tAgent(t*(tDSSKey(tHostKeys(tResourceManager(tRSAKey(t SSHExceptiontBadHostKeyException(t TransportitMissingHostKeyPolicycBseZdZd„ZRS(sÄ Interface for defining the policy that L{SSHClient} should use when the SSH server's hostname is not in either the system host keys or the application's keys. Pre-made classes implement policies for automatically adding the key to the application's L{HostKeys} object (L{AutoAddPolicy}), and for automatically rejecting the key (L{RejectPolicy}). This function may be used to ask the user to verify the key, for example. cCsdS(s Called when an L{SSHClient} receives a server key for a server that isn't in either the system or local L{HostKeys} object. To accept the key, simply return. To reject, raised an exception (which will be passed to the calling application). N((tselftclientthostnametkey((s*..\python\site-packages\paramiko\client.pytmissing_host_key4s(t__name__t __module__t__doc__R(((s*..\python\site-packages\paramiko\client.pyR )s t AutoAddPolicycBseZdZd„ZRS(sš Policy for automatically adding the hostname and new host key to the local L{HostKeys} object, and saving it. This is used by L{SSHClient}. cCsq|jj||jƒ|ƒ|jdk r>|j|jƒn|jtd|jƒ|t|j ƒƒfƒdS(NsAdding %s host key for %s: %s( t _host_keystaddtget_namet_host_keys_filenametNonetsave_host_keyst_logtDEBUGRtget_fingerprint(R R R R((s*..\python\site-packages\paramiko\client.pyRDs  (RRRR(((s*..\python\site-packages\paramiko\client.pyR>st RejectPolicycBseZdZd„ZRS(sk Policy for automatically rejecting the unknown hostname & key. This is used by L{SSHClient}. cCsC|jtd|jƒ|t|jƒƒfƒtd|ƒ‚dS(Ns Rejecting %s host key for %s: %ssUnknown server %s(RRRRRR(R R R R((s*..\python\site-packages\paramiko\client.pyRRs #(RRRR(((s*..\python\site-packages\paramiko\client.pyRLst WarningPolicycBseZdZd„ZRS(s€ Policy for logging a python-style warning for an unknown host key, but accepting it. This is used by L{SSHClient}. cCs0tjd|jƒ|t|jƒƒfƒdS(NsUnknown %s host key for %s: %s(twarningstwarnRRR(R R R R((s*..\python\site-packages\paramiko\client.pyR]s (RRRR(((s*..\python\site-packages\paramiko\client.pyRXst SSHClientc Bs¿eZdZd„Zdd„Zd„Zd„Zd„Zd„Z d„Z e ddddde e e d„ Zd „Zd d „Zd d dd„Zd„Zd„Zd„Zd„ZRS(se A high-level representation of a session with an SSH server. This class wraps L{Transport}, L{Channel}, and L{SFTPClient} to take care of most aspects of authenticating and opening channels. A typical use case is:: client = SSHClient() client.load_system_host_keys() client.connect('ssh.example.com') stdin, stdout, stderr = client.exec_command('ls -l') You may pass in explicit overrides for authentication and server host key checking. The default mechanism is to try to use local key files or an SSH agent (if one is running). @since: 1.6 cCsLtƒ|_tƒ|_d|_d|_tƒ|_d|_d|_ dS(s) Create a new SSHClient. N( Rt_system_host_keysRRRt _log_channelRt_policyt _transportt_agent(R ((s*..\python\site-packages\paramiko\client.pyt__init__ts      cCs^|dkrJtjjdƒ}y|jj|ƒWntk rEnXdS|jj|ƒdS(s Load host keys from a system (read-only) file. Host keys read with this method will not be saved back by L{save_host_keys}. This method can be called multiple times. Each new set of host keys will be merged with the existing set (new replacing old if there are conflicts). If C{filename} is left as C{None}, an attempt will be made to read keys from the user's local "known hosts" file, as used by OpenSSH, and no exception will be raised if the file can't be read. This is probably only useful on posix. @param filename: the filename to read, or C{None} @type filename: str @raise IOError: if a filename was provided and the file could not be read s~/.ssh/known_hostsN(Rtostpatht expanduserR"tloadtIOError(R tfilename((s*..\python\site-packages\paramiko\client.pytload_system_host_keys€s  cCs||_|jj|ƒdS(sö Load host keys from a local host-key file. Host keys read with this method will be checked I{after} keys loaded via L{load_system_host_keys}, but will be saved back by L{save_host_keys} (so they can be modified). The missing host key policy L{AutoAddPolicy} adds keys to this set and saves them, when connecting to a previously-unknown server. This method can be called multiple times. Each new set of host keys will be merged with the existing set (new replacing old if there are conflicts). When automatically saving, the last hostname is used. @param filename: the filename to read @type filename: str @raise IOError: if the filename could not be read N(RRR+(R R-((s*..\python\site-packages\paramiko\client.pytload_host_keysžs cCs‡t|dƒ}|jdƒxZ|jjƒD]I\}}x:|jƒD],\}}|jd|||jƒfƒqEWq,W|jƒdS(sa Save the host keys back to a file. Only the host keys loaded with L{load_host_keys} (plus any added directly) will be saved -- not any host keys loaded with L{load_system_host_keys}. @param filename: the filename to save to @type filename: str @raise IOError: if the file could not be written tws&# SSH host keys collected by paramiko s %s %s %s N(topentwriteRt iteritemst get_base64tclose(R R-tfR tkeystkeytypeR((s*..\python\site-packages\paramiko\client.pyR²s  (cCs|jS(s¾ Get the local L{HostKeys} object. This can be used to examine the local host keys or change them. @return: the local host keys @rtype: L{HostKeys} (R(R ((s*..\python\site-packages\paramiko\client.pyt get_host_keysÄscCs ||_dS(sÑ Set the channel for logging. The default is C{"paramiko.transport"} but it can be set to anything you want. @param name: new channel name for logging @type name: str N(R#(R tname((s*..\python\site-packages\paramiko\client.pytset_log_channelÎscCs ||_dS(sÜ Set the policy to use when connecting to a server that doesn't have a host key in either the system or local L{HostKeys} objects. The default policy is to reject all unknown servers (using L{RejectPolicy}). You may substitute L{AutoAddPolicy} or write your own policy class. @param policy: the policy to use when receiving a host key from a previously-unknown server @type policy: L{MissingHostKeyPolicy} N(R$(R tpolicy((s*..\python\site-packages\paramiko\client.pytset_missing_host_key_policyØs c Cs€xtj||tjtjƒD]4\} } } }}| tjkr| }|}PqqWtj||tjtjƒ\}}}}}tj|tjƒ}|dk rÃy|j|ƒWqÃqÃXn|j|ƒt|ƒ}|_|j d| ƒ|j dk r|j |j ƒn|j ƒt j||ƒ|jƒ}|jƒ}|tkr\|}nd||f}|jj|iƒj|dƒ}|dkr½|jj|iƒj|dƒ}n|dkrè|jj|||ƒ|}n||kr t|||ƒ‚n|dkr$tjƒ}n|dkr9g}n't|ttfƒrZ|g}n|}|j|||||| ƒdS(s• Connect to an SSH server and authenticate to it. The server's host key is checked against the system host keys (see L{load_system_host_keys}) and any local host keys (L{load_host_keys}). If the server's hostname is not found in either set of host keys, the missing host key policy is used (see L{set_missing_host_key_policy}). The default policy is to reject the key and raise an L{SSHException}. Authentication is attempted in the following order of priority: - The C{pkey} or C{key_filename} passed in (if any) - Any key we can find through an SSH agent - Any "id_rsa" or "id_dsa" key discoverable in C{~/.ssh/} - Plain username/password auth, if a password was given If a private key requires a password to unlock it, and a password is passed in, that password will be used to attempt to unlock the key. @param hostname: the server to connect to @type hostname: str @param port: the server port to connect to @type port: int @param username: the username to authenticate as (defaults to the current local username) @type username: str @param password: a password to use for authentication or for unlocking a private key @type password: str @param pkey: an optional private key to use for authentication @type pkey: L{PKey} @param key_filename: the filename, or list of filenames, of optional private key(s) to try for authentication @type key_filename: str or list(str) @param timeout: an optional timeout (in seconds) for the TCP connect @type timeout: float @param allow_agent: set to False to disable connecting to the SSH agent @type allow_agent: bool @param look_for_keys: set to False to disable searching for discoverable private key files in C{~/.ssh/} @type look_for_keys: bool @param compress: set to True to turn on compression @type compress: bool @raise BadHostKeyException: if the server's host key could not be verified @raise AuthenticationException: if authentication failed @raise SSHException: if there was any other error connecting or establishing an SSH session @raise socket.error: if a socket error occurred while connecting tcompresss[%s]:%dN(tsockett getaddrinfot AF_UNSPECt SOCK_STREAMRt settimeouttconnectR R%tuse_compressionR#R;t start_clientRtregistertget_remote_server_keyRtSSH_PORTR"tgetRR$RRtgetpasstgetusert isinstancetstrtunicodet_auth(R R tporttusernametpasswordtpkeyt key_filenamettimeoutt allow_agentt look_for_keysR>tfamilytsocktypetprotot canonnametsockaddrtaftaddrt_tsockttt server_keyR8tserver_hostkey_nametour_server_keyt key_filenames((s*..\python\site-packages\paramiko\client.pyRDåsP54-       ! $       cCsU|jdkrdS|jjƒd|_|jdkrQ|jjƒd|_ndS(sG Close this SSHClient and its underlying L{Transport}. N(R%RR5R&(R ((s*..\python\site-packages\paramiko\client.pyR5Ns   iÿÿÿÿcCs_|jjƒ}|j|ƒ|jd|ƒ}|jd|ƒ}|jd|ƒ}|||fS(s« Execute a command on the SSH server. A new L{Channel} is opened and the requested command is executed. The command's input and output streams are returned as python C{file}-like objects representing stdin, stdout, and stderr. @param command: the command to execute @type command: str @param bufsize: interpreted the same way as by the built-in C{file()} function in python @type bufsize: int @return: the stdin, stdout, and stderr of the executing command @rtype: tuple(L{ChannelFile}, L{ChannelFile}, L{ChannelFile}) @raise SSHException: if the server fails to execute the command twbtrb(R%t open_sessiont exec_commandtmakefiletmakefile_stderr(R tcommandtbufsizetchantstdintstdouttstderr((s*..\python\site-packages\paramiko\client.pyRj[s  tvt100iPicCs0|jjƒ}|j|||ƒ|jƒ|S(s† Start an interactive shell session on the SSH server. A new L{Channel} is opened and connected to a pseudo-terminal using the requested terminal type and size. @param term: the terminal type to emulate (for example, C{"vt100"}) @type term: str @param width: the width (in characters) of the terminal window @type width: int @param height: the height (in characters) of the terminal window @type height: int @return: a new channel connected to the remote shell @rtype: L{Channel} @raise SSHException: if the server fails to invoke a shell (R%Ritget_ptyt invoke_shell(R ttermtwidththeightRo((s*..\python\site-packages\paramiko\client.pyRurs cCs |jjƒS(sƒ Open an SFTP session on the SSH server. @return: a new SFTP session object @rtype: L{SFTPClient} (R%topen_sftp_client(R ((s*..\python\site-packages\paramiko\client.pyt open_sftpˆscCs|jS(s  Return the underlying L{Transport} object for this SSH connection. This can be used to perform lower-level tasks, like opening specific kinds of channels. @return: the Transport for this connection @rtype: L{Transport} (R%(R ((s*..\python\site-packages\paramiko\client.pyt get_transport‘s cCs¡d}|dk rly;|jtdt|jƒƒƒ|jj||ƒdSWqltk rh}|}qlXnx”|D]Œ} xƒtt fD]u} yS| j | |ƒ} |jtdt| jƒƒ| fƒ|jj|| ƒdSWq†tk rú}|}q†Xq†WqsW|r›|j dkr't ƒ|_ nxq|j j ƒD]]} y;|jtdt| jƒƒƒ|jj|| ƒdSWq7tk r“}|}q7Xq7Wng} tjjdƒ} tjjdƒ}tjj| ƒrí| jt| fƒntjj|ƒr| jt |fƒntjjdƒ} tjjdƒ}tjj| ƒra| jt| fƒntjj|ƒr‰| jt |fƒn|s˜g} nx›| D]“\} }yS| j ||ƒ} |jtd t| jƒƒ|fƒ|jj|| ƒdSWqŸtk r}|}qŸtk r1}|}qŸXqŸW|dk r|y|jj||ƒdSWq|tk rx}|}q|Xn|dk r‘|‚ntd ƒ‚dS( st Try, in order: - The key passed in, if one was passed in. - Any key we can find through an SSH agent (if allowed). - Any "id_rsa" or "id_dsa" key discoverable in ~/.ssh/ (if allowed). - Plain username/password auth, if a password was given. (The password might be needed to unlock a private key.) sTrying SSH key %sNsTrying key %s from %ssTrying SSH agent key %ss ~/.ssh/id_rsas ~/.ssh/id_dsas ~/ssh/id_rsas ~/ssh/id_dsasTrying discovered key %s in %ss#No authentication methods available(RRRRRR%tauth_publickeyRRRtfrom_private_key_fileR&Rtget_keysR(R)R*tisfiletappendR,t auth_password(R RRRSRTRfRWRXtsaved_exceptionteRUt pkey_classRtkeyfilestrsa_keytdsa_keyR-((s*..\python\site-packages\paramiko\client.pyRPœsz     &  &     cCs|jj||ƒdS(N(R%R(R tleveltmsg((s*..\python\site-packages\paramiko\client.pyRðsN(RRRR'RR.R/RR9R;R=RItTruetFalseRDR5RjRuRzR{RPR(((s*..\python\site-packages\paramiko\client.pyR!bs$      g  T(RtbinasciiRRKR(R?Rtparamiko.agentRtparamiko.commontparamiko.dsskeyRtparamiko.hostkeysRtparamiko.resourceRtparamiko.rsakeyRtparamiko.ssh_exceptionRRtparamiko.transportR RItobjectR RRRR!(((s*..\python\site-packages\paramiko\client.pyts&