Please note that the project is in beta phase. Please report any issues you encounter or suggestions you have. We will do our best to address them quickly. Contributions are very welcome!
PySSHHelper is a Python library for simplifying SSH connections and interactions with remote servers.
You can install PySSHHelper using pip:
pip install PySSHHelper
If you would like the most up-to-date version, you can instead install directly from GitHub:
git clone <copied link from github>
cd PySSHHelper
pip install .
from PySSHHelper import SSHConnection
After importing the package, you can use SSHConnection in your code:
# Create an SSH connection object
ssh = SSHConnection(
host='your_host',
port=22,
username_ssh='your_ssh_username',
password_ssh='your_ssh_password',
username_sudo='your_sudo_username',
password_sudo='your_sudo_password',
verbose=True # Set verbose to True for debugging information
)
# Connect to the SSH server
ssh.connect()
# Execute a command
output = ssh.execute_command("ls -l")
print(output)
The output variable will contain all output in the console, including escape codes from the terminal, you can use the method to clear them:
print(ssh.cleanup_escape_codes(output))
If you want to execute commands in sudo context:
ssh.elevate_privileges()
print(ssh.execute_command("echo 123 && sleep 5 && echo 321", timeout=10))
print(ssh.execute_command("id"))
And after you can revoke privileges and then continue to execute in normal context
ssh.revoke_privileges()
print(ssh.execute_command("id"))
Also, you can do some SFTP operations
print(ssh.list_directory())
ssh.sftp_get('/remote/path/file.txt', 'local_file.txt')
ssh.sftp_put('local_file.txt', '/remote/path/file.txt')
Close the SSH connection
ssh.close()