scp批量複製文件到多個服務器

一、安裝expect

yum install expect -y

二、編輯自動輸入密碼腳本

#!/usr/bin/expect  
set timeout 20  
  
if { [llength $argv] < 2} {  
    puts "Usage:"  
    puts "$argv0 local_file remote_path"  
    exit 1  
}  
  
set local_file [lindex $argv 0]  
set remote_path [lindex $argv 1]  

set passwd  YOURPASSWD

set passwderror 0  
  
spawn scp $local_file $remote_path  
  
expect {  
    "*assword:*" {  
        if { $passwderror == 1 } {  
        puts "passwd is error"  
        exit 2  
        }  
        set timeout 1000  
        set passwderror 1  
        send "$passwd\r"  
        exp_continue  
    }  
    "*es/no)?*" {  
        send "yes\r"  
        exp_continue  
    }  
    timeout {  
        puts "connect is timeout"  
        exit 3  
    }  

}  

三、編輯遍歷服務器腳本

 

#!/bin/bash

ipss="192.168.1.1 192.168.1.2 192.168.1.3"

for ips in $ipss
do
  /pwd/scp.exp source_file user@${ips}:target_dir
done

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章