转至: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

Shell脚本实现网络连通性检查Ping+Telnet的更多相关文章

  1. shell脚本检测网络是否畅通

    shell初始化安装脚本执行时,需从网络上安装一些rpm包,所有需要先检测网络的畅通性, 代码 #检测网络链接&&ftp上传数据 function networkAndFtp() { ...

  2. shell脚本 阿里云基线检查一键配置

    一.简介 源码地址 日期:2017/9/1 介绍:安全加固脚本,会符合阿里云基线检查.有幂等性,可重复执行 效果图: 二.使用 适用:centos6/7 语言:中文 注意:脚本是符合阿里云基线检查的配 ...

  3. Shell脚本 | 安卓应用权限检查

    现在 Google Play 对应用权限的管理非常严格,之前公司内有个版本就是因为新增了四个权限导致停灰处理.所以,在每个版本发布之前很有必要检查一下是否有新增权限. 安卓应用请求的所有权限可以通过 ...

  4. shell脚本查看网络配置

    #!/bin/bash ifconfig|grep -E 'eth|inet'|grep -Ev '(inet6|127.0.0.1)'|sed 's/ /\n/g'|awk NF|grep -Ev ...

  5. Linux shell脚本判断服务器网络是否可以上网

    Linux shell脚本判断网络畅通 介绍 在编写shell脚本时,有的功能需要确保服务器网络是可以上网才可以往下执行,那么此时就需要有个函数来判断服务器网络状态 我们可以通过curl来访问 www ...

  6. Shell学习---Shell脚本的静态检查工具shellcheck

    Shell脚本的静态检查工具shellcheck ubuntu下 apt install shellcheck ,即可安装shellcheck.写完shell脚本,记得用它检查一下,能给你点建议的.要 ...

  7. 常用shell脚本

    [脚本1]打印形状打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please input the length: " n ...

  8. shell脚本编程——生产实战案例

    生产实战案例     在日常的生产环境中,可能会遇到需要批量检查内网目前在线的主机IP地址有哪些,还可能需要检查这些在线的主机哪些端口是开放状态,因此依靠手工来检查是可以实现,但比较费时费力,所以需要 ...

  9. 【转】干货分享-100个shell脚本

    本文用于记录学习和日常中使用过的shell脚本 [脚本1]打印形状 打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please i ...

随机推荐

  1. C# 同步 异步 回调 状态机 async await Demo

    源码 https://gitee.com/s0611163/AsyncAwaitDemo 为什么会研究这个? 我们项目的客户端和服务端通信用的是WCF,我就想,能不能用异步的方式调用WCF服务呢?或者 ...

  2. ansible roles实践——安装java

    [root@master] /etc/ansible$ cat roles/java/tasks/main.yml ---- name: unzip jdk unarchive: src=jdk-8u ...

  3. C++数据结构类型以及实现类

    1.array数组 #include<iostream> #include<array> using namespace std; int main() { array< ...

  4. markdown介绍和使用(超全建议收藏)

    Markdown介绍 Markdown 其实在 2004 年就有了,不过之前一直很小众,这几年随着相关应用平台的发展,Markdown以其独到的优势迅速火起来了.Markdown编辑器使用一套格式标记 ...

  5. PyTorch 1.4 中文文档校对活动正式启动 | ApacheCN

    一如既往,PyTorch 1.4 中文文档校对活动启动了! 认领须知 请您勇敢地去翻译和改进翻译.虽然我们追求卓越,但我们并不要求您做到十全十美,因此请不要担心因为翻译上犯错--在大部分情况下,我们的 ...

  6. Android 12(S) 图形显示系统 - createSurface的流程(五)

    题外话 刚刚开始着笔写作这篇文章时,正好看电视在采访一位92岁的考古学家,在他的日记中有这样一句话,写在这里与君共勉"不要等待幸运的降临,要去努力的掌握知识".如此朴实的一句话,此 ...

  7. 封装jar问题java.lang.SecurityException: Invalid signature file digest for Manifest main attributes以及maven依赖重提解决

    1.jar包封装完成后,其他项目引用jar,启动时报错java.lang.SecurityException: Invalid signature file digest for Manifest m ...

  8. js获取高度

    转载请注明来源:https://www.cnblogs.com/hookjc/ <script> function getInfo() { var s = ""; s ...

  9. Python重载比较运算符

    对象包含的内置方法 class MyObj(object): def __init__(self): self.value = 0 myObj = MyObj() print(dir(myObj)) ...

  10. 一行代码为UITextField添加收键盘功能

    iOS开发中收键盘是十分常用的功能,只需一行代码即可为ViewController添加工具条收键盘功能 更重要的是使用catogory,无代码污染. 代码 // UITextField+keyboar ...