转至: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. ServletContext类介绍(对象的作用及演示、像map一样存取数据)

    什么是ServletContext类 1,什么是 ServletContext 1)ServletContext 是一个接口,它表示Servlet上下文对象 2)一个web工程,只有一个Servlet ...

  2. JDBC固定步骤-java连接MySQL

    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = ...

  3. java多线程中同步的问题?

    一.通过模拟网络延迟,解决同步的问题. package com.zxf.demo; public class G01 implements Runnable{ private int num=10; ...

  4. Codeforces Round #740 Div. 2

    题目跳转链接 A. Simply Strange Sort 题意 定义一个函数\(f_{i}\) : 如果\(a_i \ge a_{i+1}\) swap(\(a_i\) \(a_{i+1}\)) 定 ...

  5. 【ASP.NET Core】设置Web API 响应的数据格式——Produces 特性篇

    开春首文,今天老周就跟各位大伙伴们聊一个很简单的话题:怎么设定API响应的数据格式. 说本质一点,就是设置所返回内容的 MIME 类型(Content-Type 头).当然了,咱们不会使用在HTTP管 ...

  6. python编写购物车

    上次的学习又没有坚持下来,工作忙的不可开交,但我反思了一下还是自己没有下定决心好好学习,所以这次为期3个月的学习计划开始了,下面是这次学习后重新编写的购物车初版代码. 1 # 功能要求: 2 # 要求 ...

  7. 分布式系统及CAP理论

    一.集中式系统 在学习分布式之前,先了解一下与之相对应的集中式系统是什么样的. 集中式系统用一句话概括就是:一个主机带多个终端.终端没有数据处理能力,仅负责数据的录入和输出.而运算.存储等全部在主机上 ...

  8. markdown常见问题

    图片的引用问题: ![image](./images/git命令.png) 为啥不显示图片?????? 解答:图片路径不支持中文 斜体跟加粗 *强调* 或者 _强调_ (示例:斜体) **加重强调** ...

  9. shell——mkfifo管道

    转自:http://blog.sina.com.cn/s/blog_605f5b4f0101azuc.html 创建命名管道的方法为:mkfifo pipe_name. 这样就能创建一个命名的管道pi ...

  10. Linux组管理

    首先查看文件所有者.文件所在组等信息:ls -l 1.改变文件所有者:chown 用户名 文件名 执行 chown xm Hello.java 后,可以看到文件所有者现在是属于xm这个用户的了 2.改 ...