欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > windows与linux服务器之间通过scp快速并行的文件传输

windows与linux服务器之间通过scp快速并行的文件传输

2025/6/1 0:23:13 来源:https://blog.csdn.net/qq_41249412/article/details/141200853  浏览:    关键词:windows与linux服务器之间通过scp快速并行的文件传输

在大数据时代,有很多数据集包含很多小文件,这些小文件在不同设备之间的传输是一个很大的问题
sftp在传输小文件时,往往很慢。因此下面通过scp进行并行的文件传输,速度提升20倍左右。

首先需要在windows安装openSSH

基于PowerShell的OpenSSH:https://github.com/PowerShell/Win32-OpenSSH/releases
安装步骤
1、进入链接下载最新 OpenSSH-Win64.zip(64位系统),解压至D:\DEVELOPEWORKS\OpenSSH-Win64
2、打开cmd,cd进入D:\DEVELOPEWORKS\OpenSSH-Win64(安装目录),执行命令:

powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
# 设置服务自启动
sc config sshd start= auto# 启动sshd服务
net start sshd

参考教程
https://blog.csdn.net/boonya/article/details/102811966

然后使用ssh秘钥连接linux服务器(为了并行scp,可以不用重复输入密码)

1. 生成SSH密钥对(如果还没有):

ssh-keygen -t rsa

在生成SSH密钥时,你会被要求输入一些信息。以下是每个步骤的解释:

Enter file in which to save the key (C:\Users\Administrator/.ssh/id_rsa):

这里系统默认会将密钥保存到C:\Users\Administrator/.ssh/id_rsa。通常情况下,你可以直接按回车键使用默认路径。如果你指定了id_rsa.pub作为文件名,系统实际上会生成id_rsa.pub.pub,因为它会自动在文件名后面添加.pub。
建议:直接按回车使用默认路径,或者输入一个你希望存储私钥的路径,比如id_rsa。

Enter passphrase (empty for no passphrase):

你可以选择为你的SSH密钥设置一个密码短语,这样在使用该密钥时需要输入这个密码短语以增加安全性。如果你希望无密码登录,则直接按回车键跳过这个步骤。
建议:如果你希望SSH密钥能够自动登录而不需要每次输入密码,可以直接按回车跳过设置密码短语。

Confirm passphrase: 如果你设置了密码短语,系统会要求你再次输入以确认。

Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\Administrator/.ssh/id_rsa): [按回车]
Enter passphrase (empty for no passphrase): [按回车]
Enter same passphrase again: [按回车]

2. 将公钥复制到远程服务器

手动复制SSH公钥
打开PowerShell或CMD,然后使用以下命令查看并复制公钥的内容:

cat ~/.ssh/id_rsa.pub

在Windows上,如果你使用了默认路径,命令可能需要指定完整路径:

type C:\Users\Administrator\.ssh\id_rsa.pub

这会输出公钥内容,像这样:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq3Te0nKe...你的公钥内容... user@hostname

连接到远程服务器

ssh -p **** username@**.**.**.**

创建.ssh目录(如果不存在)
一旦连接到服务器,运行以下命令以确保.ssh目录存在:

mkdir -p ~/.ssh
chmod 700 ~/.ssh

将公钥添加到authorized_keys
将上面的公钥内容直接复制到authorized_keys

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq3Te0nKe...你的公钥内容... user@hostname

设置正确的权限

chmod 600 ~/.ssh/authorized_keys

退出服务器
测试无密码登录
完成上述步骤后,你可以尝试从Windows终端使用SSH密钥登录:

ssh -p **** username@**.**.**.**

如果一切正常,你应该能够直接登录到服务器,而无需输入密码。
同样scp也不需要密码,这样就可以通过python程序进行并行scp,且不用重复输入密码了。

最后是并行scp的python脚本

import subprocess
from concurrent.futures import ThreadPoolExecutor
import os
# 定义并行传输的函数
def scp_upload(file_path, remote_path, port=****):command = f"scp -r -C -P {port} {file_path} username@**.**.**.**:{remote_path}"try:subprocess.run(command, shell=True, check=True)print(f"Successfully uploaded {file_path}")except subprocess.CalledProcessError as e:print(f"Error uploading {file_path}: {e}")# 主函数
def main():# 文件列表,可以将所有需要上传的文件或目录放在这里rootPath = 'H:/胎儿胎盘'fList = os.listdir(rootPath)files_to_upload = []for f in fList:ft = os.path.join(rootPath, f)files_to_upload.append(ft)# files_to_upload = [#     "01140623206020/file1",  # 假设每个文件在子目录中#     "01140623206020/file2",#     # 添加更多文件或目录# ]remote_base_path = "/RAID5/projects/fuxingwen/gxb/pregnacy_predict/dataset/BYSY"# 使用ThreadPoolExecutor进行并行上传with ThreadPoolExecutor(max_workers=16) as executor:  # 可以根据CPU数量调整workers数量for file_path in files_to_upload:remote_path = f"{remote_base_path}/{file_path.split('/')[-1]}"executor.submit(scp_upload, file_path, remote_path)if __name__ == "__main__":main()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词