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

相关文章

Linux如何查看文件权限的命令

《Linux如何查看文件权限的命令》Linux中使用ls-R命令递归查看指定目录及子目录下所有文件和文件夹的权限信息,以列表形式展示权限位、所有者、组等详细内容... 目录linux China编程查看文件权限命令输出结果示例这里是查看tomcat文件夹总结Linux 查看文件权限命令ls -l 文件或文件夹

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

Spring Bean初始化及@PostConstruc执行顺序示例详解

《SpringBean初始化及@PostConstruc执行顺序示例详解》本文给大家介绍SpringBean初始化及@PostConstruc执行顺序,本文通过实例代码给大家介绍的非常详细,对大家的... 目录1. Bean初始化执行顺序2. 成员变量初始化顺序2.1 普通Java类(非Spring环境)(

Spring Boot 中的默认异常处理机制及执行流程

《SpringBoot中的默认异常处理机制及执行流程》SpringBoot内置BasicErrorController,自动处理异常并生成HTML/JSON响应,支持自定义错误路径、配置及扩展,如... 目录Spring Boot 异常处理机制详解默认错误页面功能自动异常转换机制错误属性配置选项默认错误处理

如何在Java Spring实现异步执行(详细篇)

《如何在JavaSpring实现异步执行(详细篇)》Spring框架通过@Async、Executor等实现异步执行,提升系统性能与响应速度,支持自定义线程池管理并发,本文给大家介绍如何在Sprin... 目录前言1. 使用 @Async 实现异步执行1.1 启用异步执行支持1.2 创建异步方法1.3 调用

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建