0
点赞
收藏
分享

微信扫一扫

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

数数扁桃 2024-09-21 阅读 21
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 client


def 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 time

if __name__ == "__main__":
    host = sys.argv[1]
    port = 22
    username = 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)`在这里插入
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 client


def 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 time

if __name__ == "__main__":
    host = sys.argv[1]
    port = 22
    username = 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)

在这里插入图片描述

在这里插入图片描述

举报

相关推荐

0 条评论