Package paramiko :: Module transport :: Class BaseTransport
[show private | hide private]
[frames | no frames]

Class BaseTransport

object --+        
         |        
  _Verbose --+    
             |    
        Thread --+
                 |
                BaseTransport

Known Subclasses:
Transport

Handles protocol negotiation, key exchange, encryption, and the creation of channels across an SSH session. Basically everything but authentication is done here.
Method Summary
  __init__(self, sock)
Create a new SSH session over an existing socket, or socket-like object.
string __repr__(self)
Returns a string representation of this object, for debugging.
  accept(self, timeout)
  add_server_key(self, key)
Add a host key to the list of keys used for server mode.
Channel check_channel_request(self, kind, chanid)
(subclass override) Determine if a channel request of a given type will be granted, and return a suitable Channel object.
  close(self)
Close this session, and any open channels that are tied to it.
  connect(self, hostkeytype, hostkey, username, password, pkey)
Negotiate an SSH2 session, and optionally verify the server's host key and authenticate using a password or private key.
(string, string) get_remote_server_key(self)
Return the host key of the server (in client mode).
PKey get_server_key(self)
Return the active host key, in server mode.
boolean is_active(self)
Return true if this session is active (open).
boolean load_server_moduli(filename)
(optional) Load a file of prime moduli for use in doing group-exchange key negotiation in server mode. (Static method)
Channel open_channel(self, kind)
Request a new channel to the server.
Channel open_session(self)
Request a new channel to the server, of type "session".
boolean renegotiate_keys(self)
Force this session to switch to new keys.
  start_client(self, event)
Negotiate a new SSH2 session as a client.
  start_server(self, event)
Negotiate a new SSH2 session as a server.
  _activate_inbound(self)
switch on newly negotiated encryption parameters for inbound traffic
  _activate_outbound(self)
switch on newly negotiated encryption parameters for outbound traffic
  _build_packet(self, payload)
  _check_banner(self)
  _compute_key(self, id, nbytes)
id is 'A' - 'F' for the various keys used by ssh
  _expect_packet(self, type)
used by a kex object to register the next packet type it expects to see
  _get_cipher(self, name, key, iv)
  _get_modulus_pack(self)
used by KexGex to find primes for group exchange
  _key_from_blob(self, keytype, keyblob)
  _log(self, level, msg)
  _negotiate_keys(self, m)
  _parse_channel_open(self, m)
  _parse_channel_open_failure(self, m)
  _parse_channel_open_success(self, m)
  _parse_debug(self, m)
  _parse_disconnect(self, m)
  _parse_kex_init(self, m)
  _parse_newkeys(self, m)
  _read_all(self, n)
  _read_message(self)
only one thread will ever be in this function
  _run(self)
  _send_kex_init(self)
announce to the other side that we'd like to negotiate keys, and what kind of key negotiation we support.
  _send_message(self, data)
  _set_K_H(self, k, h)
used by a kex object to set the K (root key) and H (exchange hash)
  _unlink_channel(self, chanid)
used by a Channel to remove itself from the active channel list
  _verify_key(self, host_key, sig)
  _write_all(self, out)
    Inherited from Thread
  getName(self)
  isAlive(self)
  isDaemon(self)
  join(self, timeout)
  run(self)
  setDaemon(self, daemonic)
  setName(self, name)
  start(self)
  _set_daemon(self)
    Inherited from _Verbose
  _note(self, format, *args)
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
x.__hash__() <==> hash(x)
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value
  __str__(x)
x.__str__() <==> str(x)
    Inherited from type
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T

