欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > python + ssh+ rich 升级和备份脚本

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

2025/11/17 10:21:59 来源:https://blog.csdn.net/weixin_45647912/article/details/142268559  浏览:    关键词:python + ssh+ rich 升级和备份脚本

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

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提供的脚本,修改后)

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 没以前好用了

在这里插入图片描述

功能齐全

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词