脚本:通过ssh、scp和expect批量复制文件到其它设备,已解决传输文件不完整的问题

一、简单示例

remoteUser=
remotepassword=
localFiles=test.txt
remoteDir=/home/nvidia/Downloads/

for line in $(cat ip.txt)
do
expect <<EOF
set timeout -1
spawn scp -r ${localFiles} ${remoteUser}@${line}:${remoteDir}
expect {
	"yes/no" {send "yes\r"; exp_continue}
	"password:" {send "${password}\r"}
}
expect 100%
expect eof
EOF
  if [ $? -eq 0 ]; then
    echo "${line}成功"
  else
    echo "${line}失败"
  fi
done

二、实际应用(cpFilesToRemoteNodes.sh)

  • 特点:
    加入了ping IP的操作,避免连接超时消耗太多时间的问题
    网络不好的时候,也能传输文件100%
    能够返回传输成功和失败的设备ip

  • 使用步骤如下:
    1、使用vi创建一个关于ip.txt文件,并填写或粘贴ip到文件里
    在这里插入图片描述

2、执行脚本
在这里插入图片描述

3、效果
在这里插入图片描述

4、脚本内容(cpFilesToRemoteNodes.sh)

#!/bin/bash

: <<NOTDOING
function:server scp files to remotehost
author:zyh
date:2022.04.22
you need to apt-get install expect
NOTDOING

[ ! -e /usr/bin/expect ] && echo -e "\033[31m failed! you need to apt-get install expect \033[0m" && exit 1


remoteIp=10.10.151.108
remoteUser=
remotepassword=
localFiles=test.txt
remoteDir=/home/nvidia/Downloads/

yesFile=yes.txt
noFile=no.txt
rm -f ${yesFile} ${noFile}


echo -e "\033[32m \n本次操作将复制单个文件/多个文件/文件夹到远程服务器设备,并重启 \033[0m"

#: <<NOTDOING
read -p "请输入本地传输文件:" localFiles && \
read -p "请输入远程主机ip(10.10.x.x 或 xxip.txt):" remoteIp && \
read -p "请输入远程主机ssh用户名:" remoteUser && \
read -p "请输入远程主机ssh密码:" remotepassword && \
read -p "请输入远程主机放置目录:" remoteDir

echo -e "\033[32m \n请确认信息 \033[0m"
echo "本地传输文件:${localFiles}"
echo "远程主机ip:${remoteIp}"
echo "远程主机ssh用户名:${remoteUser}"
echo "远程主机ssh密码:${remotepassword}"
echo "远程主机放置目录:${remoteDir}"
echo "" 

read -p "please enter (yes/no):" enter
if [ "${enter}" != "yes" ]; then
  echo -e "\033[32m 退出 \033[0m" 
  exit 1
fi
#NOTDOING

echo -e "\033[32m开始(start)---------------------\n \033[0m"


sshcmd="sync && reboot"
#sshcmd="cd /tmp/ &&  tar -zxf ${localFiles} && tar -zxf ${localFiles} && cd /tmp/home/ && ./upgrade.sh && sync && reboot"
#sshcmd="cd /tmp/ && chmod +x ${localFiles} && ./${localFiles} && sync && reboot"


#${localFiles} ${remoteUser} ${remotepassword} ${remoteIp} ${remoteDir}
function fun_scp_files {
expect <<-EOF
	set timeout -1
	spawn scp -r $1 $2@$4:$5
	expect {
		"to continue connecting (yes/no" {send "yes\r"; exp_continue}
		"password:" {send "$3\r"}
	}
	expect 100%
	expect eof
EOF
}


#${remoteUser} ${remotepassword} ${remoteIp} ${sshcmd}
function fun_ssh_cmd {
expect <<-EOF
	spawn ssh $1@$3 "$4"
	expect {
		"No route to host" exit
		"Connection refused" exit
		"Name or service not known" exit
		"to continue connecting (yes/no" {send "yes\r"; exp_continue}
		"password:" {send "$2\r"}
	}
	expect eof
EOF
}



if [[ "${remoteIp}" != *".txt" ]]; then
  echo -e "\033[32m单个操作 \033[0m"
  echo -e "\033[32m \n文件传输到${remoteIp} \033[0m"
  
  fun_scp_files ${localFiles} ${remoteUser} ${remotepassword} ${remoteIp} ${remoteDir}
  if [ $? -eq 0 ]; then
    echo ${remoteIp} >> ${yesFile}
	
    sleep 1
    echo -e "\033[32m命令操作到${remoteIp} \033[0m"
	fun_ssh_cmd ${remoteUser} ${remotepassword} ${remoteIp} "${sshcmd}"
  else
    echo ${remoteIp} >> ${noFile}
  fi
  
else
  echo -e "\033[32m批量操作 \033[0m"
  for line in $(cat ${remoteIp})
  do
    $(ping ${line} -c 1 > /dev/null 2>&1)
    if [ $? -ne 0 ]; then
      echo "${line} 网络不通" >> ${noFile}
      continue
    fi
	
    echo -e "\033[32m \n文件传输到${line} \033[0m"
    fun_scp_files ${localFiles} ${remoteUser} ${remotepassword} ${line} ${remoteDir}
    if [ $? -ne 0 ]; then
      echo "${line} 传输文件失败" >> ${noFile}
      continue
    fi
    sleep 4
	
    echo -e "\033[32m \n命令操作到${line} \033[0m"
    fun_ssh_cmd ${remoteUser} ${remotepassword} ${line} "${sshcmd}"
    if [ $? -ne 0 ]; then
      echo "${line} 命令操作失败" >> ${noFile}
      continue
    fi	
    sleep 1
	
    echo ${line} >> ${yesFile}
  done
fi

sleep 2
echo -e "\033[32m \n结束------------- \033[0m"
echo -e "\033[32m成功的有:\033[0m"
[ -e ${yesFile} ] && cat ${yesFile}
echo -e "\033[31m \n失败的有:\033[0m"
[ -e ${noFile} ] && cat ${noFile}
exit 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤劳的搬运工zyh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值