Shell脚本实现网络连通性检查Ping+Telnet

#!/bin/sh

###############################################################################################
#
#    Version :1.0
#
#     AUTHOR : LiuBS
#CREATE DATE : 2019-01-11
#   PLATFORM : Linux/AIX/HP-UX
#      USAGE : sh
#   FUNCTION : checkLink
#
export LC_ALL=zh_CN         #Set the LANG and all environment variable beginning with LC_ to C
export LANG=zh_C
###############################################################################################

################################public param###################################################
     checkpath=`pwd`
	iplist="${checkpath}/iplist.txt"                    		     #ip list
    resultfile="/tmp/resultfile.tmp"                        		 #result file
      filetime=`date +'%Y%m%d%H%M%S'`                      		     #current time
       logfile="${checkpath}/checkLink${filetime}.log"               #log file	
        pingok="${checkpath}/checkLink_pingok${filetime}.log"        #log pingok
     pingnotok="${checkpath}/checkLink_pingnotok${filetime}.log"	 #log pingnotok
      telnetok="${checkpath}/checkLink_telnetok${filetime}.log"		 #log telnetok
   telnetnotok="${checkpath}/checkLink_telnetnotok${filetime}.log"	 #log telnetnotok
          
################################public func####################################################
function log
{	
	local logcontent=$*
	echo "[`date +"%Y-%m-%d %T"`]:${logcontent}"
}

function pingCheck
{
	local ip=$1
	if [ ! -z $ip ];then
		local rate=`ping -c 1 -w 3 $ip|grep 'packet loss'|grep -v grep|awk -F',' '{print $3}'|awk -F'%' '{print $1}'|awk '{print $NF}'`
        	if [ "${rate}" = "errors" ]; then
			rate=`ping -c 1 -w 3 $ip|grep 'packet loss'|grep -v grep|awk -F',' '{print $4}'|awk -F'%' '{print $1}'|awk '{print $NF}'`
		fi
		return "${rate}"
	fi

}

function telnetCheck
{
	local ip=$1
	local port=$2
	if [ ! -z $ip ];then
  		timeout 10 telnet $ip $port<<EOF 2>${resultfile} 1>&2
quit
EOF
	fi
}

################ main #############################################################

#default value
if [ $# -eq 0 ];then
        port=15000
else
	if [ $# -gt 1 ];then
		log "The checkLink's params is too many ,please input 1 parameter!"
		exit -1
        else
		port=$1
	fi
fi

# Judaging OS Type and Loading the profile.
if [ `uname`="Linux" ]
then
	profile=.bash_profile
else
	profile=.profile
fi

if [ -f $HOME/${profile} ]
then
	. $HOME/${profile}
fi

#Init log file
>${logfile}
>${pingok}
>${pingnotok}
>${telnetok}
>${telnetnotok}

#Check link ping and telnet
while read line
do
	pingCheck ${line}
        pingRet=$?
	if [ "${pingRet}" = "100" ]; then
		log "===> ping ${line},ping is not ok!" |tee -a ${logfile}
		echo ${line} >> ${pingnotok}
	else
		log "===> ping ${line},ping is ok!" |tee -a ${logfile}
		echo ${line} >> ${pingok}
		telnetCheck $line $port 
		result=`cat ${resultfile} 2>/dev/null |grep "Connection closed by foreign host"|wc -l`
		#log "The result of telnet is ${result}"
		if [ $result -eq 1 ]
       		then
			log "===> telnet ${line} ${port},telnet is ok!"|tee -a ${logfile}
			echo ${line} >> ${telnetok}
		else
			log "===> telnet ${line} ${port},telnet is not ok!"|tee -a ${logfile}
			echo ${line} >> ${telnetnotok}
		fi	
	fi
done < $iplist

#sum data
ipSum=`cat $iplist|wc -l`
pingOkSum=`grep "ping is ok" ${logfile}|wc -l`
pingNotOKSum=`grep "ping is not ok" ${logfile}|wc -l`
telnetOkSum=`grep "telnet is ok" ${logfile}|wc -l`
telnetNotOKSum=`grep "telnet is not ok" ${logfile}|wc -l`
log "
	Ip        Sum : ${ipSum}
	Ping       OK : ${pingOkSum}
	Ping   not OK : ${pingNotOKSum}
    Telnet     OK : ${telnetOkSum}
    Telnet not OK : ${telnetNotOKSum}
" |tee -a ${logfile}
echo 0
exit 0
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值