一、安装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