脚本:通过ssh、scp和expect批量复制文件到其它设备,已解决传输文件不完整的问题
生活随笔
收集整理的這篇文章主要介紹了
脚本:通过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 EOFif [ $? -eq 0 ]; thenecho "${line}成功"elseecho "${line}失敗"fi done二、實際應用(cpFilesToRemoteNodes.sh)
-
特點:
加入了ping IP的操作,避免連接超時消耗太多時間的問題
網絡不好的時候,也能傳輸文件100%
能夠返回傳輸成功和失敗的設備ip -
使用步驟如下:
1、使用vi創建一個關于ip.txt文件,并填寫或粘貼ip到文件里
2、執行腳本
3、效果
4、腳本內容(cpFilesToRemoteNodes.sh)
#!/bin/bash: <<NOTDOING function:from host 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 0remoteIp=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 "請輸入遠程主機放置目錄:" remoteDirecho -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" ]; thenecho -e "\033[32m 退出 \033[0m" exit 0 fi #NOTDOINGecho -e "\033[32m \n開始(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 <<-EOFset timeout -1spawn scp -r $1 $2@$4:$5expect {"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 <<-EOFspawn ssh $1@$3 $4expect {"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" ]]; thenecho -e "\033[32m 單個操作 \033[0m"echo -e "\033[32m \n 文件傳輸到${remoteIp} \033[0m"fun_scp_files ${localFiles} ${remoteUser} ${remotepassword} ${remoteIp} ${remoteDir}if [ $? -eq 0 ]; thenecho ${remoteIp} >> ${yesFile}sleep 1echo -e "\033[32m 命令操作到${remoteIp} \033[0m"fun_ssh_cmd ${remoteUser} ${remotepassword} ${remoteIp} "${sshcmd}"elseecho ${remoteIp} >> ${noFile}fielseecho -e "\033[32m 批量操作 \033[0m"for line in $(cat ${remoteIp})do$(ping ${line} -c 1 > /dev/null 2>&1)if [ $? -eq 0 ]; thenecho -e "\033[32m \n 文件傳輸到${line} \033[0m"fun_scp_files ${localFiles} ${remoteUser} ${remotepassword} ${line} ${remoteDir}if [ $? -eq 0 ]; thenecho ${line} >> ${yesFile}sleep 1echo -e "\033[32m 命令操作到${line} \033[0m"fun_ssh_cmd ${remoteUser} ${remotepassword} ${line} "${sshcmd}"elseecho ${line} >> ${noFile}fisleep 4elseecho ${line} >> ${noFile}fidone fisleep 2 echo -e "\033[32m \n結束------------- \033[0m" echo -e "\033[32m \n成功的有:\033[0m" [ -e ${yesFile} ] && cat ${yesFile} echo -e "\033[31m \n失敗的有:\033[0m" [ -e ${noFile} ] && cat ${noFile} exit 0總結
以上是生活随笔為你收集整理的脚本:通过ssh、scp和expect批量复制文件到其它设备,已解决传输文件不完整的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Js简单实现音乐播放器
- 下一篇: R语言实现双变量分析教程