转至:https://blog.csdn.net/AngelLBS/article/details/89382136

#!/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

————————————————
原文链接:https://blog.csdn.net/AngelLBS/article/details/89382136

最新文章

  1. asp.net(C#)网站发布后 Global.asax 里 Application_Error 不执行的问题
  2. UIButton 的点击事件详解
  3. Eclipse SVN冲突解决
  4. Learn Vim
  5. Linux理念
  6. Android录音服务
  7. mysql 常用语法
  8. C++-----lambda使用
  9. NSTimer、CADisplayLink 内存泄漏
  10. Unity的Write Defaults-&gt;从一个例子谈起
  11. mysql的ACID的理解
  12. 在aliyun遇到一些问题
  13. [luogu 3957]跳房子
  14. Java中的默认构造函数
  15. bzoj2095-Bridge
  16. array_diff 不注意的坑
  17. Java垃圾回收机制概述
  18. html框架集 target
  19. cocos2d-x ios游戏开发初认识(六) 渲染的优化
  20. CodeForces 455C Civilization(并查集+树直径)

热门文章

  1. IoC容器-Bean管理XML方式(p名称空间注入)
  2. 查看Linux系统信息
  3. 从新建文件夹开始构建ShadowPlay Engine游戏引擎(6)
  4. Basler相机启动问题xml读取出错
  5. tcp 中 FLAGS字段,几个标识:SYN, FIN, ACK, PSH, RST, URG.
  6. JDK8 的 Lambda、Stream、LocalDate
  7. 前端开发Grunt工具的安装使用
  8. k8s实战之部署Prometheus+Grafana可视化监控告警平台
  9. Shell for&amp;while中的循环
  10. 基于Apache的Tomcat负载均衡和集群(2)