python3 paramiko 远程执行 ssh 命令、上传文件、下载文件

2024-08-21 04:18

本文主要是介绍python3 paramiko 远程执行 ssh 命令、上传文件、下载文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在win10的系统下,本来想要python3直接调用ansible库进行远程执行的,但是很可惜,ansible是基于linux系统的ssh服务进行远程调用,不太兼容windows。
那么下面来使用paramiko库,直接手写一个ssh远程调用。

介绍

paramiko 遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接,可以实现远程文件的上传,下载或通过ssh远程执行命令。

项目地址:https://github.com/paramiko/paramiko

官方文档:http://docs.paramiko.org/

使用pip3安装

pip3 install paramiko

安装过程如下:

D:\pythonProject\locust_auto_test>pip3 install paramiko
Collecting paramikoUsing cached https://files.pythonhosted.org/packages/17/9f/7430d1ed509e195d5a5bb1a2bda6353a4aa64eb95491f198a17c44e2075c/paramiko-2.5.0-py2.py3-none-any.whl
Collecting bcrypt>=3.1.3 (from paramiko)Downloading https://files.pythonhosted.org/packages/09/91/1b9e566c9aafe40eb89b31a7d322c7c070a3249bd2f3e50c8828fe4418d7/bcrypt-3.1.6-cp37-cp37m-win_amd64.whl
Requirement already satisfied: cryptography>=2.5 in d:\python37\lib\site-packages (from paramiko) (2.7)
Collecting pynacl>=1.0.1 (from paramiko)Using cached https://files.pythonhosted.org/packages/fc/e7/179847c0dce637c59cea416c75b8de1ec1e862358c7369ad99c1fad00158/PyNaCl-1.3.0-cp37-cp37m-win_amd64.whl
Requirement already satisfied: cffi>=1.1 in d:\python37\lib\site-packages (from bcrypt>=3.1.3->paramiko) (1.12.1)
Requirement already satisfied: six>=1.4.1 in d:\python37\lib\site-packages (from bcrypt>=3.1.3->paramiko) (1.12.0)
Requirement already satisfied: asn1crypto>=0.21.0 in d:\python37\lib\site-packages (from cryptography>=2.5->paramiko) (0.24.0)
Requirement already satisfied: pycparser in d:\python37\lib\site-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko) (2.19)
Installing collected packages: bcrypt, pynacl, paramiko
Successfully installed bcrypt-3.1.6 paramiko-2.5.0 pynacl-1.3.0D:\pythonProject\locust_auto_test>

测试是否安装成功,如下:

D:\pythonProject\locust_auto_test>ipython3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.In [1]: import paramikoIn [2]: 

可以看到导入并没有出错,所以下面可以正常使用这个库了。

在本次实验中,最核心的功能就是远程执行ssh命令,所以首先来实验一下这个功能。

使用ipython3远程执行ssh命令

D:\pythonProject\locust_auto_test>ipython3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.In [1]: import paramiko# 设置ssh访问信息
In [2]: remote_ip = '192.168.196.129'In [3]: remote_ssh_port = 22In [5]: ssh_password = '********'In [6]: ssh_username = 'root'In [7]: ssh = paramiko.SSHClient()# 设置连接策略
In [8]: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# ssh连接服务器
In [9]: ssh.connect( hostname = remote_ip, port = remote_ssh_port, username = ssh_username, password = ssh_password )# 远程ssh执行shell命令
In [10]: stdin, stdout, stderr = ssh.exec_command("df -h | grep dev")# 打印查看磁盘信息的结果
In [11]: print(stdout.readlines())
['/dev/mapper/centos-root   17G  9.7G  7.3G  58% /\n', 'devtmpfs                 899M     0  899M   0% /dev\n', 'tmpfs                    911M     0  911M   0% /dev/shm\n',
'/dev/sda1               1014M  142M  873M  14% /boot\n']In [12]: # 执行完毕之后,结果只会打印一次
In [14]: stdin, stdout, stderr = ssh.exec_command("df -h | grep dev")In [15]: for line in stdout.readlines():...:     print(line)...: 
/dev/mapper/centos-root   17G  9.7G  7.3G  58% /devtmpfs                 899M     0  899M   0% /devtmpfs                    911M     0  911M   0% /dev/shm/dev/sda1               1014M  142M  873M  14% /boot## 执行一个存在等待时长的shell命令
In [24]: stdin, stdout, stderr = ssh.exec_command("df -h | grep dev && echo '123' && sleep 10 && echo 'sleep complete'")## 发现应该是在执行打印的时候,才是真正执行shell命令。
In [25]: for line in stdout.readlines():...:     print(line)...: 
/dev/mapper/centos-root   17G  9.7G  7.3G  58% /devtmpfs                 899M     0  899M   0% /devtmpfs                    911M     0  911M   0% /dev/shm/dev/sda1               1014M  142M  873M  14% /boot123sleep completeIn [26]: # 执行一个查看日志的远程ssh命令
In [11]: stdin, stdout, stderr = ssh.exec_command("tail -f /root/test_log/test.log")In [12]: for line in stdout.readlines():...:     print(line)...: # 发现就算写入新的信息进去,是不会持续打印出新的信息的。
# 也就是验证了这个ssh执行时一次性的执行结果。# 关闭ssh连接
In [16]: ssh.close()

上传文件功能

