python3 paramiko 基于RSA私钥远程执行ssh、上传、下载文件

2024-08-21 04:08

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

介绍

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

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

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

使用pip3安装

pip3 install paramiko

上一篇章已经讲诉了使用密码访问的方式 python3 paramiko 远程执行 ssh 命令、上传文件、下载文件 , 下面来看看封装使用RSA公钥访问的方式。

文件结构

[root@centos7 test_log]# tree paramiko-example/
paramiko-example/
├── file3.txt
├── file4.txt
├── paramiko_test.py
└── performance_rsa_40960 directories, 4 files
[root@centos7 test_log]# 

封装示例代码 paramiko_test.py

import paramiko
import osclass ParamikoHelper():def __init__(self,remote_ip, remote_ssh_port, private_key_file, ssh_username ):self.remote_ip = remote_ipself.remote_ssh_port = remote_ssh_portself.ssh_username = ssh_usernameself.private_key = paramiko.RSAKey.from_private_key_file(private_key_file) # 实例化一个私钥对象def 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,pkey=self.private_key)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, pkey=self.private_key)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, pkey=self.private_key)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.134'remote_ssh_port = 22ssh_username = 'root'private_key_file = 'performance_rsa_4096' # RSA私钥ph = ParamikoHelper(remote_ip=remote_ip,remote_ssh_port=remote_ssh_port,private_key_file=private_key_file,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()# 上传文件file4.txt到远程服务器上file = 'file4.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 基于RSA私钥远程执行ssh、上传、下载文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

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

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

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

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

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

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

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

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

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

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

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

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

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