用于ping主机PC的外壳脚本不起作用



我正在尝试制作一个在特定网络中执行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到的地址,有什么想法吗?

函数有自己的参数列表,取自传递给它们的参数。,他们看不到主脚本的参数列表。由于您没有将值传递到pingTest,因此它的计算结果为空字符串。的呼叫ping测试

pingTest "$1";;

几件事:

  1. [0-9]*) pingTest;;替换为[0-9]*) pingTest "$1";;以不调用无参数的pingTest
  2. ping,当以您的方式调用时,将永远运行。您将希望使用ping -c 4左右来发送数量有限的数据包
  3. 在你使用$1的任何地方引用它都是一个非常好的主意,以防有人传递给你一个带有空格的论点。也就是说,用"$1"替换$1。这对于所有变量来说都是一个很好的实践

您需要将$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

其他问题:(

如何允许输入范围并扩展范围

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

将需要一个额外的case语句,并且需要将其插入[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[@]}
;;