服务器之间传递数据脚本

server/2024/10/18 18:17:16/

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

  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 osdef 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()


http://www.ppmy.cn/server/14445.html

相关文章

机动车销售发票识别,难在哪?

机动车销售发票作为购车交易中不可或缺的一环,其重要性不言而喻。然而,在实际操作中,许多消费者和商家都面临着机动车销售发票识别的难题。那么,这一难题究竟难在何处呢?本文将从多个方面进行深入分析。 一、发票种类繁…

第 394 场 LeetCode 周赛题解

A 统计特殊字母的数量 I 哈希&#xff1a;遍历然后枚举 class Solution {public:int numberOfSpecialChars(string word) {unordered_map<char, int> m;for (auto ch : word)m[ch] 1;int res 0;for (char ch a; ch < z; ch)if (m.count(ch) && m.count(A …

基于深度神经网络的图像识别技术研究

基于深度神经网络的图像识别技术是目前人工智能领域的研究热点之一&#xff0c;其强大的特征提取和模式识别能力使得图像识别任务取得了显著的进展。以下是对基于深度神经网络的图像识别技术的研究探讨。 首先&#xff0c;深度神经网络通过构建多层次的神经元连接&#xff0c;…

LabVIEW专栏八、类

该章目的是可以开发仪器类。 一、类的概述 一般来说类有三大特性&#xff0c;封装&#xff0c;继承和多态。 在实际项目中&#xff0c;最主要是继承和多态&#xff0c;要搞清楚这两者的概念和在LabVIEW中是怎样应用的。在LabVIEW中&#xff0c;面向对象编程用到的就是LabVIE…

【分治】Leetcode 颜色分类

题目讲解 75. 颜色分类 这道题的本质就是数组分三块 算法讲解 使用三个指针&#xff0c;i遍历数组&#xff0c;left标记0的最右侧&#xff0c;right标记2的最左侧 如果当前的nums[i] 0,我们就让nums[left] 和 nums[i]位置上的数字做交换&#xff0c;这里的i是可以向前移…

机械臂模型更换成自己的urdf模块

1.将urdf生成slx文件 smimport(rm_65_flange.urdf);%生成Simscape物理模型 2.更换joint部分&#xff08;对应与几个输入几个输出&#xff09;&#xff08;依次更换&#xff09; 3.更改关节部分&#xff08;依次更换&#xff09; 找到urdf文件夹下的meshes文件夹&#xff0c;看…

【Java】全套云HIS源码包含EMR、LIS(多医院、卫生机构使用)

云HIS系统简介 SaaS模式Java版云HIS系统源码&#xff0c;在公立二甲医院应用三年&#xff0c;经过多年持续优化和打磨&#xff0c;系统运行稳定、功能齐全&#xff0c;界面布局合理、操作简便。 1、融合B/S版电子病历系统&#xff0c;支持电子病历四级&#xff0c;HIS与电子病…

springboot遇到的错误

控制台异常显示: java.lang.NoClassDefFoundError: org/springframework/aot/AotDetector 原因, mybatis 版本与 springboot版本不匹配导致,调低一些mybatis版本即可。 &#xff08;本来3.0.2的版本是不冲突的&#xff0c;中途加了一些就冲突了...&#xff09; <dependen…