用于ping主机pc的Shell脚本无法正常工作

时间:2015-05-28 09:14:58

标签: bash ping

我试图让脚本在某个网络中ping。但是如果我尝试使用例如./pingtest.sh 63 66,我会收到错误ping: unknown host 192.168.129.。 这是我的剧本:

#!/bin/bash

function pingTest(){    
    ping 192.168.129.$1

}

function helpText(){
    echo Please give in some numbers.
}
until [ -z $1 ]
do

case $1 in
    [0-9]*) pingTest;;
    [a-z]*) echo Error, please give in numbers;shift;;  
    -h ) helpText;;
    -help ) helpText;;
esac  
shift
done

有人可以帮助我做这项工作吗?

我还试图提供有多少ping地址,我有什么想法可以做到这一点?

3 个答案:

答案 0 :(得分:0)

您需要将$1传递给函数

case $1 in
    [0-9]*) pingTest "$1";;
    [a-z]*) echo Error, please give in numbers;shift;;

其他问题

如果指定数量的数据包未在特定时间限制内返回,

ping将返回错误。可以使用-c标志指定数据包的数量,使用-w标志指定超时数,这些数据包分别采用数量的数据包数和秒数。

如果您想要干净的输出,可以将命令输出传递给/dev/null

这些可以结合起来更新你的脚本看起来像这样...

#!/bin/bash

function pingTest(){
    # If after 5 seconds 4 packets haven't been received then return 1
    # else return 0
    # This also pipes to output from ping to /dev/null
    ping -c 4 -w 5 "192.168.129.$1" > /dev/null 2>&1 && return 0 || return 1
}

function helpText(){
    # Quoting is important.
    # It makes it nicer and safer.
    echo "Please give in some numbers."
}

alive_ips=0

until [ -z $1 ]
do
    case $1 in
        # You have all the space you want. Use it :) Space out them lines.
        [0-9]*)
            # If the function return 0 then increment the alive ips
            # If not then don't
            if pingTest "$1"
            then
                echo "$1 is alive"
                # Increment number of alive ip addresses
                (( alive_ips++ ))
            else
                echo "$1 is down"
            fi
        ;;
        [a-z]*)
            echo "Error, please give in numbers"
            # shift
            # No need for a shift here
        ;;
        "-help" | "-h" )
            #You can combine these flags
            helpText
            # Get the script to exit after you ask for help
            exit 0
        ;;
    esac  
    shift
done

echo ""
echo "The number of alive ips are: $alive_ips"

exit 0

其他附加问题:)

如何允许输入范围并扩展它们?

这个小程序变得有点复杂,但应该很难实现。

需要另外一个案例陈述,这需要插在[0-9]*)上方,因为我们不希望它与那个匹配,而只是匹配参数中的-

新案例看起来像这样......

*"-"* )
    # Take the input and replace all '-' characters with ' '
    # And put them into an array ( )
    range=( ${1//-/ } )

    # Check that there is only 2 items.
    if (( ${#range[@]} != 2 ))
    then
        echo "Too many arguments in $1"
        # Shift the arguments to the next one
        shift
        # Move onto next argument and stop executing any more
        # code in this case
        continue
    fi

    # Use the seq command to produce a list at integers between the two
    # ip addresses and put them into an array
    ip_range=( $(seq ${range[@]}) )

    # Set all the $1, $2, $3 ... to What they are now ($@) followed by the
    # expanded range (${ip_range[@]})
    set -- $@ ${ip_range[@]}

;;

答案 1 :(得分:0)

函数有自己的参数列表,取自传递给它们的参数。,它们看不到主脚本的参数列表。由于您没有将值传递给pingTest,因此它将作为空字符串进行评估。使用

调用ping测试
pingTest "$1";;

答案 2 :(得分:0)

有几件事:

  1. [0-9]*) pingTest;;替换为[0-9]*) pingTest "$1";;,不要在没有参数的情况下调用pingTest
  2. ping,当你按照你的方式打电话时,会永远奔跑。您需要使用ping -c 4左右发送有限数量的数据包。
  3. 在你使用它的任何地方引用$1是一个非常好的主意,以防有人通过你带有空格的参数。也就是说,将$1替换为"$1"。这是所有变量的良好实践。