一个shell写的ping函数

发布时间:2022-04-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了一个shell写的ping函数脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
复制代码 代码如下:

#!/bin/bash
#2013-01-06 14:00:00 wanggy exp
#note:ping monitor
set -u
#set -x
ping_fun()
{
    d_network=192.168.1
    echo -n "input the network(default $d_network):"
    read network
    : ${network:=$d_network}
    echo "network:$network"
    d_hostip_beg=1
    d_hostip_end=254
    echo -n "input the hostip(default $d_hostip_beg $d_hostip_end):"
    read hostip_beg hostip_end
    : ${hostip_beg:=$d_hostip_beg}
    : ${hostip_end:=$d_hostip_end}
    echo "hostip_beg:$hostip_beg"
    echo "hostip_end:$hostip_end"
    count=3
    for ((hostip=$hostip_beg;hostip<=$hostip_end;hostip++));do
            host=$network.$hostip
            echo "开始ping检测$host"
            ping -c $count $host &>/dev/null
                    if [ $? = 0 ];then
                            echo "$host is up"
                    else
                            sleep 3
                            ping -c $count $host &>/dev/null
                            if [ $? = 0 ];then
                                    echo "$host is up"
                            else
                                    echo "$host is down"
                            fi
                    fi
            done
    #echo "执行完毕"
    exit 0
    }
    main()
    {
    echo "----开始执行ping程序----"
    ping_fun
    }
    main
    exit 0

脚本宝典总结

以上是脚本宝典为你收集整理的一个shell写的ping函数全部内容,希望文章能够帮你解决一个shell写的ping函数所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: