Expect script: file name: expect_ssh
#!/usr/bin/expectset host [lindex $argv 0]set username [lindex $argv 1]set Password [lindex $argv 2]spawn ssh [email prote CTED] $hostexpect {"(yes/no)?" {send "yes\n" expect "*assword:" {send "$password \ n"}} "*assword:" {send "$password \ r"}}set pwd 1234567expect "$" 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 E Of
Bash script: Call expect script, bash file name: bash_ssh.sh
#!/bin/shrpm-q expect &>/dev/nullif [$?-ne 0]; Then echo "Install expect ..." yum install-y expect >/dev/nullfilist_file=$1cat $list _file | While read Linedo host_name= ' echo $line | awk ' {print $} ' username= ' echo $line | awk ' {print $} ' password= ' echo $line | awk ' {print $} './expect_ssh $host _name $username $passworddone
Create a Host.list file
# Touch host.list192.168.66.130 Tom 123456192.168.66.131 Tom 123456
How to use:
SH bash_ssh.sh host.list
Shell+expect Completing the remote startup service