python + ssh+ rich 升级和备份脚本

news/2024/12/22 2:21:11/

升级版本 (根据AI提供的脚本,修改后)

python">import os
import paramiko
from scp import SCPClient
from rich.progress import (BarColumn,DownloadColumn,Progress,TaskID,TextColumn,TimeRemainingColumn,TransferSpeedColumn,
)def get_file_size(file_path):return os.path.getsize(file_path)def progress_callback(filename, size, sent, progress_instance, task_id):progress_instance.update(task_id, completed=sent)# 创建 SSH 客户端并连接到远程服务器
def create_ssh_client(host, port, username, password):client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())client.connect(host, port, username, password)return clientdef scp_transfer_with_progress(local_file, remote_path, host, port, username, password):os.chdir(local_file)ssh_client = create_ssh_client(host, port, username, password)with Progress(TextColumn("[bold blue] {task.fields[filename]}", justify="right"),BarColumn(),"[progress.percentage]{task.percentage:>3.1f}%","•",DownloadColumn(),"•",TransferSpeedColumn(),"•",TimeRemainingColumn(), ) as progress:task_id = progress.add_task(filename="upgrade ...", description="upgrade...")def userProgress(filename, size, sent):progress.update(task_id, total=size, advance=sent,filename="Upgrade: " + os.path.basename(filename).decode())with SCPClient(ssh_client.get_transport(), progress=userProgress, socket_timeout=15) as scp:scp.put("./", remote_path, recursive=True)ssh_client.close()import sys
import timeif __name__ == "__main__":host = sys.argv[1]port = 22username = sys.argv[2]password = sys.argv[3]local_file = "upgradeSoftware"remote_path = "/mnt/flash"scp_transfer_with_progress(local_file, remote_path, host, port, username, password)`在这里插入

备份版本 (根据AI提供的脚本,修改后)

python">import os
import paramiko
from scp import SCPClient
from rich.progress import Progress
from rich.progress import (BarColumn,DownloadColumn,Progress,TaskID,TextColumn,TimeRemainingColumn,TransferSpeedColumn,
)```python
def get_file_size(file_path):return os.path.getsize(file_path)def progress_callback(filename, size, sent, progress_instance, task_id):progress_instance.update(task_id, completed=sent)# 创建 SSH 客户端并连接到远程服务器
def create_ssh_client(host, port, username, password):client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())client.connect(host, port, username, password)return clientdef scp_transfer_with_progress(local_file, remote_path, host, port, username, password):ssh_client = create_ssh_client(host, port, username, password)ssh_client.exec_command("rm  -rf /mnt/flash/log/message.*txt")with Progress(TextColumn("[bold blue] {task.fields[filename]}", justify="right"),BarColumn(),"[progress.percentage]{task.percentage:>3.1f}%","•",DownloadColumn(),"•",TransferSpeedColumn(),"•",TimeRemainingColumn(), ) as progress:task_id = progress.add_task(filename="Downloading...", description="Downloading...")def userProgress(filename, size, sent):progress.update(task_id, total=size, advance=sent, filename="Current Download:" + os.path.basename(filename))with SCPClient(ssh_client.get_transport(), progress=userProgress, socket_timeout=15) as scp:scp.get(remote_path, local_file, recursive=True)ssh_client.close()import sys
import timeif __name__ == "__main__":host = sys.argv[1]port = 22username = sys.argv[2]password = sys.argv[3]local_file = time.strftime("%Y%m%dT%H%M%S", time.localtime())os.mkdir(local_file)remote_path = sys.argv[4]scp_transfer_with_progress(local_file, remote_path, host, port, username, password)

可以提示很多的中间信息

感觉AI 没以前好用了

在这里插入图片描述

功能齐全

在这里插入图片描述


http://www.ppmy.cn/news/1528113.html

相关文章

实战案例(5)防火墙通过跨三层MAC识别功能控制三层核心下面的终端

如果网关是在核心设备上面,还能用MAC地址进行控制吗? 办公区域的网段都在三层上面,防火墙还能基于MAC来控制吗? 采用正常配置模式的步骤与思路 (1)配置思路与上面一样 (2)与上面区…

SpringBoot+Vue+MySQL驾校预约管理系统

目录 前言 功能设计 系统实现 获取源码 博主主页:百成Java 往期系列:Spring Boot、SSM、JavaWeb、python、小程序 前言 随着社会的进步,各行各业都在充分利用信息化时代的优势。由于计算机技术的广泛应用和普及,各种信息系统…

【mysql】逻辑运算符

逻辑运算符 逻辑运算符主要是为了判断表达式的真假,返回结果也是1,0,null OR 这里面或就是两个条件或的关系,比如我要department_id等于10和等于20的情况就可以使用或. SELECT last_name,salary,department_id FROM employees WHERE department_id10 OR department_id20 …

C++速通LeetCode简单第18题-杨辉三角(全网唯一递归法)

全网唯一递归法&#xff1a; vector<vector<int>> generate(int numRows) {vector<int> v;vector<vector<int>>vn;if (numRows 1){v.push_back(1);vn.push_back(v);v.clear();return vn;//递归记得return}if (numRows 2){v.push_back(1);vn.p…

使用BGP及静态路由方式实现链路冗余和ByPass

总体目标 场景:交换机A同时连接交换机B和C,数据优先通过A-B链路,若A-B链路出现故障,则自动切换到A-C链路。实现:使用BGP来控制路由优先级,并通过BFD来快速检测链路故障。//实际应用中B可以是一个安全检测设备,比如IPS、WAF之类,也可以是一个VPN设备。 这样正常情况下流…

裸土覆盖检测算法、裸土识别检测算法、裸土检测算法

裸土检测算法是一种基于计算机视觉和图像处理技术&#xff0c;用于自动识别和评估裸露土壤区域的智能工具。以下是对裸土检测算法的详细介绍&#xff1a; 1. 技术原理 - 数据采集与预处理&#xff1a;利用卫星、无人机或传感器收集地面图像数据&#xff0c;并进行必要的预处理…

物联网设备为何成为黑客攻击的目标

随着物联网&#xff08;Internet of Things, IoT&#xff09;技术的发展&#xff0c;越来越多的智能设备被引入日常生活和工业生产中。然而&#xff0c;由于设计和安全措施的不足&#xff0c;物联网设备常常成为黑客攻击的目标。本文将探讨物联网设备易受攻击的原因及其背后的安…

VMware ESXi 8.0U3b macOS Unlocker OEM BIOS 2.7 集成网卡驱动和 NVMe 驱动 (集成驱动版)

VMware ESXi 8.0U3b macOS Unlocker & OEM BIOS 2.7 集成网卡驱动和 NVMe 驱动 (集成驱动版) 发布 ESXi 8.0U3 集成驱动版&#xff0c;在个人电脑上运行企业级工作负载 请访问原文链接&#xff1a;https://sysin.org/blog/vmware-esxi-8-u3-sysin/&#xff0c;查看最新版…