Bash ping主机pc的Shell脚本赢得';行不通

Bash ping主机pc的Shell脚本赢得';行不通,bash,ping,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

我正试图在某个网络中为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地址的数量,有什么办法吗?

您需要向函数传递
$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
*"-"* )
    # 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[@]}

;;
附加问题:)

如何允许输入范围并对其进行扩展

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

将需要一个额外的case语句,并且需要在
[0-9]*)
上方插入该语句,因为我们不希望它与该语句匹配,而是只与其中的
-
参数匹配

新的案例看起来是这样的

#!/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
*"-"* )
    # 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[@]}

;;

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

pingTest "$1";;
有几件事:

  • 替换
    [0-9]*)pingTest带有
    [0-9]*)pingTest“$1”
    在没有参数的情况下不调用
    pingTest
  • ping
    ,按您的方式调用时,将永远运行。您需要使用ping-c4
    左右来发送有限数量的数据包
  • 在你使用它的任何地方引用
    $1
    都是一个非常好的主意,以防有人传递给你一个带有空格的参数。也就是说,将
    $1
    替换为
    “$1”
    。这是所有变量的良好实践

  • 我还试图给出ping地址的数量,有什么办法吗?@Gregory我已经更新了我的答案,告诉你怎么做:)谢谢,成功了!我只有最后一个问题。如果我键入。/pingtest.sh 41-46,它将ping 6个地址,最后一位数字是41到46?@Gregory您可能想使用case(开关)检查其中是否有
    “-”
    ,这样您就知道它是
    41-46
    参数。然后,您将计算该参数以获取两个数字,并使用类似于
    seq
    的方法获取要使用的数字数组。然后您可以执行
    设置--$@${array\u of_numbers[@]}
    ,这将把这些数字放在传入参数列表的末尾?那么代码是什么呢?我对数组不太在行:/我还试图给出有多少个ping地址处于打开状态,有什么办法吗?我还试图给出有多少个ping地址处于打开状态,有什么办法吗?
    ping
    如果主机是可访问的,则返回0(这对shell来说是真的),否则返回非零,因此,您正在寻找类似
    i=0;对于“$@”中的ip;如果ping-c4“192.168.129.$ip”;然后i=$((i+1));fi;完成;echo$i
    。用
    ping-c4“192.168.129.$ip”>/dev/null 2>&1关闭
    ping
    似乎是个明智的主意,我应该把它放在哪里?在我的until循环内,但在case外?这将是循环的替代品。如果要将其集成到循环中,重要的部分是介于
    If
    fi
    之间。我不需要until循环吗?