ssh_paramiko¶
ssh_paramiko is a wrapper utility around ssh, using paramiko, to execute commands and exchange files remotelly
Usage¶
Executing a simple remote command¶
>>> from ssh_paramiko import RemoteServer
>>> ssh = RemoteServer('/tmp/sshkey',logFolder='.')
>>> ssh.set_log_level('DEBUG')
Log: Changing log level to DEBUG | Log level:DEBUG | Date:01/11/2016 12:04:40
>>> ssh.set_log_rotate_handler(True) # set bzipped log files (ssh_paramiko.debug.log.bz2
# and ssh_paramiko.error.log.bz2) to be rotated
>>> ssh.connect_server('myServer')
Log: Connecting to server myServer | Log level:DEBUG | Date:01/11/2016 12:04:41
Log: Initiating connection with server myServer... | Log level:DEBUG | Date:01/11/2016 12:04:41
Log: Instantiating transport object for sftp... | Log level:DEBUG | Date:01/11/2016 12:04:41
(True, '')
>>> ssh.execute_cmd('whoami')
(True, 'root\n', '')
>>> ssh.close_connection()
Log: Connection with server myServer ended. | Log level:INFO | Date:01/11/2016 12:04:45
True
Transfering a remote file to a local file¶
>>> from ssh_paramiko import RemoteServer
>>> ssh = RemoteServer('/tmp/sshkey',logFolder='.')
>>> ssh.set_log_level('DEBUG')
Log: Changing log level to DEBUG | Log level:DEBUG | Date:01/11/2016 12:04:40
>>> ssh.set_log_rotate_handler(True) # set bzipped log files (ssh_paramiko.debug.log.bz2 and
# ssh_paramiko.error.log.bz2) to be rotated
>>> ssh.connect_server('myServer')
Log: Connecting to server myServer | Log level:DEBUG | Date:01/11/2016 12:04:41
Log: Initiating connection with server myServer... | Log level:DEBUG | Date:01/11/2016 12:04:41
Log: Instantiating transport object for sftp... | Log level:DEBUG | Date:01/11/2016 12:04:41
(True, '')
>>> ssh.get_file('local_file.py', '/root/remote_file.py', callBack=ssh.transferProgressBar)
Log: Transfering remote file /root/remote_file.py from server myServer to local file local_file.py
| Log level:DEBUG | Date:01/11/2016 12:08:15
TrueSize: 542 bytes(0.0 MB) || File transfered. [###################################] 100.0%
>>> ssh.close_connection()
Log: Connection with server myServer ended. | Log level:INFO | Date:01/11/2016 12:04:45
True
Transfering a local file to a remote file¶
>>> from ssh_paramiko import RemoteServer
>>> ssh = RemoteServer('/tmp/sshkey',logFolder='.')
>>> ssh.set_log_level('DEBUG')
Log: Changing log level to DEBUG | Log level:DEBUG | Date:01/11/2016 12:07:40
>>> ssh.set_log_rotate_handler(True) # set bzipped log files (ssh_paramiko.debug.log.bz2 and
# ssh_paramiko.error.log.bz2) to be rotated
>>> ssh.connect_server('myServer')
Log: Connecting to server myServer | Log level:DEBUG | Date:01/11/2016 12:07:41
Log: Initiating connection with server myServer... | Log level:DEBUG | Date:01/11/2016 12:07:41
Log: Instantiating transport object for sftp... | Log level:DEBUG | Date:01/11/2016 12:07:41
(True, '')
>>> ssh.put_file('local_file.py', '/root/remote_file.py', callBack=ssh.transferProgressBar)
Log: Transfering local file local_file.py to remote file /root/remote_file.py in server myServer |
Log level:DEBUG | Date:01/11/2016 12:07:44
TrueSize: 542 bytes(0.0 MB) || File transfered. [###################################] 100.0%
>>> ssh.close_connection()
Log: Connection with server myServer ended. | Log level:INFO | Date:01/11/2016 12:07:44
True
Installation¶
To install ssh_paramiko, simply run:
$ pip install ssh_paramiko
ssh_paramiko is compatible with Python 2.6+
Documentation¶
Source Code¶
Feel free to fork, evaluate and contribute to this project.
License¶
GPLv3 licensed.
ssh_paramiko package contents:¶
ssh_paramiko package¶
Submodules¶
ssh_paramiko.ssh_paramiko module¶
-
class
ssh_paramiko.ssh_paramiko.
RemoteServer
(key_ssh, **kwargs)¶ Bases:
loggers.loggers.Loggers
Access remote server
Provide a layer of abstraction for accessing, executing commands and transfering files between a host and a server.
Parameters: - key_ssh (
str
) – path of the ssh private key to connect (must be None if using user and pasword to connect) - log_folder (
str
, optional , default =None) – folder where the log files of this class will be generated - username (
str
, optional , default =root) – username using the connection - password (
str
,optional, default =None) – password for connection if using user and password instead of key - ssh_port (
str
, optional, default =22) – ssh tcp port - server_has_dns (
bool
, optional, default =True) – if the server is not registered in a _d_n_s domain and/or has not its _d_n_s name equals to its hostname, this flag must be set to False, otherwise this condition will be checked to certify we are trully connected to the right server.
-
close_connection
()¶ Closes remote server connection
Returns: True if successfully disconnected, False otherwise Return type: bool
-
connect_server
(server, ping=True)¶ Connects a host and a server via ssh
Parameters: - server (
str
) – remote server - ping (
bool
, default = True) – if False, ignores if the remote server does not ping back
Returns: True if successfully connected, False otherwise
Return type: ret (
bool
)Returns: Message of error if cannot connect, empty string otherwise
Return type: msg (
str
)- server (
-
execute_cmd
(cmd, timeout=20)¶ _executes a command in a remote server shell
Parameters: - cmd (
str
) – command - timeout (
str
) – timeout to the command execution (default: 20)
Returns: True if command successfully executed, False otherwise
Return type: ret (
bool
)Returns: command standard output
Return type: output (
str
)Returns: command standard error
Return type: error (
str
)- cmd (
-
get_file
(local_file_path, remote_file_path, callback=None)¶ Transfers a remote file to a local file
Parameters: - local_file_path (
str
) – path of the local file - remote_file_path (
str
) – path of the remote file - callback (
callback
) – callback that reports file transfer status (bytes transfered and total bytes) _default: None
Returns: True if successfully transfered, False otherwise
Return type: bool
- local_file_path (
-
static
ping_server
(server, tries=4)¶ Connects a host and a server via ssh
Parameters: - server (
str
) – remote server - ping (
bool
, default = True) – if False, ignores if the remote server does not ping back
Returns: True if successfully connected, False otherwise
Return type: ret (
bool
)Returns: Message of error if cannot connect, empty string otherwise
Return type: msg (
str
)- server (
-
put_file
(local_file_path, remote_file_path, callback=None)¶ Transfers a local file to a remote file
Parameters: - local_file_path (
str
) – path of the local file - remote_file_path (
str
) – path of the remote file - callback (
callback
) – callback that reports file transfer status (bytes transfered and total bytes) _default: None
Returns: True if successfully transfered, False otherwise
Return type: bool
- local_file_path (
-
static
transfer_progress_bar
(transfered_bytes, total_bytes)¶ Provides a transfer progress bar
Given a file to be transfered, print a progress bar according to the bytes transfered yet and the file size to be used as a callback for the methods put_file and get_file.
Parameters: - transfered_bytes (
str
,int
orfloat
) – bytes transfered - total_bytes (
int
orfloat
) – size of file in bytes
- transfered_bytes (
-
validate_files
(local_file_path, remote_file_path)¶ _checks if a remote and local files has the same sha1sum
Parameters: - local_file_path (
str
) – path of the local file to be validated - remote_file_path (
str
) – path of the remote file to be validated
Returns: True if files’ sha1sums are the same, False otherwise
Return type: bool
- local_file_path (
- key_ssh (