#!/usr/bin/expect set host [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] spawn ssh $username@$host expect { "(yes/no)?" { send "yes\n" expect "*assword:" { send "$password\n"} } "*assword:" { send "$password\r" } } set pwd 1234567 expect "$" send "su -\r" expect "Password:" send "$pwd\r" expect "*]#" send "kill -9 `ps -ef|grep java|grep -v grep|awk '{print \$2}'`\r" expect "#" send "nohup su - tomcat -c /usr/local/tomcat6/bin/startup.sh &\r" send "\r" send "exit\r" send "exit\r" expect eof
bash脚本:调用expect脚本,bash文件名:bash_ssh.sh
#!/bin/sh rpm -q expect &>/dev/null if [ $? -ne 0 ]; then echo "install expect......" yum install -y expect > /dev/null fi list_file=$1 cat $list_file | while read line do host_name=`echo $line | awk '{print $1}'` username=`echo $line | awk '{print $2}'` password=`echo $line | awk '{print $3}'` ./expect_ssh $host_name $username $password done
建立host.list文件
# touch host.list 192.168.66.130 tom 123456 192.168.66.131 tom 123456
使用方法:
sh bash_ssh.sh host.list
原文地址:http://blog.51cto.com/stuart/2089771