In [2]: import osIn [10]: import paramiko## 设置sftp连接信息
In [11]: remote_ip = '192.168.196.129'In [12]: remote_ssh_port = 22In [13]: ssh_password = '***********'In [14]: ssh_username = 'root'## 创建sftp连接
In [16]: t = paramiko.Transport((remote_ip, remote_ssh_port))In [17]: t.connect(username=ssh_username, password=ssh_password)In [18]: sftp = paramiko.SFTPClient.from_transport(t)## 执行上传sftp
In [26]: sftp.put('D:\\pythonProject\\locust_auto_test\\paramiko_test\\file1.txt', '/root/test_log/file1.txt')
Out[26]: <SFTPAttributes: [ size=18 uid=0 gid=0 mode=0o100644 atime=1560329096 mtime=1560329096 ]>In [27]: ## 关闭sftp连接
In [28]: t.close()

到远程服务器查看上传好的文件,如下:

[root@centos7 test_log]# ls
file1.txt
[root@centos7 test_log]# cat file1.txt 
测试上传文件[root@centos7 test_log]# 
[root@centos7 test_log]# 

执行下载文件

首先在远程Centos7将file1.txt文件拷贝一份为file2.txt,用于下载该文件。

[root@centos7 test_log]# cp file1.txt file2.txt 
[root@centos7 test_log]# 
[root@centos7 test_log]# ls
file1.txt  file2.txt
[root@centos7 test_log]# 

执行下载文件功能如下:

## 创建sftp连接
In [29]: t = paramiko.Transport((remote_ip, remote_ssh_port))In [30]: t.connect(username=ssh_username, password=ssh_password)In [31]: sftp = paramiko.SFTPClient.from_transport(t)## 通过sftp查看远程服务器该路径有什么文件
In [32]: sftp.listdir('/root/test_log')
Out[32]: ['file1.txt', 'file2.txt']## 设置本地路径
In [35]: local_dir = 'D:\\pythonProject\\locust_auto_test\\paramiko_test\\file2.txt'## 设置远程路径
In [36]: remote_dir = '/root/test_log/file2.txt'## 下载远程路径的文件到本地路径
In [37]: sftp.get(remote_dir,local_dir)## 查看本地路径是否已有file2.txt,可以看到已经成功下载下来了。
In [38]: os.listdir(os.getcwd())
Out[38]: ['file1.txt', 'file2.txt', 'test1.py']

上面我写windows下的路径都是直接写了个全路径,是为了方便理解,下面可以使用命令来设置这些路径。

In [41]: local_dir = os.path.join(os.getcwd(),'file2.txt')In [42]: sftp.get(remote_dir,local_dir)In [43]: os.listdir(os.getcwd())
Out[43]: ['file1.txt', 'file2.txt', 'test1.py']In [44]: 

当时由于windows与linux获取当前路径的拼接方式不同,所以linux路径我还是直接使用字符串写远程路径的方式。

上面基本上已经将功能都完成了,下一步就可以将这些方法都封装到一个工具类中。

封装工具类方法

import paramiko
import osclass ParamikoHelper():def __init__(self,remote_ip, remote_ssh_port, ssh_password, ssh_username ):self.remote_ip = remote_ipself.remote_ssh_port = remote_ssh_portself.ssh_password = ssh_passwordself.ssh_username = ssh_usernamedef connect_ssh(self):try:self.ssh = paramiko.SSHClient()self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())self.ssh.connect(hostname=self.remote_ip, port=self.remote_ssh_port, username=self.ssh_username,password=self.ssh_password)except Exception as e:print(e)return self.sshdef close_ssh(self):try:self.ssh.close()except Exception as e:print(e)def exec_shell(self, shell):ssh = self.connect_ssh()try:stdin, stdout, stderr = ssh.exec_command(shell)return stdin, stdout, stderrexcept Exception as e:print(e)def sftp_put_file(self, file, local_dir, remote_dir):try:t = paramiko.Transport((self.remote_ip, self.remote_ssh_port))t.connect(username=self.ssh_username, password=self.ssh_password)sftp = paramiko.SFTPClient.from_transport(t)sftp.put(os.path.join(local_dir, file), remote_dir)t.close()except Exception:print("connect error!")def sftp_get_file(self, file, local_dir, remote_dir):try:t = paramiko.Transport((self.remote_ip, self.remote_ssh_port))t.connect(username=self.ssh_username, password=self.ssh_password)sftp = paramiko.SFTPClient.from_transport(t)sftp.get(remote_dir, os.path.join(local_dir, file))t.close()except Exception:print("connect error!")def main():remote_ip = '192.168.196.129'remote_ssh_port = 22ssh_password = '**************'ssh_username = 'root'ph = ParamikoHelper(remote_ip=remote_ip,remote_ssh_port=remote_ssh_port,ssh_password=ssh_password,ssh_username=ssh_username)# 远程执行ssh命令shell = "df -h | grep dev"stdin, stdout, stderr = ph.exec_shell(shell)for line in stdout.readlines():print(line)ph.close_ssh()# 上传文件file2.txt到远程服务器上file = 'file2.txt'remote_dir = '/root/test_log/' + filelocal_dir = os.getcwd()ph.sftp_put_file(file=file, local_dir=local_dir, remote_dir=remote_dir)# 下载文件file3.txtfile = 'file3.txt'remote_dir = '/root/test_log/' + filelocal_dir = os.getcwd()ph.sftp_get_file(file=file, local_dir=local_dir, remote_dir=remote_dir)if __name__ == '__main__':main()
13423234-0e3934319aa622f6.png

这篇关于python3 paramiko 远程执行 ssh 命令、上传文件、下载文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092092

相关文章

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

postgresql数据库基本操作及命令详解

《postgresql数据库基本操作及命令详解》本文介绍了PostgreSQL数据库的基础操作,包括连接、创建、查看数据库,表的增删改查、索引管理、备份恢复及退出命令,适用于数据库管理和开发实践,感兴... 目录1. 连接 PostgreSQL 数据库2. 创建数据库3. 查看当前数据库4. 查看所有数据库

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx