Batch Configuration Host --expect

## mounted Expect 

yum -Y the install Expect

### step1: send the script file to a remote server;

### step2: execute scripts on a remote server; delete the script file after completion

#!/bin/bash

IpList=`cat $1`

for ip in $IpList
do
echo -e "\033[32m 'send script to $ip' \033[0m"
/usr/bin/expect <<-EOF
set timeout -1
set passwdlist { username {password1 password2 password3 ...} }
foreach {u p} \$passwdlist {
spawn  scp script.sh \$u@$ip:/opt/
lassign \$p s(1) s(2) s(3)
set i 1
expect {
"*yes/no*"  {send "yes\r";exp_continue}
"*assword:*" {send "\$s(\$i)\r";incr i;set okpasswd [expr {\$i - 1}]; exp_continue}
"*]*" { puts "scp completed"; exit}
"Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)." {puts "ipaddr:$ip password:unkonw"; exit }
}
}
EOF
done

for ip in $IpList
do
echo -e "\033[32m 'execute script on $ip' \033[0m"
/usr/bin/expect <<-EOF
set timeout -1
set passwdlist { username {password1 password2...} }
foreach {u p} \$passwdlist {
spawn ssh \$u@$ip
lassign \$p s(1) s(2) s(3)
set i 1
expect {
"*yes/no*" {send "yes\r";exp_continue}
"*password:*" {send "\$s(\$i)\r";incr i;set okpasswd [expr {\$i - 1}] ;exp_continue}
"*]*" {send "hostname\r"}
"Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)." {puts "ipaddr:$ip password:unkonw"; exit }
}
expect "]*"
send { /opt/script.sh } 
send "\r"
expect "]*"
send { rm /opt/script.sh }
send "\r"
expect "]*"
send "exit\r"
}
EOF
done

Guess you like

Origin www.cnblogs.com/gy99/p/12175105.html