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


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

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

    1. $ ./CheckSystems.sh -t IPv4 192.168.1.102 192.168.1.104 
    2. Checking system at 192.168.1.102... 
    3. [...] 
    4. --- 192.168.1.102 ping statistics --- 
    5. 3 packets transmitted, 3 received, 0% packet loss,[...] 
    6. Checking system at 192.168.1.104... 
    7. [...] 
    8. --- 192.168.1.104 ping statistics --- 
    9. 3 packets transmitted, 0 received, +3 errors, 100% packet loss,[...] 
    10. $


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

    1. $ ./CheckSystems.sh -t IPv4 
    2. IP Address(es) parameters are missing. 
    3. Exiting script... 
    4. $
    5. $ cat CheckSystems.sh 
    6. #!/bin/bash 
    7. # Check systems on local network allowing for 
    8. # a variety of input methods. 
    9. ########### Determine Input Method ################### 
    10. # Check for command-line options here using getopts. 
    11. # If none, then go on to File Input Method 
    12. while getopts t: opt 
    13. do 
    14.     case "$opt" in 
    15.         t) # Found the -t option 
    16.         if [ $OPTARG = "IPv4"
    17.         then 
    18.             pingcommand=$(which ping) 
    19.  #
    20.         elif [ $OPTARG = "IPv6"
    21.         then 
    22.             pingcommand=$(which ping6) 
    23.  
    24.         else 
    25.             echo "Usage: -t IPv4 or -t IPv6" 
    26.             echo "Exiting script..." 
    27.             exit 
    28.         fi 
    29.         ;; 
    30.         *) echo "Usage: -t IPv4 or -t IPv6" 
    31.             echo "Exiting script..." 
    32.         exit;; 
    33.         esac 
    34.  
    35.     shift $[ $OPTIND - 1 ] 
    36.  
    37.     if [ $# -eq 0 ] 
    38.     then 
    39.         echo 
    40.         echo "IP Address(es) parameters are missing." 
    41.         echo 
    42.         echo "Exiting script..." 
    43.         exit 
    44.     fi 
    45.  
    46.     for ipaddress in "$@" 
    47.     do 
    48.         echo 
    49.         echo "Checking system at $ipaddress..." 
    50.         echo 
    51.         $pingcommand -q -c 3 $ipaddress 
    52.         echo 
    53.     done 
    54.     exit 
    55. done 
    56. ########### File Input Method ################### 
    57. echo 
    58. echo "Please enter the file name with an absolute directory reference..." 
    59. echo 
    60. choice=0 
    61. while [ $choice -eq 0 ] 
    62. do 
    63.     read -t 60 -p "Enter name of file: " filename 
    64.     if [ -z $filename
    65.     then 
    66.         quitanswer="" 
    67.         read -t 10 -n 1 -p "Quit script [Y/n]? " quitanswer 
    68.  #
    69.         case $quitanswer in 
    70.             Y | y) echo 
    71.                 echo "Quitting script..." 
    72.                 exit;; 
    73.             N | n) echo 
    74.                 echo "Please answer question: " 
    75.                 choice=0;; 
    76.             *) echo 
    77.                 echo "No response. Quitting script..." 
    78.                 exit;; 
    79.         esac 
    80.         else 
    81.         choice=1 
    82.     fi 
    83. done 
    84. if [ -s $filename ] && [ -r $filename
    85. then 
    86.     echo "$filename is a file, is readable, and is not empty." 
    87.     echo 
    88.     cat $filename | while read line 
    89.     do 
    90.         ipaddress=$line 
    91.         read line 
    92.         iptype=$line 
    93.         if [ $iptype = "IPv4"
    94.         then 
    95.             pingcommand=$(which ping) 
    96.         else 
    97.             pingcommand=$(which ping6) 
    98.         fi 
    99.         echo "Checking system at $ipaddress..." 
    100.         $pingcommand -q -c 3 $ipaddress 
    101.         echo 
    102.     done 
    103.     echo "Finished processing the file. All systems checked." 
    104. else 
    105.     echo 
    106.     echo "$filename is either not a file, is empty, or is" 
    107.     echo "not readable by you. Exiting script..." 
    108. fi 
    109. #################### Exit Script ##################### 
    110. exit 
    111. $


     

  • 相关阅读:
    ElasticSearch
    ATF启动(六):bl32(OP-TEE)-->bl33 ATF ending
    ARM开发(二)ARM体系结构——ARM,数据和指令类型,处理器工作模式,寄存器,状态寄存器,流水线,指令集,汇编小练习题
    接口自动化测试框架搭建【附详细搭建视频】
    Nestjs配置服务,配置Cookie和Session
    ETCD数据库源码分析——集群通信初始化
    React基础-JSX动态绑定属性的方式
    08 相合估计
    transformer学习笔记
    MAC M2芯片执行yolov8 + deepsort 实现目标跟踪
  • 原文地址:https://blog.csdn.net/mmmmm168m/article/details/134068358