一、简单示例
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