看了很多例子都是/usr/bin/epxect 和 /bin/bash分开使用
经过测试是可以合在一起进行编程。
#!/bin/bash
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:"
LANG=zh_CN
export PATH LANG
USERNAME="s"
############
# 主机信息 #
############
INTF=$(netstat -rn | tail -1 | awk '{print $NF}')
IP=$(ifconfig $INTF | awk '/inet addr/{ split($2,tmp,":") ; print tmp[2] }')
HOST_NAME=$(hostname --short)
###########
##########
#邮件环境#
##########
#CHARTSET=""
MAIL_CLIENT=""
MAIL_SENDER=""
MAIL_SERVER=""
case $IP in
192.168.230.*|192.168.1[0-1].*|192.168.238.*|209.*)
MAIL_SERVER=""
;;
*)
MAIL_SERVER=""
;;
esac
##########
tap=`expect -c "
set timeout 200;
spawn /usr/bin/ssh "$USERNAME"@"$IP" 'pwd'
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"xxx\r\";}
}
expect eof;"`
if echo $tap | grep -q "/home/support"
then
echo "machinealine_=0"
else
echo "machinealine_=1"
echo "机器假死了,SSH连接不上去了" > /tmp/.deadalive
#########发送邮件############
env MAILRC=/dev/null charset="$CHARTSET" from="$MAIL_SENDER" smtp="$MAIL_SERVER" \
nail -n -s "$HOST_NAME ($IP)假死检测" $MAIL_CLIENT < /tmp/.deadalive
fi
文章评论