Shell脚本实现网络连通性检查Ping+Telnet
程序员文章站
2022-01-15 12:01:04
...
#!/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
推荐阅读
-
实现MySQL定时批量检查表repair和优化表optimize table的shell脚本
-
Shell脚本实现批量下载网络图片代码分享
-
Shell脚本实现检查服务器安全状态(用户、登录IP、防火墙检查)
-
Shell脚本实现检测某ip网络畅通情况(含短信报警功能)
-
实现MySQL定时批量检查表repair和优化表optimize table的shell脚本
-
shell脚本实现公司项目部署交付环境预检查的思路
-
Shell脚本实现批量下载网络图片代码分享
-
Shell脚本实现检查服务器安全状态(用户、登录IP、防火墙检查)
-
Shell脚本实现检测某ip网络畅通情况(含短信报警功能)
-
shell脚本实现公司项目部署交付环境预检查的思路