paramiko.SSHClient

Package paramiko :: Class SSHClient |
[show private | hide private]
[frames | no frames]

## Type SSHClient

    object --+
             |
            SSHClient

A high-level representation of a session with an SSH server. This class wraps `Transport`, `Channel`, and `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

Method Summary
| ``__init__`(self)` Create a new SSHClient.
| ``close`(self)` Close this SSHClient and its underlying `Transport`.
| ``connect`(self, hostname, port, username, password, pkey, key_filename)` Connect to an SSH server and authenticate to it.
tuple(`ChannelFile`, `ChannelFile`, `ChannelFile`) | ``exec_command`(self, command)` Execute a command on the SSH server.
`HostKeys` | ``get_host_keys`(self)` Get the local `HostKeys` object.
`Channel` | ``invoke_shell`(self, term, width, height)` Start an interactive shell session on the SSH server.
| ``load_host_keys`(self, filename)` Load host keys from a local host-key file.
| ``load_system_host_keys`(self, filename)` Load host keys from a system (read-only) file.
`SFTPClient` | ``open_sftp`(self)` Open an SFTP session on the SSH server.
| ``save_host_keys`(self, filename)` Save the host keys back to a file.
| ``set_log_channel`(self, name)` Set the channel for logging.
| ``set_missing_host_key_policy`(self, policy)` Set the policy to use when connecting to a server that doesn't have a host key in either the system or local `HostKeys` objects.
Inherited from object
| ``__delattr__`(...)` x.__delattr__('name') <==> del x.name
| ``__getattribute__`(...)` x.__getattribute__('name') <==> x.name
| ``__hash__`(x)` x.__hash__() <==> hash(x)
| ``__new__`(T, S, ...)` T.__new__(S, ...) -> a new object with type S, a subtype of T
| ``__reduce__`(...)` helper for pickle
| ``__reduce_ex__`(...)` helper for pickle
| ``__repr__`(x)` x.__repr__() <==> repr(x)
| ``__setattr__`(...)` x.__setattr__('name', value) <==> x.name = value
| ``__str__`(x)` x.__str__() <==> str(x)

Method Details

### __init__(self) (Constructor) Create a new SSHClient. Overrides: `__builtin__.object.__init__`

### close(self) Close this SSHClient and its underlying `Transport`.

### connect(self, hostname, port=22, username=None, password=None, pkey=None, key_filename=None) Connect to an SSH server and authenticate to it. The server's host key is checked against the system host keys (see `load_system_host_keys`) and any local host keys (`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 `set_missing_host_key_policy`). The default policy is to reject the key and raise an `SSHException`. Authentication is attempted in the following order of priority: The `pkey` or `key_filename` passed in (if any) Any key we can find through an SSH agent Any "id_rsa" or "id_dsa" key discoverable in `~/.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. Parameters: `hostname` - the server to connect to (type=str) `port` - the server port to connect to (type=int) `username` - the username to authenticate as (defaults to the current local username) (type=str) `password` - a password to use for authentication or for unlocking a private key (type=str) `pkey` - an optional private key to use for authentication (type=`PKey`) `key_filename` - the filename of an optional private key to use for authentication (type=str) Raises: `BadHostKeyException` - if the server's host key could not be verified `AuthenticationException` - if authentication failed `SSHException` - if there was any other error connecting or establishing an SSH session

### exec_command(self, command) Execute a command on the SSH server. A new `Channel` is opened and the requested command is executed. The command's input and output streams are returned as python `file`-like objects representing stdin, stdout, and stderr. Parameters: `command` - the command to execute (type=str) Returns: the stdin, stdout, and stderr of the executing command (type=tuple(`ChannelFile`, `ChannelFile`, `ChannelFile`)) Raises: `SSHException` - if the server fails to execute the command

### get_host_keys(self) Get the local `HostKeys` object. This can be used to examine the local host keys or change them. Returns: the local host keys (type=`HostKeys`)

### invoke_shell(self, term='vt100', width=80, height=24) Start an interactive shell session on the SSH server. A new `Channel` is opened and connected to a pseudo-terminal using the requested terminal type and size. Parameters: `term` - the terminal type to emulate (for example, `"vt100"`) (type=str) `width` - the width (in characters) of the terminal window (type=int) `height` - the height (in characters) of the terminal window (type=int) Returns: a new channel connected to the remote shell (type=`Channel`) Raises: `SSHException` - if the server fails to invoke a shell

### load_host_keys(self, filename) Load host keys from a local host-key file. Host keys read with this method will be checked after keys loaded via `load_system_host_keys`, but will be saved back by `save_host_keys` (so they can be modified). The missing host key policy `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. Parameters: `filename` - the filename to read (type=str) Raises: `IOError` - if the filename could not be read

### load_system_host_keys(self, filename=None) Load host keys from a system (read-only) file. Host keys read with this method will not be saved back by `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 `filename` is left as `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. Parameters: `filename` - the filename to read, or `None` (type=str) Raises: `IOError` - if a filename was provided and the file could not be read

### open_sftp(self) Open an SFTP session on the SSH server. Returns: a new SFTP session object (type=`SFTPClient`)

### save_host_keys(self, filename) Save the host keys back to a file. Only the host keys loaded with `load_host_keys` (plus any added directly) will be saved -- not any host keys loaded with `load_system_host_keys`. Parameters: `filename` - the filename to save to (type=str) Raises: `IOError` - if the file could not be written

### set_log_channel(self, name) Set the channel for logging. The default is `"paramiko.transport"` but it can be set to anything you want. Parameters: `name` - new channel name for logging (type=str)

### set_missing_host_key_policy(self, policy) Set the policy to use when connecting to a server that doesn't have a host key in either the system or local `HostKeys` objects. The default policy is to reject all unknown servers (using `RejectPolicy`). You may substitute `AutoAddPolicy` or write your own policy class. Parameters: `policy` - the policy to use when receiving a host key from a previously-unknown server (type=`MissingHostKeyPolicy`)

Generated by Epydoc 2.1 on Wed Aug 16 14:35:04 2006 | http://epydoc.sf.net