批量scp指令碼——從多台機器拷貝檔案

來源:互聯網
上載者:User

批量scp指令碼——從多台機器拷貝檔案 為了方便的從多台伺服器擷取日誌,寫了個簡易指令碼專門用於批量拷貝伺服器日誌到執行指令碼的機器中。該指令碼包含2個檔案bscp.sh和bscp.exp。 使用方式: sh bscp.sh <username> <host1,host2> <log_file>username:你ssh到目標機器的密碼。host1,host2:目標機器的ip或者機器名,多個之間用逗號分割。log_file:你要批量下載的日誌的絕對路徑。 運行後,程式提示輸入目標機器的密碼(這裡需要多台機器的ssh使用者名稱密碼是相同的) bscp.sh主程式: [plain] #!/bin/bash  if [ $# != 3 ]    ;then      echo "usage:<username> <host1,host2> <log_file>"      exit 1  fi  stty -echo               #隱藏密碼輸出  read -p "Please enter target hosts' passwd of $1:" passwd  stty echo  echo    dirpath=`dirname $0`  #echo $dirpath  $dirpath/bscp.exp $1 $2 $3 $passwd   expect指令碼:[plain] #!/usr/bin/expect -f  set user [lindex $argv 0]  set hosts [lindex $argv 1]  set logfile [lindex $argv 2]  set passwd [lindex $argv 3]  set timeout 10  set hostlist [split $hosts ","]             # 把host字串分割成列表    set slashIdx [expr [string last / $logfile] + 1]   set filename [string range $logfile $slashIdx end]  # 擷取記錄檔名    foreach h $hostlist {      set hostfile $filename      spawn scp $user@$h:$logfile ./$filename.$h      expect "*Enter passphrase for key*" {   # 這裡可以改成其他可能出現的顯示文字,如password:等.或者加多互動環節          send  "$passwd\r"          send  "\r"      }      expect "*%*" {set timeout -1 ; puts "\rtrasmitting..."}      expect eof {                            # 下載完成後輸出成功資訊          puts "\rtransmit successfully!"          set timeout 10      }  }   一個栗子: 執行擷取3台機日誌:./bscp.sh ultrani host1,host2,host3 /home/admin/xxx/logs/access.log 結果是把3台機器的日誌下載到執行指令碼的目錄中 日誌尾碼以機器名結尾:access.log.host1access.log.host2access.log.host3
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.