批量scp脚本——从多台机器拷贝文件
為了方便的從多臺(tái)服務(wù)器獲取日志,寫(xiě)了個(gè)簡(jiǎn)易腳本專(zhuān)門(mén)用于批量拷貝服務(wù)器日志到執(zhí)行腳本的機(jī)器中。該腳本包含2個(gè)文件bscp.sh和bscp.exp。
使用方式:
sh bscp.sh <username> <host1,host2> <log_file>
username:你ssh到目標(biāo)機(jī)器的密碼。
host1,host2:目標(biāo)機(jī)器的ip或者機(jī)器名,多個(gè)之間用逗號(hào)分割。
log_file:你要批量下載的日志的絕對(duì)路徑。
運(yùn)行后,程序提示輸入目標(biāo)機(jī)器的密碼(這里需要多臺(tái)機(jī)器的ssh用戶(hù)名密碼是相同的,且之前需要建立過(guò)ssh連接,就是不需要再進(jìn)行yes/no交互)
bscp.sh主程序:
?
#!/bin/bash if [ $# != 3 ] ;thenecho "usage:<username> <host1,host2> <log_file>"exit 1 fi stty -echo #隱藏密碼輸出 read -p "Please enter target hosts' passwd of $1:" passwd stty echo echodirpath=`dirname $0` #echo $dirpath $dirpath/bscp.exp $1 $2 $3 $passwd?
expect腳本:
?
#!/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 $filenamespawn scp $user@$h:$logfile ./$filename.$hexpect "*Enter passphrase for key*" { # 這里可以改成其他可能出現(xiàn)的顯示文字,如password:等.或者加多yes/no的交互環(huán)節(jié)send "$passwd\r"send "\r"}expect "*%*" {set timeout -1 ; puts "\rtrasmitting..."}expect eof { # 下載完成后輸出成功信息puts "\rtransmit successfully!"set timeout 10} }?
?
一個(gè)栗子:
執(zhí)行獲取3臺(tái)機(jī)日志:
./bscp.sh ultrani host1,host2,host3 /home/admin/xxx/logs/access.log
結(jié)果是把3臺(tái)機(jī)器的日志下載到執(zhí)行腳本的目錄中
日志后綴以機(jī)器名結(jié)尾:
access.log.host1
access.log.host2
access.log.host3
?
轉(zhuǎn)載于:https://www.cnblogs.com/dyllove98/archive/2013/06/15/3137583.html
總結(jié)
以上是生活随笔為你收集整理的批量scp脚本——从多台机器拷贝文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: POJ 3694 Network ★(边
- 下一篇: 王家林 云计算分布式大数据Hadoop实