RedSSH

class redssh.RedSSH(encoding='utf8', terminal='vt100', known_hosts=None, ssh_host_key_verification=SSHHostKeyVerify.warn, ssh_keepalive_interval=0.0, set_flags={}, method_preferences={}, callbacks={}, auto_terminate_tunnels=False, tcp_nodelay=False)[source]

Bases: object

Instances the start of an SSH connection. Extra options are available after redssh.RedSSH.connect() is called.

Parameters
  • encoding (str) – Set the encoding to something other than the default of 'utf8' when your target SSH server doesn’t return UTF-8.

  • terminal (str) – Set the terminal sent to the remote server to something other than the default of 'vt100'.

  • known_hosts (str) – Set the known hosts file to a set location other than '~/.ssh/known_hosts', None is the default location.

  • ssh_host_key_verification (redssh.enums.SSHHostKeyVerify) – Change the behaviour of remote host key verification. Can be set to one of the following values, strict, warn, auto_add or none.

  • ssh_keepalive_interval (float) – Enable or disable SSH keepalive packets, value is interval in seconds, 0 is off.

  • set_flags (dict) – Not supported in ssh2-python 0.18.0

  • method_preferences (dict) – Not supported in ssh2-python 0.18.0

  • callbacks (dict) – Not supported yet

  • auto_terminate_tunnels (bool) – Automatically terminate tunnels when errors are detected

  • tcp_nodelay (bool) – Set TCP_NODELAY for the underlying socket.socket(), by default this is off via False.

check_closed(channel=None)[source]

Returns True or False when the main channel has recieved an EOF or an associated channel.

close_tunnels()[source]

Closes all SSH tunnels if any are open.

connect(hostname, port=22, username='', password=None, allow_agent=False, host_based=None, key_filepath=None, passphrase=None, look_for_keys=False, sock=None, timeout=None)[source]

Warning

Some authentication methods are not yet supported!

Parameters
  • hostname (str) – Hostname to connect to.

  • port (int) – SSH port to connect to.

  • username (str) – Username to connect as to the remote server.

  • password (str) – Password to offer to the remote server for authentication.

  • allow_agent (bool) – Allow the local SSH key agent to offer the keys held in it for authentication.

  • host_based (bool) – Allow the local SSH host keys to be used for authentication. NOT IMPLEMENTED!

  • key_filepath (array/str) – Array of filenames to offer to the remote server. Can be a string for a single key.

  • passphrase (str) – Passphrase to decrypt any keys offered to the remote server.

  • look_for_keys (bool) – Enable offering keys in ~/.ssh automatically. NOT IMPLEMENTED!

  • sock (socket.socket()) – A pre-connected socket to the remote server. Useful if you have strange network requirements.

  • timeout (float) – Timeout for the socket connection to the remote server.

dynamic_tunnel(local_port, bind_addr='127.0.0.1', error_level=TunnelErrorLevel.warn)[source]

Opens a SOCKS proxy AKA gateway or dynamic port the same way the -D option does for the OpenSSH client.

Providing a 0 for the local port will mean the OS will assign an unbound port for you. This port number will be provided to you by this function.

Parameters
  • local_port (int) – The local port on the local machine to bind to.

  • bind_addr (str) – The bind address on this machine to bind to for the local port.

  • error_level (redssh.enums.TunnelErrorLevel) – The level of verbosity that errors in tunnel threads will use.

Returns

int The local port that has been bound.

eof()[source]

Returns True or False when the main channel has recieved an EOF.

execute_command(command)[source]

Run a command. This will block as the command executes.

Parameters

command (str) – Command to execute.

Returns

tuple (int, str) - of (return_code, command_output)

exit()[source]

Kill the current session if connected.

flush()[source]

Flush all data on the primary channel’s stdin to the remote connection. Only works if connected, otherwise returns 0.

Returns

int - Amount of bytes sent to remote machine.

last_error()[source]

Get the last error from the current session.

Returns

str

local_tunnel(local_port, remote_host, remote_port, bind_addr='127.0.0.1', error_level=TunnelErrorLevel.warn)[source]

Forwards a port on the remote machine the same way the -L option does for the OpenSSH client.

Providing a 0 for the local port will mean the OS will assign an unbound port for you. This port number will be provided to you by this function.

