Paramiko is a Python library that provides a high-level interface for interacting with SSH. It is used for a variety of tasks, such as remote command execution, file transfer, and port forwarding. Paramiko is included in the standard Python distribution, but it is also available as an RPM package for RHEL.
Before installing Paramiko, you need to install the following yum installation packages:
sudo yum install python-devel libffi-devel openssl-devel
Once these packages are installed, you can install Paramiko using either the Python Package Index (PyPI) or the RHEL RPM repository.
Installing Paramiko from PyPI
To install Paramiko from PyPI, open a terminal and run the following command:
pip install paramiko
This will install the latest version of Paramiko to your Python environment.
Installing Paramiko from the RHEL RPM Repository
To install Paramiko from the RHEL RPM repository, open a terminal and run the following command:
sudo yum install python-paramiko
This will install the Paramiko RPM package to your system.
Verifying the Installation
To verify that Paramiko has been installed successfully, run the following command in a terminal:
python -m import paramiko
If this command runs without any errors, then Paramiko has been installed successfully.
Using Paramiko
Once Paramiko has been installed, you can use it to connect to remote SSH servers and execute commands, transfer files, and forward ports.
Examples
Here are some examples of how to use Paramiko to perform common tasks:
Example 1: Execute a command on a remote server
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect('remote_server_hostname', username='username', password='password')
stdin, stdout, stderr = ssh_client.exec_command('ls -la')
output = stdout.read().decode()
print(output)
Example 2: Transfer a file to a remote server
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect('remote_server_hostname', username='username', password='password')
sftp_client = ssh_client.open_sftp()
sftp_client.put('local_file_path', 'remote_file_path')
sftp_client.close()
Example 3: Forward a port on a remote server
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect('remote_server_hostname', username='username', password='password')
ssh_client.forward_local_port(local_port=1234, remote_host='localhost', remote_port=22)
print('Port 1234 has been forwarded to port 22 on the remote server.')
Conclusion
Paramiko is a powerful Python library that can be used to perform a variety of tasks related to SSH. It is easy to install and use, and it is available for both Linux and Windows.
0 Comments