expect自动检测并重启另外一台服务器上的程序的代码,有需要的朋友可以参考下。
[s005 you] # cat haproxy_expect
#!/usr/bin/expect
set ssh_user "fivetrees"
set password "123456"
spawn ssh -i /root/.ssh/$ssh_user Server004.xd.com
expect_before "no)?" {
send "yesr" }
sleep 0.5
expect "Enter passphrase for key*"
send "$passwordr"
expect "*#"
send "/tmp/haproxy.shr"
expect "*#"
send "echor"
exit
[s004 him] # cat haproxy.sh
#!/bin/bash
Thread=`ps -ef | grep haproxy | grep -v haproxy.sh | grep -v grep`
if [ -z "$Thread" ]
then
/tmp/haproxy_expect
fi
[s004 him] # cat haproxy_expect
#!/usr/bin/expect
set ssh_user "fivetrees"
set password "123456"
spawn ssh -i /root/.ssh/$ssh_user Server005.xd.com
expect_before "no)?" {
send "yesr" }
sleep 0.5
expect "Enter passphrase for key*"
send "$passwordr"
expect "*#"
send "/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfgr"
expect "*#"
send "echor"
exit
您可能感兴趣的文章:
expect脚本远程批量管理服务器的思路解析
expect批量管理计算机的一个脚本
一个远程登录的简单expect脚本
批量修改ssh配置的expect脚本
expect自动登录远程服务器和执行重复任务的例子
ssh expect自动登录的脚本
远程登录的简单expect脚本
Expect的安装与应用
用expect写的一个脚本