当前位置: 首页 > news >正文

shell_52.Linux测试与其他网络主机的连通性脚本

实战演练
本节将展示一个实用脚本,该脚本在处理用户输入的同时,使用 ping 命令或 ping6 命令来测试与其他网络主机的连通性。
ping 命令或 ping6 命令可以快速测试网络主机是否可用。这个命令很有用,经常作为首选工具。如果只是检查单个主机,那么直接使用该命令即可。
但是如果有数个甚至数百个主机需要检查,则 shell 脚本可以助你一臂之力。

这个脚本通过两种方法来选择要检查的主机:一是使用命令行选项,二是使用文件。下面是
该脚本在 Ubuntu 系统中使用命令行选项的用法演示:

$ ./CheckSystems.sh -t IPv4 192.168.1.102 192.168.1.104 
Checking system at 192.168.1.102... 
[...] 
--- 192.168.1.102 ping statistics --- 
3 packets transmitted, 3 received, 0% packet loss,[...] 
Checking system at 192.168.1.104... 
[...] 
--- 192.168.1.104 ping statistics --- 
3 packets transmitted, 0 received, +3 errors, 100% packet loss,[...] 
$


如果没有指定 IP 地址参数,则脚本会提示用户并退出:

$ ./CheckSystems.sh -t IPv4 
IP Address(es) parameters are missing. 
Exiting script... 
$$ cat CheckSystems.sh 
#!/bin/bash 
# Check systems on local network allowing for 
# a variety of input methods. 
# 
# 
########### Determine Input Method ################### 
# 
# Check for command-line options here using getopts. 
# If none, then go on to File Input Method 
# 
while getopts t: opt 
do case "$opt" in t) # Found the -t option if [ $OPTARG = "IPv4" ] then pingcommand=$(which ping) #elif [ $OPTARG = "IPv6" ] then pingcommand=$(which ping6) # else echo "Usage: -t IPv4 or -t IPv6" echo "Exiting script..." exit fi ;; *) echo "Usage: -t IPv4 or -t IPv6" echo "Exiting script..." exit;; esac # shift $[ $OPTIND - 1 ] # if [ $# -eq 0 ] then echo echo "IP Address(es) parameters are missing." echo echo "Exiting script..." exit fi # for ipaddress in "$@" do echo echo "Checking system at $ipaddress..." echo $pingcommand -q -c 3 $ipaddress echo done exit 
done 
# 
########### File Input Method ################### 
# 
echo 
echo "Please enter the file name with an absolute directory reference..." 
echo 
choice=0 
while [ $choice -eq 0 ] 
do read -t 60 -p "Enter name of file: " filename if [ -z $filename ] then quitanswer="" read -t 10 -n 1 -p "Quit script [Y/n]? " quitanswer #case $quitanswer in Y | y) echo echo "Quitting script..." exit;; N | n) echo echo "Please answer question: " choice=0;; *) echo echo "No response. Quitting script..." exit;; esac else choice=1 fi 
done 
# 
if [ -s $filename ] && [ -r $filename ] 
then echo "$filename is a file, is readable, and is not empty." echo cat $filename | while read line do ipaddress=$line read line iptype=$line if [ $iptype = "IPv4" ] then pingcommand=$(which ping) else pingcommand=$(which ping6) fi echo "Checking system at $ipaddress..." $pingcommand -q -c 3 $ipaddress echo done echo "Finished processing the file. All systems checked." 
else echo echo "$filename is either not a file, is empty, or is" echo "not readable by you. Exiting script..." 
fi 
# 
#################### Exit Script ##################### 
# 
exit 
$


 

http://www.lryc.cn/news/209966.html

相关文章:

  • OpenCV C++ 图像处理实战 ——《缺陷检测》
  • Python操作MySQL基础使用
  • 【pytorch】pytorch中的高级索引
  • 基于图像识别的自动驾驶汽车障碍物检测与避障算法研究
  • Spring boot定时任务
  • Glide原理
  • wps表格按分隔符拆分单元格
  • 【SEC 学习】Vim 的基本使用
  • Linux中shell脚本练习
  • AS/400简介
  • FreeRTOS 中断管理介绍和实操
  • 性能测试 —— Jmeter 常用三种定时器!
  • ROS自学笔记十七:Arbotix
  • Mac电脑窗口管理Magnet中文 for mac
  • Centos7 部署 Stable Diffusion
  • 【Python】一个句子中也许有多个连续空格,过滤掉多余的空格,只留下一个空格
  • 嵌入式项目电灯
  • [ubuntu系统下的文本编辑器nano,vim,gedit,文件使用,以及版本更新问题]
  • C#WinformListView实现缺陷图片浏览器
  • C- qsort()
  • 【Apache Flink】基于时间和窗口的算子-配置时间特性
  • 数组的优点和缺点
  • 接口返回响应,统一封装(ResponseBodyAdvice + Result)(SpringBoot)
  • 苹果cms模板MXone V10.7魔改版源码 全开源
  • ArcGIS笔记13_利用ArcGIS制作岸线与水深地形数据?建立水动力模型之前的数据收集与处理?
  • 一些k8s集群操作命令
  • pycharm运行R语言脚本(win10环境下安装)
  • Java进击框架:Spring-Test(六)
  • 微软:Octo Tempest是最危险的金融黑客组织之一
  • JS加密/解密之逻辑运算符加密进阶篇