platypush.plugins.ssh

class platypush.plugins.ssh.SshPlugin(key_file: Optional[str] = None, passphrase: Optional[str] = None, **kwargs)[source]

SSh plugin.

Requires:

  • paramiko (pip install paramiko)
__init__(key_file: Optional[str] = None, passphrase: Optional[str] = None, **kwargs)[source]
Parameters:
  • key_file – Default key file (default: any “id_rsa”, “id_dsa”, “id_ecdsa”, or “id_ed25519” key discoverable in ~/.ssh/.
  • passphrase – Key file passphrase (default: None).
chdir(path: str, keep_alive: bool = False, **kwargs) → None[source]

Change directory to the specified path.

Parameters:
chmod(path: str, mode: int, keep_alive: bool = False, **kwargs) → None[source]

Change the access rights of a path.

Parameters:
  • path – Path to be modified.
  • mode – Access permissions (in octal mode).
  • keep_alive – Keep the connection active after running the command (default: False).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
chown(path: str, uid: int, gid: int, keep_alive: bool = False, **kwargs) → None[source]

Change the owner of a path.

Parameters:
  • path – Path to be modified.
  • uid – New user ID.
  • gid – New group ID.
  • keep_alive – Keep the connection active after running the command (default: False).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
connect(host: str, port: int = 22, user: Optional[str] = None, password: Optional[str] = None, key_file: Optional[str] = None, passphrase: Optional[str] = None, compress: bool = False, timeout: Optional[int] = None, auth_timeout: Optional[int] = None) → None[source]

Open an SSH connection.

Parameters:
  • host – Host name or IP. Can also be in the format [user]@<host>:[port].
  • port – Remote port (default: 22).
  • user – Username (default: None, same user name as the one running platypush).
  • password – Password (default: None).
  • key_file – Key file to use for authentication (default: None).
  • passphrase – Passphrase for the key file (default: None).
  • compress – Compress data on the connection (default: False).
  • timeout – Data transfer timeout in seconds (default: None).
  • auth_timeout – Authentication timeout in seconds (default: None).
disconnect(host: str, port: int = 22, user: Optional[str] = None) → None[source]

Close a connection to a host.

Parameters:
  • host – Host name or IP. Can also be in the format [user]@<host>:[port].
  • port – Remote port (default: 22).
  • user – Username (default: None, same user name as the one running platypush).
exec(cmd: str, keep_alive: bool = False, timeout: Optional[int] = None, stdin: Optional[str] = None, env: Optional[Dict[str, str]] = None, **kwargs) → platypush.message.response.Response[source]

Run a command on a host.

Parameters:
  • cmd – Command to run
  • keep_alive – Keep the connection active after running the command (default: False).
  • timeout – Communication timeout in seconds (default: None).
  • stdin – Optional string to pass on the stdin of the command.
  • env – Dictionary of environment variables to be used for the connection (default: None).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
Returns:

The output of the executed command.

get(remote_path: str, local_path: str, recursive: bool = False, keep_alive: bool = False, **kwargs) → None[source]

Download a file or folder from an SSH server.

Parameters:
  • remote_path – Remote path (file or directory).
  • local_path – Local path (file or directory).
  • recursive – Set to True if you want to recursively download folders (default: False).
  • keep_alive – Keep the connection active after running the command (default: False).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
getcwd(keep_alive: bool = False, **kwargs) → str[source]

Get the current working directory.

Parameters:
keygen(filename: str, type: str = 'rsa', bits: int = 4096, comment: Optional[str] = None, passphrase: Optional[str] = None) → platypush.message.response.ssh.SSHKeygenResponse[source]

Generate an SSH keypair.

Parameters:
  • filename – Output file name for the private key (the public key will be stored in <filename>.pub).
  • type – Encryption algorithm, either “rsa” or “dsa” (default: “rsa”).
  • bits – Key length in bits (default: 4096).
  • comment – Key comment (default: None).
  • passphrase – Key passphrase (default: None).
Returns:

platypush.message.response.ssh.SSHKeygenResponse.

ln(src: str, dest: str, keep_alive: bool = False, **kwargs) → None[source]

Create a symbolic link.

Parameters:
ls(path: str = '.', attrs: bool = False, keep_alive: bool = False, **kwargs) → Union[List[str], Dict[str, Any]][source]

Return the list of files in a path on a remote server.

Parameters:
  • path – Remote path (default: current directory).
  • keep_alive – Keep the connection active after running the command (default: False).
  • attrs – Set to True if you want to get the full information of each file (default: False).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
Returns:

A list of filenames if attrs=False, otherwise a dictionary filename -> {attributes if attrs=True.

mkdir(path: str, mode: int = 511, keep_alive: bool = False, **kwargs) → None[source]

Create a directory.

Parameters:
  • path – Path to be created.
  • mode – Access permissions (default: 0777).
  • keep_alive – Keep the connection active after running the command (default: False).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
mv(path: str, new_path: str, keep_alive: bool = False, **kwargs) → None[source]

Move/rename a file.

Parameters:
  • path – Remote path to move/rename.
  • new_path – Destination path.
  • keep_alive – Keep the connection active after running the command (default: False).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
put(remote_path: str, local_path: str, recursive: bool = False, keep_alive: bool = False, **kwargs) → None[source]

Upload a file or folder to an SSH server.

Parameters:
  • remote_path – Remote path (file or directory).
  • local_path – Local path (file or directory).
  • recursive – Set to True if you want to recursively upload folders (default: False).
  • keep_alive – Keep the connection active after running the command (default: False).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
rm(path: str, keep_alive: bool = False, **kwargs) → None[source]

Remove a file from the server.

Parameters:
rmdir(path: str, keep_alive: bool = False, **kwargs) → None[source]

Remove a directory.

Parameters:
start_forward_tunnel(local_port: int, remote_host: str, remote_port: int, bind_addr: Optional[str] = '', **kwargs)[source]

Start an SSH forward tunnel, tunnelling <local_port> to <remote_host>:<remote_port>.

Parameters:
  • local_port – Local port.
  • remote_host – Remote host.
  • remote_port – Remote port.
  • bind_addr – If set, the local_port will be bound to this address/subnet (default: ‘’, or 0.0.0.0: any).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
start_reverse_tunnel(server_port: int, remote_host: str, remote_port: int, bind_addr: Optional[str] = '', **kwargs)[source]

Start an SSH reversed tunnel. <server_port> on the SSH server is forwarded across an SSH session back to the local machine, and out to a <remote_host>:<remote_port> reachable from this network.

Parameters:
  • server_port – Server port.
  • remote_host – Remote host.
  • remote_port – Remote port.
  • bind_addr – If set, the server_port will be bound to this address/subnet (default: ‘’, or 0.0.0.0: any).
  • kwargs – Arguments for platypush.plugins.ssh.SshPlugin.connect().
stop_forward_tunnel(local_port: int, remote_host: str, remote_port: int)[source]

Stop an active SSH forward tunnel.

Parameters:
  • local_port – Local port.
  • remote_host – Remote host.
  • remote_port – Remote port.
stop_reverse_tunnel(server_port: int, remote_host: str, remote_port: int)[source]

Stop an active SSH reversed tunnel.

Parameters:
  • server_port – Server port.
  • remote_host – Remote host.
  • remote_port – Remote port.