1. 程式人生 > >scp批量複製檔案到多個伺服器

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