服务器之间传递数据脚本

2024-04-26 06:12

本文主要是介绍服务器之间传递数据脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

  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.chinasem.cn/article/936837

相关文章

Linux服务器Java启动脚本

Linux服务器Java启动脚本 1、初版2、优化版本3、常用脚本仓库 本文章介绍了如何在Linux服务器上执行Java并启动jar包, 通常我们会使用nohup直接启动,但是还是需要手动停止然后再次启动, 那如何更优雅的在服务器上启动jar包呢,让我们一起探讨一下吧。 1、初版 第一个版本是常用的做法,直接使用nohup后台启动jar包, 并将日志输出到当前文件夹n

centos6一键安装vsftpd脚本

centos6一键安装vsftpd脚本 手动安装vsftpd参考教程:Centos下安装Vsftpd的图文教程 vsftpd脚本功能: 1.安装 (命令执行:sh xxx.sh)2.添加ftp用户 (命令执行:sh xxx.sh add)3.卸载vsftpd (命令执行:sh xxx.sh uninstall) 测试环境:centos6 x64 centos6 x86(测试centos7以

Android逆向(反调,脱壳,过ssl证书脚本)

文章目录 总结 基础Android基础工具 定位关键代码页面activity定位数据包参数定位堆栈追踪 编写反调脱壳好用的脚本过ssl证书校验抓包反调的脚本打印堆栈bilibili反调的脚本 总结 暑假做了两个月的Android逆向,记录一下自己学到的东西。对于app渗透有了一些思路。 这两个月主要做的是代码分析,对于分析完后的持久化等没有学习。主要是如何反编译源码,如何找到

Lua 脚本在 Redis 中执行时的原子性以及与redis的事务的区别

在 Redis 中,Lua 脚本具有原子性是因为 Redis 保证在执行脚本时,脚本中的所有操作都会被当作一个不可分割的整体。具体来说,Redis 使用单线程的执行模型来处理命令,因此当 Lua 脚本在 Redis 中执行时,不会有其他命令打断脚本的执行过程。脚本中的所有操作都将连续执行,直到脚本执行完成后,Redis 才会继续处理其他客户端的请求。 Lua 脚本在 Redis 中原子性的原因

CentOs7上Mysql快速迁移脚本

因公司业务需要,对原来在/usr/local/mysql/data目录下的数据迁移到/data/local/mysql/mysqlData。 原因是系统盘太小,只有20G,几下就快满了。 参考过几篇文章,基于大神们的思路,我封装成了.sh脚本。 步骤如下: 1) 先修改好/etc/my.cnf,        ##[mysqld]       ##datadir=/data/loc

PHP抓取网站图片脚本

方法一: <?phpheader("Content-type:image/jpeg"); class download_image{function read_url($str) { $file=fopen($str,"r");$result = ''; while(!feof($file)) { $result.=fgets($file,9999); } fclose($file); re

Python脚本:对文件进行批量重命名

字符替换:批量对文件名中指定字符进行替换添加前缀:批量向原文件名添加前缀添加后缀:批量向原文件名添加后缀 import osdef Rename_CharReplace():#对文件名中某字符进行替换(已完结)re_dir = os.getcwd()re_list = os.listdir(re_dir)original_char = input('请输入你要替换的字符:')replace_ch

Python脚本:TXT文档行数统计

count = 0 #计数变量file_dirs = input('请输入您要统计的文件根路径:')filename = open(file_dirs,'r') #以只读方式打开文件file_contents = filename.read() #读取文档内容到file_contentsfor file_content in file_contents:

Python脚本:批量解压RAR文件

所需模块: os.getcwd() #获取脚本文件路径os.system() #执行系统命令 import os#source_dir = input("Please input in source_dir:")#unzip_dir = input("Please input in unzip_dir:") source_dir = os.

站长常用Shell脚本整理分享(全)

站长常用Shell脚本整理分享 站长常用Shell脚本整理分享1-10 站长常用Shell脚本整理分享11-20 站长常用Shell脚本整理分享21-30 站长常用Shell脚本整理分享31-40 站长常用Shell脚本整理分享41-50 站长常用Shell脚本整理分享51-59 长期更新