Network engineers learn Python-34-SSH client and server library Paramiko

Paramiko is an SSH client and server library for Python that can implement the encryption and authentication functions of the SSHv2 protocol and supports the SFTP and SCP protocols. In this article, we will introduce how to use the Paramiko library to establish SSH connections, execute remote commands, transfer files, and more.

Install Paramiko

To use the Paramiko library, we need to install it first. It can be installed using the pip command:

pip install paramiko

establish ssh connection

Establishing an SSH connection using the Paramiko library is very simple, just specify the hostname, username and password. Here's a simple example:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username='username', password='password')

stdin, stdout, stderr = ssh.exec_command('ls')
print(stdout.read().decode())

ssh.close()

In the above example, we first create an SSHClient object and pass the hostname, username, and password as parameters to connect()the method. Then, use exec_command()the method to execute lsthe command and print the results to the console. Finally, we close the SSH connection.

execute remote command

Executing remote commands using the Paramiko library is also simple, just pass the command as a string to exec_command()the method. Here is an example:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username='username', password='password')

command = 'ls -l'
stdin, stdout, stderr = ssh.exec_command(command)

for line in stdout.readlines():
    print(line.strip())

ssh.close()

In the above example, we use exec_command()the method to execute ls -lthe command and readlines()the method to read the output and print it to the console. It should be noted that in some cases, it is necessary to use strip()methods to remove newlines in the output results.

transfer files

Transferring files using the Paramiko library is also simple, just use SFTPClientclasses. Here is an example:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username='username', password='password')

sftp = ssh.open_sftp()
sftp.put('local_file_path', 'remote_file_path')
sftp.get('remote_file_path', 'local_file_path')

sftp.close()
ssh.close()

In the above example, we first create an SFTPClientobject and use put()methods to transfer local files to the remote host. Then, use get()the method to transfer the remote file to the local host. Finally, we close SFTPClientthe SSH connection.

Use key authentication

In addition to authenticating using username and password, we can also authenticate using SSH keys. Here is an example:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username='username', pkey=key)

stdin, stdout, stderr = ssh.exec_command('ls')
print(stdout.read().decode())

ssh.close()

In the above example, we first from_private_key_file()load the SSH private key file using the method and pass the private key as a parameter to connect()the method. We then use exec_command()the method to execute lsthe command and print the results to the console. Finally, we close the SSH connection.

advanced options

The Paramiko library also provides many advanced options such as timeouts, port forwarding, key management, etc. Here is an example:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username='username', password='password', port=22, timeout=10)

# 开启端口转发
transport = ssh.get_transport()
local_port = 8080
remote_host = 'google.com'
remote_port = 80
transport.request_port_forward('', local_port, remote_host, remote_port)

# 密钥管理
key = paramiko.RSAKey.generate(2048)
private_key_string = key.export_key('PEM')
public_key_string = key.get_base64()

ssh.close()

In the above example, we first use connect()the method to connect to the remote host and specify the port number and timeout. Then, we use get_transport()the method to obtain the SSH transfer object and use request_port_forward()the method to enable port forwarding. Finally, we generate()generate the RSA key using the method and export the private and public keys using export_key()the and methods.get_base64()

in conclusion

In this article, we introduce how to use the Python Paramiko library to implement SSH client and server functions, including establishing SSH connections, executing remote commands, transferring files, etc. Additionally, we covered how to use SSH keys for authentication and discussed some advanced options. Using the Paramiko library, we can easily write Python scripts to manage remote servers and network devices.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132623390