#!/usr/bin/expect
#定义变量
set passwd "123abc321"
#执行命令
spawn ssh root@192.168.10.11
#与远程机器交互 截取特定信息 发送变量
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
interact
#!/usr/bin/expect
set host "192.168.80.102"
set passwd "aaa123bbb"
spawn ssh user1@$host
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" {send "$passwd\r"}
}
expect "]*"
send "touch /tmp/test.aa\r"
expect "]*"
send "echo 111 >/tmp/test.aa\r"
expect "]*"
send "exit\r"
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" {send "yes\r"}
"password:" {send "$passwd\r"}
}
interact
#!/bin/bash
#批量关闭远程机器上的Tomcat服务
#作者:阿铭
#日期:2018-10-29
ipfile=/data/ip-passwd.txt
cat >> kill_tomcat.exp <<EOF
#!/usr/bin/expect
set passwd [lindex \$argv 0]
set host [lindex \$argv 1]
spawn ssh tomcat@\$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "\$passwd\r" }
}
expect "]*"
send "/opt/tomcat/bin/shutdown.sh\r"
expect "]*"
send "if ps aux |grep -q tomcat; then killall -9 java; fi"
expect "]*"
send "exit\r"
EOF
chmod a+x kill_tomcat.exp
cat $ipfile | while read line
do
ip=`echo $line|awk '{print $1}'`
pw=`echo $line|awk '{print $2}'`
./kill_tomcat.exp $pw $ip
done