shell脚本批量执行scp操作,并解决密码输入问题

问题:

之前一直使用sftp等软件进行服务器之间的文件传输,存在传输速度慢等缺点,因此尝试使用scp传输多个文件,发现大量重复工作,因此,考虑使用shell脚本来执行此操作。但是存在在进行scp操作时,需要远程服务器的密码?

解决方案:

1. 首先介绍一下scp远程传输文件操作。

 scp -P port -r user@ip:/dir_name file_name

 其中 port 表示远程服务器的的端口号

filename表示本机的文件路径

user表示远程服务器的用户名

ip表示远程服务器的IP地址

dir_name表示需要传输的文件绝对路径

2.了解好scp的用法之后,就需要写shell脚本文件

#!/bin/bash

remote_user="user_name"
remote_host="ip_addrest"
remote_port="port"
remote_path="file_path"

local_path="file_path"

subdirectories=("0050_02"....."your file names")


for subdir in "${subdirectories[@]}"; do
    scp -P "$remote_port" -r "$remote_user@$remote_host:$remote_path/$subdir" "$local_path"
done

3.解决完shell脚本的问题之后,就需要解决远程服务器的密码问题。对于这个问题,考虑使用SSH密钥来解决。

3.1确保本地机器上有SSH密钥对,如果没有,可以使用命令来生成

ssh-keygen -t rsa

3.2将生成的公钥(一般在~/.ssh/id_rsa.pub)添加到远程服务器的~/.ssh/authorized_keys 文件中

ssh-copy-id -p port user@ip address

3.3修改之前完成的shell脚本

#!/bin/bash

remote_user="user_name"
remote_host="ip_addrest"
remote_port="port"
remote_path="file_path"

local_path="file_path"

subdirectories=("0050_02"....."your file names")


for subdir in "${subdirectories[@]}"; do
    scp -P "$remote_port"\
     -r -i ~/.ssh/id_rsa \
    "$remote_user@$remote_host:$remote_path/$subdir"\
    "$local_path"
done

这样就可以批量的在服务器之间传输文件了!!!

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值