服务器之间传递数据脚本

服务器之间的数据复制传递

  1. 准备 Python 环境: 确保你的计算机上安装了 Python,并安装了 Paramiko 库。你可以使用 pip 命令来安装 Paramiko,如下所示:

    pip install paramiko

  2. 修改脚本: 将脚本中的以下变量替换为你的实际服务器信息和凭据:

    • source_server_ip: 源服务器的 IP 地址或主机名。
    • source_file: 要复制的源文件的路径。
    • source_dir: 要复制的源文件夹的路径。
    • destination_server_ip: 目标服务器的 IP 地址或主机名。
    • dest_path: 目标服务器上的目标路径。
    • username: 登录服务器的用户名。
    • password: 登录服务器的密码。
  3. 运行脚本: 在命令行中运行脚本文件,如下所示:

    python your_script.py

    其中 your_script.py 是保存脚本代码的文件名。

  4. 检查结果: 执行完脚本后,它将输出“File copied successfully!”和“Directory copied successfully!”,表示文件和文件夹已成功从源服务器复制到目标服务器。

import paramiko
import os

def copy_file(source_host, source_file, dest_host, dest_path, username, password):
    # SSH 连接源服务器
    source_ssh = paramiko.SSHClient()
    source_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    source_ssh.connect(source_host, username=username, password=password)

    # SCP 文件从源服务器复制到目标服务器
    scp = paramiko.SFTPClient.from_transport(source_ssh.get_transport())
    scp.get(source_file, os.path.join(dest_path, os.path.basename(source_file)))

    # 关闭 SSH 连接
    source_ssh.close()

def copy_directory(source_host, source_dir, dest_host, dest_path, username, password):
    # SSH 连接源服务器
    source_ssh = paramiko.SSHClient()
    source_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    source_ssh.connect(source_host, username=username, password=password)

    # 获取目录下所有文件和子目录
    with source_ssh.open_sftp() as sftp:
        for item in sftp.listdir(source_dir):
            item_path = os.path.join(source_dir, item)
            dest_item_path = os.path.join(dest_path, item)
            if sftp.isfile(item_path):  # 复制文件
                sftp.get(item_path, dest_item_path)
            elif sftp.isdir(item_path):  # 递归复制子目录
                os.makedirs(dest_item_path, exist_ok=True)
                copy_directory(source_host, item_path, dest_host, dest_item_path, username, password)

    # 关闭 SSH 连接
    source_ssh.close()

def main():
    # 源服务器和目标服务器的信息
    source_host = 'source_server_ip'
    source_file = '/path/to/source/file.txt'
    source_dir = '/path/to/source/directory'
    dest_host = 'destination_server_ip'
    dest_path = '/path/to/destination/directory'
    username = 'your_username'
    password = 'your_password'

    # 复制文件
    copy_file(source_host, source_file, dest_host, dest_path, username, password)
    print("File copied successfully!")

    # 复制文件夹
    copy_directory(source_host, source_dir, dest_host, dest_path, username, password)
    print("Directory copied successfully!")

if __name__ == "__main__":
    main()

相关推荐

  1. 服务器之间传递数据脚本

    2024-04-26 06:54:06       17 阅读
  2. Vue3如何实现组件之间数据传递

    2024-04-26 06:54:06       38 阅读
  3. 前端网页之间传递参数

    2024-04-26 06:54:06       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-26 06:54:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-26 06:54:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-26 06:54:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-26 06:54:06       20 阅读

热门阅读

  1. TensorFlow 的基本概念和使用场景

    2024-04-26 06:54:06       15 阅读
  2. Oracle expdp/impdp 及 exp/imp 命令详解

    2024-04-26 06:54:06       13 阅读
  3. Debian常用命令

    2024-04-26 06:54:06       10 阅读
  4. nodejs连接oracle批量更新数据测试

    2024-04-26 06:54:06       12 阅读
  5. Debian常用命令

    2024-04-26 06:54:06       16 阅读
  6. 一个网络空间安全的小游戏

    2024-04-26 06:54:06       12 阅读
  7. 蛋白质致病突变的计算方法(六)

    2024-04-26 06:54:06       15 阅读
  8. Recat学习

    2024-04-26 06:54:06       10 阅读