shell 检测 tcp 端口是否开放

2017年11月29日 0 条评论 384 次阅读 0 人点赞

[v_blue]脚本使用mutt命令发送邮件,可以替换为mail,使用方法http://www.sevenfal.com/2017/11/29/1019.html[/v_blue]

#!/bin/bash
ly_ver=1.0.1
date1="2017-12-04 14:09"
#curl 检测超时(秒)
tcp_timeout=3
#检测出现 3 次状态码不正常后邮件报警
mail_do=3
#故障未恢复再次告警时间(1小时以后)
let check_check=60*1

path1=/usr/local/monitor

which nc 1> /dev/null 2> /dev/null || yum install nc -y
if [ ! -x /usr/bin/monitor ];then
	cp $0 /usr/bin/monitor
	chmod +x /usr/bin/monitor
fi

if $(crontab -l |grep -q '^\* \* \* \* \* /usr/local/monitor/crontab_tcp$') ;then
	cd .
else 
	(crontab -l 2>/dev/null;echo '* * * * * /usr/local/monitor/crontab_tcp') |crontab -
fi

function ip_add(){
	echo "请输入需要添加监控的 ip 或域名(eg. www.sevenfal.com):"
	read check_ip
	read -p "请输需要添加监控的端口(eg. 22):" check_port
	until echo ${check_port} |grep -q "^[[:digit:]]\+$" ;do
		read -p "请输需要添加监控的端口(eg. 22):" check_port
	done
	echo "${check_port} ${check_ip} 0" >> ${path1}/ip_list.txt
}
function ip_list(){
	cat -n ${path1}/ip_list.txt
}
function ip_del(){
	ip_list
	read -p "请输入需要删除的行号(eg. 2):" del_line
	until echo ${del_line} |grep -q "^[[:digit:]]\+$" ;do
		read -p "请输入需要删除的行号(eg. 2):" del_line
	done
	sed -i "${del_line}d" ${path1}/ip_list.txt
	ip_list
}

function mail_add(){
	echo "请输入需要通知的邮箱地址(eg. [email protected]):"
	read mail_name
	echo "${mail_name}" >> ${path1}/mail_list.txt
}
function mail_list(){
	cat -n ${path1}/mail_list.txt
}
function mail_del(){
	mail_list
	read -p "请输入需要删除的行号(eg. 2):" del_line
	until echo ${del_line} |grep -q "^[[:digit:]]\+$" ;do
		read -p "请输入需要删除的行号(eg. 2):" del_line
	done
	sed -i "${del_line}d" ${path1}/mail_list.txt
	mail_list
}

function monitor_check(){
	mkdir -p ${path1}
	if [ ! -f ${path1}/ip_list.txt ];then
		touch ${path1}/ip_list.txt
	fi
	if [ ! -f ${path1}/mail_list.txt ];then
		touch ${path1}/mail_list.txt
	fi
	rm -rf ${path1}/crontab_tcp
	echo '#!/bin/bash' > ${path1}/crontab_tcp
	echo 'path1=/usr/local/monitor' >> ${path1}/crontab_tcp
	echo "tcp_timeout=${tcp_timeout}" >> ${path1}/crontab_tcp
	echo "check_check=${check_check}" >> ${path1}/crontab_tcp
	echo "mail_do=${mail_do}" >> ${path1}/crontab_tcp
	cat <<"EOF" >> ${path1}/crontab_tcp
cat -n ${path1}/ip_list.txt | while read val;do
	ip_num=`echo $val|awk -F ' ' '{print $1}'`
	check_port=`echo $val|awk -F ' ' '{print $2}'`
	check_ip=`echo $val|awk -F ' ' '{print $3}'`
	check_num=`echo $val|awk -F ' ' '{print $4}'`
	time1=`date "+%Y-%m-%d %H:%M:%S"`
	nc -z -w ${tcp_timeout} ${check_ip} ${check_port} && (
		sed -i "${ip_num} a${check_port} ${check_ip} 0" ${path1}/ip_list.txt
		sed -i "${ip_num}d" ${path1}/ip_list.txt
		if (( ${check_num} > ${mail_do} ))
			then
				for mail_name in `cat ${path1}/mail_list.txt`;do
					echo "解除时间:${time1} <br>故障地址:${check_ip}:${check_port} <br> 当前状态:<span style='color:green'>正常</span> <br>"  | mutt -s "故障解除:${check_ip}:${check_port} 端口正常!" -e 'set content_type="text/html"' ${mail_name}
				done
		fi
	) || (
		let check_num=${check_num}+1
		sed -i "${ip_num} a${check_port} ${check_ip} ${check_num}" ${path1}/ip_list.txt
		sed -i "${ip_num}d" ${path1}/ip_list.txt
		if [[ ${check_num} = ${mail_do} || ${check_num} = ${check_check} ]]
		  then
		    for mail_name in `cat ${path1}/mail_list.txt`;do
		      echo "故障时间:${time1} <br>故障地址:${check_ip}:${check_port} <br> 当前状态:<span style='color:red'>错误</span> <br>" | mutt -s "故障出现:${check_ip}:${check_port} 端口不可达!" -e 'set content_type="text/html"' ${mail_name}
		    done
		fi
	)

done
EOF
chmod +x ${path1}/crontab_tcp

}

echo "+-------------------------------------------+"
echo "|          Monitor for tcp port  ${ly_ver}      |"
echo "+-------------------------------------------+"
echo "|                        ${date1}   |"
echo "+-------------------------------------------+"
arg1=$1
arg2=$2
monitor_check


case ${arg1} in
	add)
		if [ "${arg2}" = "ip" ];then 
			ip_add
		elif [ "${arg2}" = "mail" ];then 
			mail_add
		else
			echo "Usage: monitor add {ip|mail}"
		fi
	;;
	list)
		if [ "${arg2}" = "ip" ];then 
			ip_list
		elif [ "${arg2}" = "mail" ];then 
			mail_list
		else
			echo "Usage: monitor list {ip|mail}"
		fi
	;;
	del)
		if [ "${arg2}" = "ip" ];then 
			ip_del
		elif [ "${arg2}" = "mail" ];then 
			mail_del
		else
			echo "Usage: monitor del {ip|mail}"
		fi
	;;
	*)
	echo "Usage: monitor {add|list|del} {ip|mail}"
	;;
esac

Sevenfal

这个人太懒什么东西都没留下

文章评论(0)