当前位置: 首页 > news >正文

【跨服务器的数据自动化下载--安装公钥,免密下载】

跨服务器的数据自动化下载

    • 功能介绍:
    • 上代码:

发现好久没写csdn了,说多了都是泪~~
以后会更新一些自动化工作的脚本or 小tricks,欢迎交流。在这里插入图片描述分享一个最近在业务上写的较为实用的自动化脚本,可以批量从远端服务器下载指定数据到当前程序运行服务器,无需每次都输入密码,通过集成安装公钥的功能,实现免密下载。这个方式适合这种不能安装 sshpass又想全自动化的环境。

功能介绍:

第一次运行:会提示输入一次远端 服务器 的密码(安装公钥)。安装完成后立即用免密下载。
以后运行:直接免密下载,全自动。
后续所有下载脚本全自动化,不用再输入任何密码,也不依赖额外工具。
以从远端服务器(账户:abc)下载数据为例。

上代码:

#!/usr/bin/env python3
import sys
import os
import subprocess
import tempfile
import shutildef check_key_auth(remote_username, remote_host, private_key_path):"""检查是否已配置免密"""result = subprocess.run(['ssh', '-i', private_key_path, '-o', 'BatchMode=yes', '-o', 'StrictHostKeyChecking=no',f'{remote_username}@{remote_host}', 'echo ok'],stdout=subprocess.PIPE, stderr=subprocess.PIPE)return result.returncode == 0def install_pubkey(remote_username, remote_host, private_key_path):"""将本地公钥写入远端 authorized_keys(会要求输入密码一次)"""pubkey_path = private_key_path + '.pub'if not os.path.exists(pubkey_path):print(f"未找到公钥文件 {pubkey_path}")return Falsewith open(pubkey_path, 'r') as f:pubkey_content = f.read().strip()print(f"正在将公钥写入 {remote_host},需要输入 {remote_username} 的密码...")cmd = ['ssh', f'{remote_username}@{remote_host}',f'mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo "{pubkey_content}" >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys']return subprocess.run(cmd).returncode == 0def download_files_batch(remote_username, remote_host, private_key_path, files_to_download):"""批量下载实现:1. 在本地创建一个临时目录 tmpdir2. 用 single scp 命令把所有 remote:files 下载到 tmpdir(scp 支持多个 source + 单个 destination directory)3. 将 tmpdir 下的文件移动到各自的目标位置"""# 先保证目标本地目录存在(创建父目录)for _, local_path in files_to_download:os.makedirs(os.path.dirname(local_path), exist_ok=True)tmpdir = tempfile.mkdtemp(prefix='download_')try:# 构造 scp 命令:scp -i key user@host:/path/to/file1 user@host:/path/to/file2 ... <tmpdir>scp_cmd = ['scp', '-i', private_key_path, '-o', 'StrictHostKeyChecking=no']for remote_path, _ in files_to_download:scp_cmd.append(f'{remote_username}@{remote_host}:{remote_path}')scp_cmd.append(tmpdir)  # scp 要求最后是目标目录print("执行 scp,目标临时目录:", tmpdir)print("scp 命令:", ' '.join(scp_cmd))proc = subprocess.run(scp_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)if proc.returncode != 0:print("批量 scp 失败,错误信息:")print(proc.stderr.decode(errors='ignore'))return False# scp 成功,把文件从 tmpdir 移动到最终位置for remote_path, local_path in files_to_download:fname = os.path.basename(remote_path)src = os.path.join(tmpdir, fname)if not os.path.exists(src):print(f"警告:远端文件 {remote_path} 未被下载到临时目录({src} 不存在)")# 继续处理其它文件continueshutil.move(src, local_path)print(f"已移动:{src} -> {local_path}")print("全部文件处理完成。")return Truefinally:# 清理临时目录(如果里面还有剩余文件,会一并删除)try:shutil.rmtree(tmpdir)except Exception as e:print("清理临时目录时出错:", e)def main():if len(sys.argv) != 2:print("用法: python fully_auto_download.py <date>")sys.exit(1)date = sys.argv[1]remote_host = "" ##你需要获取数据的远端ipremote_username = "abc"private_key_path = '/.ssh/id_rsa'files_to_download = [(f"/data/{date[:4]}/{date}_000000_000000.csv",f"/save/{date}_000000_000000.csv"),## 此处省略其他需要下载的数据]# 检查免密if not check_key_auth(remote_username, remote_host, private_key_path):print("未检测到免密,开始安装公钥...")if not install_pubkey(remote_username, remote_host, private_key_path):print("公钥安装失败,请检查密码是否正确或远端权限设置。")sys.exit(1)if not check_key_auth(remote_username, remote_host, private_key_path):print("免密配置仍然失败,请手动检查。")sys.exit(1)print("免密配置成功!")# 一次性批量下载ok = download_files_batch(remote_username, remote_host, private_key_path, files_to_download)if not ok:print("批量下载出现问题,退回逐个下载尝试。")# 如果批量下载失败,可以回退到逐个 scp(免密已经配置好了,不会再要求密码)for remote_path, local_path in files_to_download:try:os.makedirs(os.path.dirname(local_path), exist_ok=True)scp_cmd = ['scp', '-i', private_key_path, '-o', 'StrictHostKeyChecking=no',f'{remote_username}@{remote_host}:{remote_path}', local_path]subprocess.run(scp_cmd, check=True)print(f"下载成功: {local_path}")except subprocess.CalledProcessError as e:print(f"下载失败: {local_path}, 错误: {e}")if __name__ == "__main__":main()
http://www.lryc.cn/news/618276.html

相关文章:

  • n8n、Workflow实战
  • 快速了解自然语言处理
  • QT多线程全面讲解
  • NTP常见日志分析
  • MySQL User表入门教程
  • Mysql GROUP_CONCAT函数数据超长导致截取问题记录
  • 测试自动化框架全解读:为什么、类型、优势与最佳实践
  • 分布式光伏气象站:为光伏电站的 “气象感知眼”
  • 【opencv-Python学习笔记(2): 图像表示;图像通道分割;图像通道合并;图像属性】
  • 云原生应用的DevOps3(CI/CD十大安全风险、渗透场景)
  • LeetCode 2787.将一个数字表示成幂的和的方案数:经典01背包
  • 小红书笔记信息获取_实在智能RPA源码解读
  • 使用 NetBird 创建安全的私有网络,简化远程连接!
  • 完整多端口 Nginx Docker部署 + GitLab Runner注册及标签使用指南
  • 从原理到实践:一文掌握Kafka的消息生产与消费
  • Unity:GUI笔记(一)——文本、按钮、多选框和单选框、输入框和拖动条、图片绘制和框绘制
  • 从零部署Nacos:替代Eureka的服务注册与服务发现基础教程
  • WPS文字和Word:不只是表格,段落也可以排序
  • 文字转语音 edge_tts
  • 微内核与插件化设计思想及其在前端项目中的应用
  • PostgreSQL 范围、空间唯一性约束
  • 用 Apache Iceberg 与 Apache Spark 在 Google Cloud 打造高性能、可扩展的数据湖仓
  • Flink运行时的实现细节
  • SQL 语言分类
  • Spark 运行流程核心组件(一)作业提交
  • 数据量暴涨时,抓取架构该如何应对?
  • 开发npm包【详细教程】
  • Bevy渲染引擎核心技术深度解析:架构、体积雾与Meshlet渲染
  • C++Linux八股
  • 08--深入解析C++ list:高效操作与实现原理