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 
$
相关推荐
花落已飘10 分钟前
多线程 vs 异步
linux·网络·系统架构
PanZonghui35 分钟前
Centos项目部署之Nginx部署项目
linux·nginx
九丝城主1 小时前
2025使用VM虚拟机安装配置Macos苹果系统下Flutter开发环境保姆级教程--中篇
服务器·flutter·macos·vmware
码出钞能力1 小时前
linux内核模块的查看
linux·运维·服务器
星辰云-2 小时前
# Linux Centos系统硬盘分区扩容
linux·运维·centos·磁盘扩容
聽雨2372 小时前
02每日简报20250704
linux·科技·金融·生活·社交电子·娱乐·媒体
Maki Winster3 小时前
Peek-Ubuntu上Gif录制工具-24.04LTS可装
linux·ubuntu·peek
qq_171538853 小时前
TCP/IP协议精解:IP协议——互联网世界的邮政编码系统
网络·网络协议·tcp/ip
珹洺3 小时前
计算机网络:(七)网络层(上)网络层中重要的概念与网际协议 IP
网络·tcp/ip·计算机网络
小皮侠3 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github