SCP批量传输脚本

SCP批量传输脚本

介绍

SCP(Secure Copy)命令是SSH中最方便有用的命令之一,用于在两台服务器之间直接传送文件。

用法如下:

  • 从本地复制到远程

    • 文件操作:
scp local_file remote_username@remote_ip:remote_folder
    • 目录操作:
scp -r local_folder remote_username@remote_ip:remote_folder
  • 从远程复制到本地

    • 文件操作:
scp remote_username@remote_ip:remote_file local_file
    • 目录操作:
scp -r remote_username@remote_ip:remote_folder local_folder

单台传输

#!/usr/bin/expect
if {$argc < 2} {
        send_user "usage: $argv0 src_file username ip dest_file password\n"
exit
}
##set key [lindex $argv 0]
set src_file [lindex $argv 0]
set username [lindex $argv 1]
set host_ip [lindex $argv 2]
set dest_file [lindex $argv 3]
set password [lindex $argv 4]
##spawn scp -i $key $src_file $username@$host_ip:$dest_file
spawn scp  $src_file $username@$host_ip:$dest_file
expect {
        "(yes/no)?"
                {
                        send "yes\n"
                        expect "password:" {send "$password\n"}
                }
        "password:"
                {
                        send "$password\n"
                }
}
expect "100%"
expect eof

多台传输

  • 编写服务器信息文件
vim server_list.conf
ip 用户名 密码 源文件 目标文件地址
  • 脚本

#!/bin/bash
host_list="server_list.conf"
cat $host_list | while read line
do
  host_ip=`echo $line|awk '{print $1}'`
  username=`echo $line|awk '{print $2}'`
  password=`echo $line|awk '{print $3}'`
  src_file=`echo $line|awk '{print $4}'`
  dest_file=`echo $line|awk '{print $5}'`
  ##key=`echo $line|awk '{print $6}'`
  ##./allscp.sh $key $src_file $username $host_ip $dest_file $password
  ./allscp.sh $src_file $username $host_ip $dest_file $password
done
posted @ 2023-10-25 09:19  寻梦99  阅读(443)  评论(0)    收藏  举报