##安装expect yum -y install expect
###step1: 发送脚本文件到远程服务器;
###step2: 在远程服务器执行脚本;完成后删除脚本文件
#!/bin/bash IpList=`cat $1` for ip in $IpList do echo -e " 33[32m 'send script to $ip' 33[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 ";exp_continue} "*assword:*" {send "$s($i) ";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 " 33[32m 'execute script on $ip' 33[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 ";exp_continue} "*password:*" {send "$s($i) ";incr i;set okpasswd [expr {$i - 1}] ;exp_continue} "*]*" {send "hostname "} "Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)." {puts "ipaddr:$ip password:unkonw"; exit } } expect "]*" send { /opt/script.sh } send " " expect "]*" send { rm /opt/script.sh } send " " expect "]*" send "exit " } EOF done