Parameters
  • local_port (int) – The local port on the local machine to bind to.

  • remote_host (str) – The remote host to connect to via the remote machine.

  • remote_port (int) – The remote host’s port to connect to via the remote machine.

  • bind_addr (str) – The bind address on this machine to bind to for the local port.

  • error_level (redssh.enums.TunnelErrorLevel) – The level of verbosity that errors in tunnel threads will use.

Returns

int The local port that has been bound.

methods(method)[source]

Returns what value was settled on during session negotiation.

read(block=False)[source]

Recieve data from the remote session. Only works if the current session has made it past the login process.

Parameters

block (bool) – Block until data is received from the remote server. True will block until data is recieved and False may return b'' if no data is available from the remote server.

Returns

generator - A generator of byte strings that has been recieved in the time given.

remote_tunnel(local_port, remote_host, remote_port, bind_addr='127.0.0.1', error_level=TunnelErrorLevel.warn)[source]

Forwards a port to the remote machine via the local machine the same way the -R option does for the OpenSSH client.

Parameters
  • local_port (int) – The local port on the remote side for clients to connect to.

  • remote_host (str) – The remote host to connect to via the local machine.

  • remote_port (int) – The remote host’s port to connect to via the local machine.

  • error_level (redssh.enums.TunnelErrorLevel) – The level of verbosity that errors in tunnel threads will use.

Returns

None

send(string)[source]

Send data to the remote session. Only works if the current session has made it past the login process.

Parameters

string (str) – String to send to the remote session.

Returns

int - Amount of bytes sent to remote machine.

setenv(varname, value)[source]

Set an environment variable on the channel.

Parameters
  • varname (str) – Name of environment variable to set on the remote channel.

  • value (str) – Value to set varname to.

Returns

None

shutdown_tunnel(tunnel_type, sport, rhost=None, rport=None, bind_addr='127.0.0.1')[source]

Closes an open tunnel. Provide the same arguments to this that was given for openning the tunnel.

Examples:

local_tunnel(9999,’localhost’,8888) would be shutdown_tunnel(redssh.enums.TunnelType.local,9999,’localhost’,8888)

remote_tunnel(7777,’localhost’,8888) would be shutdown_tunnel(redssh.enums.TunnelType.remote,7777,’localhost’,8888)

dynamic_tunnel(9999) would be shutdown_tunnel(redssh.enums.TunnelType.dynamic,9999)

dynamic_tunnel(9999,’10.0.0.1’) would be shutdown_tunnel(redssh.enums.TunnelType.dynamic,9999,bind_addr=’10.0.0.1’)

Parameters
  • tunnel_type (redssh.enums.TunnelType) – The tunnel type to shutdown.

  • sport (str) – The bound port for local and dynamic tunnels or the local port on the remote side for remote tunnels.

  • rhost (str) – The remote host for local and remote tunnels.

  • rport (int) – The remote port for local and remote tunnels.

  • bind_addr (str) – The bind address used for local and dynamic tunnels.

Returns

None

start_scp()[source]

Start the SCP client.

Returns

None

start_sftp()[source]

Start the SFTP client. The client will be available at self.sftp and will be an instance of redssh.sftp.RedSFTP

Returns

None

tunnel_is_alive(tunnel_type, sport, rhost=None, rport=None, bind_addr='127.0.0.1')[source]

Checks if a tunnel is alive. Provide the same arguments to this that was given for openning the tunnel.

Examples:

local_tunnel(9999,’localhost’,8888) would be tunnel_is_alive(redssh.enums.TunnelType.local,9999,’localhost’,8888)

remote_tunnel(7777,’localhost’,8888) would be tunnel_is_alive(redssh.enums.TunnelType.remote,7777,’localhost’,8888)

dynamic_tunnel(9999) would be tunnel_is_alive(redssh.enums.TunnelType.dynamic,9999)

dynamic_tunnel(9999,’10.0.0.1’) would be tunnel_is_alive(redssh.enums.TunnelType.dynamic,9999,bind_addr=’10.0.0.1’)

Parameters
  • tunnel_type (redssh.enums.TunnelType) – The tunnel type to shutdown.

  • sport (str) – The bound port for local and dynamic tunnels or the local port on the remote side for remote tunnels.

  • rhost (str) – The remote host for local and remote tunnels.

  • rport (int) – The remote port for local and remote tunnels.

  • bind_addr (str) – The bind address used for local and dynamic tunnels.

Returns

bool, if bad tunnel type provided returns None