Shell脚本实现网络连通性检查Ping+Telnet
转至: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的更多相关文章
- shell脚本检测网络是否畅通
shell初始化安装脚本执行时,需从网络上安装一些rpm包,所有需要先检测网络的畅通性, 代码 #检测网络链接&&ftp上传数据 function networkAndFtp() { ...
- shell脚本 阿里云基线检查一键配置
一.简介 源码地址 日期:2017/9/1 介绍:安全加固脚本,会符合阿里云基线检查.有幂等性,可重复执行 效果图: 二.使用 适用:centos6/7 语言:中文 注意:脚本是符合阿里云基线检查的配 ...
- Shell脚本 | 安卓应用权限检查
现在 Google Play 对应用权限的管理非常严格,之前公司内有个版本就是因为新增了四个权限导致停灰处理.所以,在每个版本发布之前很有必要检查一下是否有新增权限. 安卓应用请求的所有权限可以通过 ...
- shell脚本查看网络配置
#!/bin/bash ifconfig|grep -E 'eth|inet'|grep -Ev '(inet6|127.0.0.1)'|sed 's/ /\n/g'|awk NF|grep -Ev ...
- Linux shell脚本判断服务器网络是否可以上网
Linux shell脚本判断网络畅通 介绍 在编写shell脚本时,有的功能需要确保服务器网络是可以上网才可以往下执行,那么此时就需要有个函数来判断服务器网络状态 我们可以通过curl来访问 www ...
- Shell学习---Shell脚本的静态检查工具shellcheck
Shell脚本的静态检查工具shellcheck ubuntu下 apt install shellcheck ,即可安装shellcheck.写完shell脚本,记得用它检查一下,能给你点建议的.要 ...
- 常用shell脚本
[脚本1]打印形状打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please input the length: " n ...
- shell脚本编程——生产实战案例
生产实战案例 在日常的生产环境中,可能会遇到需要批量检查内网目前在线的主机IP地址有哪些,还可能需要检查这些在线的主机哪些端口是开放状态,因此依靠手工来检查是可以实现,但比较费时费力,所以需要 ...
- 【转】干货分享-100个shell脚本
本文用于记录学习和日常中使用过的shell脚本 [脚本1]打印形状 打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please i ...
随机推荐
- Python解释器下载安装教程
简介: 自从20世纪90年代初Python语言诞生至今,它已被逐渐广泛应用于系统管理任务的处理和Web编程.2021年10月,语言流行指数的编译器Tiobe将Python加冕为最受欢迎的编程语言,20 ...
- JVM调优方法
目 录 目 录 I 诠释JVM调优 1 第1章 JVM内存模型及垃圾收集算法 1 1.1 根据Java虚拟机规范,JVM将内存划分为 1 1.2 垃圾回收算法 1 第2章 内存泄漏及解决方法 2 2. ...
- Hadoop支持LZO
0. 环境准备 maven(下载安装,配置环境变量,修改sitting.xml加阿里云镜像)gcc-c++zlib-develautoconfautomakelibtool通过yum安装即可 yum ...
- R-B Tree
1.简介 R-B Tree,全称Red-Black Tree,又称为"红黑树",为一种自平衡二叉查找树(特殊的平衡二叉树,都是在插入和删除操作时通过特定操作保持二叉树的平衡,从而获 ...
- 微服务架构 | 11.1 整合 Seata AT 模式实现分布式事务
目录 前言 1. Seata 基础知识 1.1 Seata 的 AT 模式 1.2 Seata AT 模式的工作流程 1.3 Seata 服务端的存储模式 1.4 Seata 与 Spring Clo ...
- 男孩和女孩(二)-->相识
转载请注明来源:https://www.cnblogs.com/hookjc/ 那天是男孩的十九岁生日:男孩还是像平常一样,一大早就起来了(快七点了).一切都是那么的平常,直到第一节课下课,男孩的同窗 ...
- 微擎site.php函数以及路由连接
任务1: 微擎模块设计: module.php 规则类: 会调用module.php manifest.xml中业务菜单对应的模块 如果在site.php中没有相应的函数 比如 /web/index. ...
- IOS开发之----常用函数和常数--秀清
介绍一下Objective-c常用的函数,常数变量 算术函数 [算术函数] 函数名 说明 int rand() 随机数生成.(例)srand(time(nil)); //随机数初期化int val = ...
- 解决"Uncaught (in promise) Error: Navigation cancelled from "/" to "/login" with a new navigation"报错处理
Uncaught (in promise) Error: Navigation cancelled from "/" to "/login" with a ne ...
- Windows office2019免费激活,附代码
office2019地址:链接:https://pan.baidu.com/s/1zPt5U7b0L-bGHl5AOtYs2w提取码:m5ei 新建一个txt,然后把这段代码放进去,然后保存关闭改后缀 ...