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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

在MySQL执行UPDATE语句时遇到的错误1175的解决方案

《在MySQL执行UPDATE语句时遇到的错误1175的解决方案》MySQL安全更新模式(SafeUpdateMode)限制了UPDATE和DELETE操作,要求使用WHERE子句时必须基于主键或索引... mysql 中遇到的 Error Code: 1175 是由于启用了 安全更新模式(Safe Upd

Java下载文件中文文件名乱码的解决方案(文件名包含很多%)

《Java下载文件中文文件名乱码的解决方案(文件名包含很多%)》Java下载文件时,文件名中文乱码问题通常是由于编码不正确导致的,使用`URLEncoder.encode(filepath,UTF-8... 目录Java下载文件中文文件名乱码问题一般情况下,大家都是这样为了解决这个问题最终解决总结Java下

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

Python实现文件下载、Cookie以及重定向的方法代码

《Python实现文件下载、Cookie以及重定向的方法代码》本文主要介绍了如何使用Python的requests模块进行网络请求操作,涵盖了从文件下载、Cookie处理到重定向与历史请求等多个方面,... 目录前言一、下载网络文件(一)基本步骤(二)分段下载大文件(三)常见问题二、requests模块处理

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

Redis的Zset类型及相关命令详细讲解

《Redis的Zset类型及相关命令详细讲解》:本文主要介绍Redis的Zset类型及相关命令的相关资料,有序集合Zset是一种Redis数据结构,它类似于集合Set,但每个元素都有一个关联的分数... 目录Zset简介ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZ

详解如何在React中执行条件渲染

《详解如何在React中执行条件渲染》在现代Web开发中,React作为一种流行的JavaScript库,为开发者提供了一种高效构建用户界面的方式,条件渲染是React中的一个关键概念,本文将深入探讨... 目录引言什么是条件渲染?基础示例使用逻辑与运算符(&&)使用条件语句列表中的条件渲染总结引言在现代

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

关于Maven生命周期相关命令演示

《关于Maven生命周期相关命令演示》Maven的生命周期分为Clean、Default和Site三个主要阶段,每个阶段包含多个关键步骤,如清理、编译、测试、打包等,通过执行相应的Maven命令,可以... 目录1. Maven 生命周期概述1.1 Clean Lifecycle1.2 Default Li