shell脚本

现在有两台debian服务器,服务器A,服务器B想写一个脚本,服务器A down掉后,服务器B的dhcp能够起来。然后在计划任务里运行,我这样想的
判断是服务器B否能ping通服务器A的心跳接口
如果不能ping通服务器A
判断服务器B的dhcp服务是否开启
如果开启了dhcp
不执行任何操作
如果没有开启dhcp
开启dhcp
服务器A与B的心跳线IP地址为10.0.0.1/24 10.0.0.2/24
有谁能帮忙弄个脚本,谢谢

作者: F5刷新   发布时间: 2011-08-29

[root@localhost ~]# cat dhcp.sh
#!/bin/bash
HostA="192.168.100.254"
HostB="192.168.100.2"
Server="vsftpd"
while [ -e /etc/init.d/$Server ]
do
echo $(/bin/date +%X) && /bin/ping -c 2 -f $HostA &>/dev/null || /etc/init.d/$Server status || /etc/init.d/$Server start
sleep 20 //等待20秒
done
[root@localhost ~]# ./dhcp.sh
04:35:52  //第一次检测时间
04:36:12
04:36:32
04:36:52
04:37:12
04:37:32 //HostA Down机时间
vsftpd (pid 9095) is running... //J检测服务启动
04:38:02
vsftpd (pid 9095) is running...

再修改一下
#!/bin/bash
HostA="10.0.0.1"
HostB="10.0.0.2"
Server="dhcpd"
while [ -e /etc/init.d/$Server ]
do
/bin/ping -c 2 -f $HostA &>/dev/null || /etc/init.d/$Server status || /etc/init.d/$Server start
sleep 20
done
//每20秒检测一次HostA是否存活,如果存在则等20秒之后继续检测,如果HostA已经Down掉,则检查DHCP服务时候正常启动,如果正常启动则等20秒之后继续检测,如果DHCP服务没有启动,则启动DHCP服务
之后 执行命令 nohup ./dhcp.sh & //放入后台一直运行
之后 echo "/usr/bin/nohup /root/dhcp.sh &"  >> /etc/rc.local //设置开机自动启动

作者: 风舞紫晓   发布时间: 2011-08-29

再修改一下
#!/bin/bash
HostA="10.0.0.1"
HostB="10.0.0.2"
Server="dhcpd"
while [ -e /etc/init.d/$Server ]
do
/bin/ping -c 2 -f $HostA &>/dev/null || /etc/init.d/$Server status
if [ $? -eq 0 ];then
exit
else
/etc/init.d/$Server start && exit
fi
sleep 20
done
//应该是检测到启用本地主机之后,就不需要检测HostA了,退出脚本,然后再在HostA上执行这个脚本检测HostB

作者: 风舞紫晓   发布时间: 2011-08-29