Class Variable Summary
int OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED = 1                                                                     
int OPEN_FAILED_CONNECT_FAILED = 2                                                                     
int OPEN_FAILED_RESOURCE_SHORTAGE = 4                                                                     
int OPEN_FAILED_UNKNOWN_CHANNEL_TYPE = 3                                                                     
list preferred_ciphers = ['aes128-cbc', 'blowfish-cbc', 'aes2...
list preferred_kex = ['diffie-hellman-group1-sha1', 'diffie-h...
list preferred_keys = ['ssh-rsa', 'ssh-dss']
list preferred_macs = ['hmac-sha1', 'hmac-md5', 'hmac-sha1-96...
int REKEY_BYTES = 1073741824                                                            
int REKEY_PACKETS = 1073741824                                                            
dict _channel_handler_table = {96: <unbound method Channel._h...
dict _cipher_info = {'blowfish-cbc': {'block-size': 8, 'key-s...
str _CLIENT_ID = 'pyssh_1.1'
dict _handler_table = {20: <function _negotiate_keys at 0x403...
dict _kex_info = {'diffie-hellman-group1-sha1': <class 'param...
dict _mac_info = {'hmac-sha1': {'class': <module 'Crypto.Hash...
NoneType _modulus_pack = None                                                                  
str _PROTO_ID = '2.0'
    Inherited from Thread
bool _Thread__initialized = False

Instance Method Details

__init__(self, sock)
(Constructor)

Create a new SSH session over an existing socket, or socket-like object. This only creates the Transport object; it doesn't begin the SSH session yet. Use connect or start_client to begin a client session, or start_server to begin a server session.

If the object is not actually a socket, it must have the following methods:
  • settimeout(float): Sets a timeout for read & write calls.
  • send(string): Writes from 1 to len(string) bytes, and returns an int representing the number of bytes written. Returns 0 or raises EOFError if the stream has been closed.
  • recv(int): Reads from 1 to int bytes and returns them as a string. Returns 0 or raises EOFError if the stream has been closed.
Parameters:
sock - a socket or socket-like object to create the session over.
           (type=socket)
Overrides:
threading.Thread.__init__

__repr__(self)
(Representation operator)

Returns a string representation of this object, for debugging.
Returns:
string
Overrides:
threading.Thread.__repr__

add_server_key(self, key)

Add a host key to the list of keys used for server mode. When behaving as a server, the host key is used to sign certain packets during the SSH2 negotiation, so that the client can trust that we are who we say we are. Because this is used for signing, the key must contain private key info, not just the public half.
Parameters:
key - the host key to add, usually an RSAKey or DSSKey.
           (type=PKey)

check_channel_request(self, kind, chanid)

(subclass override) Determine if a channel request of a given type will be granted, and return a suitable Channel object. This method is called in server mode when the client requests a channel, after authentication is complete.

In server mode, you will generally want to subclass Channel to override some of the methods for handling client requests (such as connecting to a subsystem or opening a shell) to determine what you want to allow or disallow. For this reason, check_channel_request must return a new object of that type. The chanid parameter is passed so that you can use it in Channel's constructor.

The default implementation always returns None, rejecting any channel requests. A useful server must override this method.
Parameters:
kind - the kind of channel the client would like to open (usually "session").
           (type=string)
chanid - ID of the channel, required to create a new Channel object.
           (type=int)
Returns:
a new Channel object (or subclass thereof), or None to refuse the request.
           (type=Channel)

close(self)

Close this session, and any open channels that are tied to it.

connect(self, hostkeytype=None, hostkey=None, username='', password=None, pkey=None)

Negotiate an SSH2 session, and optionally verify the server's host key and authenticate using a password or private key. This is a shortcut for start_client, get_remote_server_key, and Transport.auth_password or Transport.auth_publickey. Use those methods if you want more control.

You can use this method immediately after creating a Transport to negotiate encryption with a server. If it fails, an exception will be thrown. On success, the method will return cleanly, and an encrypted session exists. You may immediately call open_channel or open_session to get a Channel object, which is used for data transfer.
Parameters:
hostkeytype - the type of host key expected from the server (usually "ssh-rsa" or "ssh-dss"), or None if you don't want to do host key verification.
           (type=string)
hostkey - the host key expected from the server, or None if you don't want to do host key verification.
           (type=string)
username - the username to authenticate as.
           (type=string)
password - a password to use for authentication, if you want to use password authentication; otherwise None.
           (type=string)
pkey - a private key to use for authentication, if you want to use private key authentication; otherwise None.
           (type=PKey)
Raises:
SSHException - if the SSH2 negotiation fails, the host key supplied by the server is incorrect, or authentication fails.

Note: If you fail to supply a password or private key, this method may succeed, but a subsequent open_channel or open_session call may fail because you haven't authenticated yet.

Since: doduo

get_remote_server_key(self)

Return the host key of the server (in client mode). The type string is usually either "ssh-rsa" or "ssh-dss" and the key is an opaque string, which may be saved or used for comparison with previously-seen keys. (In other words, you don't need to worry about the content of the key, only that it compares equal to the key you expected to see.)
Returns:
tuple of (key type, key)
           (type=(string, string))
Raises:
SSHException - if no session is currently active.

get_server_key(self)

Return the active host key, in server mode. After negotiating with the client, this method will return the negotiated host key. If only one type of host key was set with add_server_key, that's the only key that will ever be returned. But in cases where you have set more than one type of host key (for example, an RSA key and a DSS key), the key type will be negotiated by the client, and this method will return the key of the type agreed on. If the host key has not been negotiated yet, None is returned. In client mode, the behavior is undefined.
Returns:
host key of the type negotiated by the client, or None.
           (type=PKey)

is_active(self)

Return true if this session is active (open).
Returns:
True if the session is still active (open); False if the session is closed.
           (type=boolean)

open_channel(self, kind)

Request a new channel to the server. Channels are socket-like objects used for the actual transfer of data across the session. You may only request a channel after negotiating encryption (using connect or start_client and authenticating.
Parameters:
kind - the kind of channel requested (usually "session").
           (type=string)
Returns:
a new Channel on success, or None if the request is rejected or the session ends prematurely.
           (type=Channel)

open_session(self)

Request a new channel to the server, of type "session". This is just an alias for open_channel('session').
Returns:
a new Channel on success, or None if the request is rejected or the session ends prematurely.
           (type=Channel)

renegotiate_keys(self)

Force this session to switch to new keys. Normally this is done automatically after the session hits a certain number of packets or bytes sent or received, but this method gives you the option of forcing new keys whenever you want. Negotiating new keys causes a pause in traffic both ways as the two sides swap keys and do computations. This method returns when the session has switched to new keys, or the session has died mid-negotiation.
Returns:
True if the renegotiation was successful, and the link is using new keys; False if the session dropped during renegotiation.
           (type=boolean)

start_client(self, event=None)

Negotiate a new SSH2 session as a client. This is the first step after creating a new Transport. A separate thread is created for protocol negotiation, so this method returns immediately.

When negotiation is done (successful or not), the given Event will be triggered. On failure, is_active will return False.

After a successful negotiation, you will usually want to authenticate, calling auth_password or auth_publickey.
Parameters:
event - an event to trigger when negotiation is complete.
           (type=threading.Event)

Notes:

  • connect is a simpler method for connecting as a client.
  • After calling this method (or start_server or connect), you should no longer directly read from or write to the original socket object.

start_server(self, event=None)

Negotiate a new SSH2 session as a server. This is the first step after creating a new Transport and setting up your server host key(s). A separate thread is created for protocol negotiation, so this method returns immediately.

When negotiation is done (successful or not), the given Event will be triggered. On failure, is_active will return False.

After a successful negotiation, the client will need to authenticate. Override the methods get_allowed_auths, check_auth_none, check_auth_password, and check_auth_publickey to control the authentication process.

After a successful authentication, the client should request to open a channel. Override check_channel_request to allow channels to be opened.
Parameters:
event - an event to trigger when negotiation is complete.
           (type=threading.Event)

Note: After calling this method (or start_client or connect), you should no longer directly read from or write to the original socket object.

_activate_inbound(self)

switch on newly negotiated encryption parameters for inbound traffic

_activate_outbound(self)

switch on newly negotiated encryption parameters for outbound traffic

_compute_key(self, id, nbytes)

id is 'A' - 'F' for the various keys used by ssh

_expect_packet(self, type)

used by a kex object to register the next packet type it expects to see

_get_modulus_pack(self)

used by KexGex to find primes for group exchange

_read_message(self)

only one thread will ever be in this function

_send_kex_init(self)

announce to the other side that we'd like to negotiate keys, and what kind of key negotiation we support.

_set_K_H(self, k, h)

used by a kex object to set the K (root key) and H (exchange hash)

_unlink_channel(self, chanid)

used by a Channel to remove itself from the active channel list

Static Method Details

load_server_moduli(filename=None)

(optional) Load a file of prime moduli for use in doing group-exchange key negotiation in server mode. It's a rather obscure option and can be safely ignored.

In server mode, the remote client may request "group-exchange" key negotiation, which asks the server to send a random prime number that fits certain criteria. These primes are pretty difficult to compute, so they can't be generated on demand. But many systems contain a file of suitable primes (usually named something like /etc/ssh/moduli). If you call load_server_moduli and it returns True, then this file of primes has been loaded and we will support "group-exchange" in server mode. Otherwise server mode will just claim that it doesn't support that method of key negotiation.
Parameters:
filename - optional path to the moduli file, if you happen to know that it's not in a standard location.
           (type=string)
Returns:
True if a moduli file was successfully loaded; False otherwise.
           (type=boolean)

Note: This has no effect when used in client mode.

Since: doduo


Class Variable Details

OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED

Type:
int
Value:
1                                                                     

OPEN_FAILED_CONNECT_FAILED

Type:
int
Value:
2                                                                     

OPEN_FAILED_RESOURCE_SHORTAGE

Type:
int
Value:
4                                                                     

OPEN_FAILED_UNKNOWN_CHANNEL_TYPE

Type:
int
Value:
3                                                                     

preferred_ciphers

Type:
list
Value:
['aes128-cbc', 'blowfish-cbc', 'aes256-cbc', '3des-cbc']               

preferred_kex

Type:
list
Value:
['diffie-hellman-group1-sha1', 'diffie-hellman-group-exchange-sha1']   

preferred_keys

Type:
list
Value:
['ssh-rsa', 'ssh-dss']                                                 

preferred_macs

Type:
list
Value:
['hmac-sha1', 'hmac-md5', 'hmac-sha1-96', 'hmac-md5-96']               

REKEY_BYTES

Type:
int
Value:
1073741824                                                            

REKEY_PACKETS

Type:
int
Value:
1073741824                                                            

_channel_handler_table

Type:
dict
Value:
{93: <unbound method Channel._window_adjust>,
 94: <unbound method Channel._feed>,
 96: <unbound method Channel._handle_eof>,
 97: <unbound method Channel._handle_close>,
 98: <unbound method Channel._handle_request>,
 99: <unbound method Channel._request_success>,
 100: <unbound method Channel._request_failed>}                        

_cipher_info

Type:
dict
Value:
{'3des-cbc': {'block-size': 8,
              'class': <module 'Crypto.Cipher.DES3' from '/usr/lib/pyt\
hon2.3/site-packages/Crypto/Cipher/DES3.so'>,
              'key-size': 24,
              'mode': 2},
 'aes128-cbc': {'block-size': 16,
                'class': <module 'Crypto.Cipher.AES' from '/usr/lib/py\
thon2.3/site-packages/Crypto/Cipher/AES.so'>,
...                                                                    

_CLIENT_ID

Type:
str
Value:
'pyssh_1.1'                                                            

_handler_table

Type:
dict
Value:
{20: <function _negotiate_keys at 0x403d28b4>,
 21: <function _parse_newkeys at 0x403d2a04>,
 90: <function _parse_channel_open at 0x403d2ae4>,
 91: <function _parse_channel_open_success at 0x403d2a74>,
 92: <function _parse_channel_open_failure at 0x403d2aac>}             

_kex_info

Type:
dict
Value:
{'diffie-hellman-group-exchange-sha1': <class 'paramiko.kex_gex.KexGex\
'>,
 'diffie-hellman-group1-sha1': <class 'paramiko.kex_group1.KexGroup1'>\
}                                                                      

_mac_info

Type:
dict
Value:
{'hmac-md5': {'class': <module 'Crypto.Hash.MD5' from '/usr/lib/python\
2.3/site-packages/Crypto/Hash/MD5.pyc'>,
              'size': 16},
 'hmac-md5-96': {'class': <module 'Crypto.Hash.MD5' from '/usr/lib/pyt\
hon2.3/site-packages/Crypto/Hash/MD5.pyc'>,
                 'size': 12},
 'hmac-sha1': {'class': <module 'Crypto.Hash.SHA' from '/usr/lib/pytho\
n2.3/site-packages/Crypto/Hash/SHA.pyc'>,
...                                                                    

_modulus_pack

Type:
NoneType
Value:
None                                                                  

_PROTO_ID

Type:
str
Value:
'2.0'                                                                  

Generated by Epydoc 2.0 on Sun Jan 4 02:33:11 2004 http://epydoc.sf.net