详解Python远程控制模块:Paramiko概念、方法及七大案例(Python远程控制利器:Paramiko模块详解与七大实战案例)
原创
一、Paramiko简介
Paramiko 是一个 Python 实现的 SSHv2 协议的库,它提供了客户端和服务端的功能。使用 Paramiko,可以让我们通过 Python 代码实现 SSH 连接,执行远程命令,传输文件等操作。Paramiko 是一个非常强势的模块,广泛应用于自动化运维、远程控制等领域。
二、Paramiko 安装与导入
在起始使用 Paramiko 之前,首先需要安装它。可以使用 pip 命令进行安装:
pip install paramiko
安装完成后,我们可以通过以下代码导入 Paramiko 模块:
import paramiko
三、Paramiko 基本概念与方法
下面介绍一些 Paramiko 的基本概念和方法。
1. SSHClient 类
SSHClient 类是 Paramiko 中用于创建和管理 SSH 连接的核心类。以下是 SSHClient 类的一些常用方法:
- connect(host, port, username, password):连接到指定的 SSH 服务器。
- exec_command(command):执行远程命令。
- get_transport():获取当前连接的传输对象。
- close():关闭连接。
2. Transport 类
Transport 类是 SSHClient 类内部使用的传输对象,用于处理 SSH 连接的底层细节。以下是 Transport 类的一些常用方法:
- start_server():启动 SSH 服务器。
- accept():接受客户端连接。
- get_allowed_auths():获取允许的认证方法。
- auth_password(username, password):使用密码认证。
- open_channel(channel_type, chan_name, sendInitialPacket=True):打开一个通道。
四、Paramiko 实战案例
案例一:远程执行命令
以下是一个使用 Paramiko 执行远程命令的示例代码:
import paramiko
# 创建 SSH 对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect('192.168.1.100', 22, 'username', 'password')
# 执行命令
stdin, stdout, stderr = ssh.exec_command('ls -l')
print(stdout.read().decode())
# 关闭连接
ssh.close()
案例二:远程文件传输
以下是一个使用 Paramiko 进行远程文件传输的示例代码:
import paramiko
import os
# 创建 SSH 对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect('192.168.1.100', 22, 'username', 'password')
# 上传文件
sftp = ssh.open_sftp()
local_path = '/path/to/local/file'
remote_path = '/path/to/remote/file'
sftp.put(local_path, remote_path)
# 下载文件
sftp.get(remote_path, local_path)
# 关闭连接
sftp.close()
ssh.close()
案例三:远程文件操作
以下是一个使用 Paramiko 进行远程文件操作的示例代码:
import paramiko
# 创建 SSH 对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect('192.168.1.100', 22, 'username', 'password')
# 创建 SFTP 对象
sftp = ssh.open_sftp()
# 创建远程目录
remote_dir = '/path/to/remote/dir'
try:
sftp.mkdir(remote_dir)
except IOError:
print(f"Directory {remote_dir} already exists.")
# 删除远程文件
remote_file = '/path/to/remote/file'
try:
sftp.remove(remote_file)
except IOError:
print(f"File {remote_file} not found.")
# 关闭连接
sftp.close()
ssh.close()
案例四:远程文件权限修改
以下是一个使用 Paramiko 修改远程文件权限的示例代码:
import paramiko
# 创建 SSH 对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect('192.168.1.100', 22, 'username', 'password')
# 修改文件权限
remote_file = '/path/to/remote/file'
ssh.exec_command(f'chmod 755 {remote_file}')
# 关闭连接
ssh.close()
案例五:远程文件搜索
以下是一个使用 Paramiko 进行远程文件搜索的示例代码:
import paramiko
# 创建 SSH 对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect('192.168.1.100', 22, 'username', 'password')
# 搜索远程文件
remote_path = '/path/to/search/directory'
search_command = f'find {remote_path} -name "*.txt"'
stdin, stdout, stderr = ssh.exec_command(search_command)
files = stdout.read().decode().split(' ')
# 输出搜索导致
for file in files:
if file:
print(file)
# 关闭连接
ssh.close()
案例六:远程文件内容读取
以下是一个使用 Paramiko 读取远程文件内容的示例代码:
import paramiko
# 创建 SSH 对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect('192.168.1.100', 22, 'username', 'password')
# 读取远程文件内容
remote_file = '/path/to/remote/file'
stdin, stdout, stderr = ssh.exec_command(f'cat {remote_file}')
content = stdout.read().decode()
# 输出文件内容
print(content)
# 关闭连接
ssh.close()
案例七:远程文件内容写入
以下是一个使用 Paramiko 向远程文件写入内容的示例代码:
import paramiko
# 创建 SSH 对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect('192.168.1.100', 22, 'username', 'password')
# 向远程文件写入内容
remote_file = '/path/to/remote/file'
content = 'Hello, world!'
ssh.exec_command(f'echo {content} > {remote_file}')
# 关闭连接
ssh.close()
五、总结
Paramiko 是一个功能强势的 Python 远程控制模块,通过它可以实现 SSH 连接、执行远程命令、传输文件等操作。本文详细介绍了 Paramiko 的基本概念和方法,并通过七大实战案例展示了怎样使用 Paramiko 进行远程控制。掌握 Paramiko,可以大大节约运维高效能,实现自动化运维。