bash 脚本中 ping 多个 IP 地址时出错

发布于 2025-01-20 17:38:34 字数 1502 浏览 2 评论 0原文

 Welcome to the IP Check Program!! Please input the ip address: 300.500
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 IP address is alive
 Thank you for using the IP Check Program. \nPress 0 to exit and any other number to 
  continue

有人可以帮我吗?这是显示的错误。我猜为什么该程序被搞砸了。因为“ 300.500”显然不是一个有效的IP地址,但它读取为活着。请帮忙!我的代码在下面

#!/bin/bash
rs=1
while [ $rs -gt 0 ]
do
    clear
#greet the user and request input
    echo -n "Welcome to the IP Check Program!! Please input the ip address: "
read ip
#check user's input is IP address format
isIP="expr match $io'[0-9]\+\.[]0-9\+\.[0-9]\+\.[0-9]\+";
isIP=$?
if [ $isIP -eq 0 ];
then
    isValid=1

    for i in 1 2 3 4
    do
            n='echo $ip | cut -d. -f$i'
            if [ "$n" -gt 255 -o $n -lt 0 ];
            then
                    echo "INPUT ERROR. IP ADDRESS DIGITS SHOULD BE BETWEEN 0 AND 255 
ONLY"
                    isValid=0
            fi
    done
    if [ "$isValid" -eq 1 ];
    then
            echo "IP address is alive"
    fi
  else
    echo "IP address is not responding"
fi
#greet user after using program
echo -n "Thank you for using the IP Check Program. Press 0 to exit and any other number 
to continue"
read rs
 Welcome to the IP Check Program!! Please input the ip address: 300.500
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 IP address is alive
 Thank you for using the IP Check Program. \nPress 0 to exit and any other number to 
  continue

could someone please help me out? this is the error displayed. i guess why the program is messed up. because "300.500" is obviously not a valid ip address but it reads it as alive. PLEASE HELP! My code is below

#!/bin/bash
rs=1
while [ $rs -gt 0 ]
do
    clear
#greet the user and request input
    echo -n "Welcome to the IP Check Program!! Please input the ip address: "
read ip
#check user's input is IP address format
isIP="expr match $io'[0-9]\+\.[]0-9\+\.[0-9]\+\.[0-9]\+";
isIP=$?
if [ $isIP -eq 0 ];
then
    isValid=1

    for i in 1 2 3 4
    do
            n='echo $ip | cut -d. -f$i'
            if [ "$n" -gt 255 -o $n -lt 0 ];
            then
                    echo "INPUT ERROR. IP ADDRESS DIGITS SHOULD BE BETWEEN 0 AND 255 
ONLY"
                    isValid=0
            fi
    done
    if [ "$isValid" -eq 1 ];
    then
            echo "IP address is alive"
    fi
  else
    echo "IP address is not responding"
fi
#greet user after using program
echo -n "Thank you for using the IP Check Program. Press 0 to exit and any other number 
to continue"
read rs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春庭雪 2025-01-27 17:38:34

@isioma-udobi,您需要改进您的脚本。它有几个基本问​​题必须先解决,然后才能使其发挥作用。如果你阅读了戈登告诉你的内容,应该会有很大帮助。我重新编写了您的代码,进行了一些更改并向其添加了一些教学评论。看一下,看看您的脚本是否可以取得进展:

#!/bin/bash
# What is the point of this rs variable?? Give a better name to it
rs=1
while [ $rs -gt 0 ]

# where is the corresponding "done"???
do

    clear

    # greet the user and request input
    echo -n "Welcome to the IP Check Program!!"\
    "Please input the ip address: "
    read ip

    # check user's input is IP address format
    # Rewrite the following 2 lines as Gordon suggested
    #isIP="expr match $io'[0-9]\+\.[]0-9\+\.[0-9]\+\.[0-9]\+";
    #isIP=$?
    
    # This line will also change with the last 2
    if [ $isIP -eq 0 ];
    then
    isValid=1

    # Test if each part of the IPv4 is valid
    for i in 1 2 3 4
    do
        # In this line, you are simply creating a string
        # which looks like the command 'echo' wrinting
        # into the standard input of a 'cut'.
        n='echo $ip | cut -d. -f$i'

        # Check if the IPv4 part is in the valid
        # interval
        if [ "$n" -gt 255 -o $n -lt 0 ];
        then
        echo "INPUT ERROR. IP ADDRESS DIGITS"\
            "SHOULD BE BETWEEN 0 AND 255 ONLY"
        # Once a part is invalid, the others do
        # not need to be tested. You can test
        # them, though. This code do not break
        # the result
        isValid=0
        fi
    done

    # Is the whole IP is valid?
    if [ "$isValid" -eq 1 ];
    then
        echo "IP address is alive"
    fi
    else
    # echo "IP address is not ??responding??"
    echo "The given IPv4 address is not valid"
    fi
    # greet user after using program
    echo -n "Thank you for using the IP Check Program."\
    "Press 0 to exit and any other number to continue"
    read rs

@isioma-udobi , you need to improve your script. It has several basic problems that must be solved before you can make it work. What Gordon told you should help a lot, if you read what is pointed. And I reformated your code, made a few changes and added some didactic comments to it. Take a look, and see if you can progress with your script:

#!/bin/bash
# What is the point of this rs variable?? Give a better name to it
rs=1
while [ $rs -gt 0 ]

# where is the corresponding "done"???
do

    clear

    # greet the user and request input
    echo -n "Welcome to the IP Check Program!!"\
    "Please input the ip address: "
    read ip

    # check user's input is IP address format
    # Rewrite the following 2 lines as Gordon suggested
    #isIP="expr match $io'[0-9]\+\.[]0-9\+\.[0-9]\+\.[0-9]\+";
    #isIP=$?
    
    # This line will also change with the last 2
    if [ $isIP -eq 0 ];
    then
    isValid=1

    # Test if each part of the IPv4 is valid
    for i in 1 2 3 4
    do
        # In this line, you are simply creating a string
        # which looks like the command 'echo' wrinting
        # into the standard input of a 'cut'.
        n='echo $ip | cut -d. -f$i'

        # Check if the IPv4 part is in the valid
        # interval
        if [ "$n" -gt 255 -o $n -lt 0 ];
        then
        echo "INPUT ERROR. IP ADDRESS DIGITS"\
            "SHOULD BE BETWEEN 0 AND 255 ONLY"
        # Once a part is invalid, the others do
        # not need to be tested. You can test
        # them, though. This code do not break
        # the result
        isValid=0
        fi
    done

    # Is the whole IP is valid?
    if [ "$isValid" -eq 1 ];
    then
        echo "IP address is alive"
    fi
    else
    # echo "IP address is not ??responding??"
    echo "The given IPv4 address is not valid"
    fi
    # greet user after using program
    echo -n "Thank you for using the IP Check Program."\
    "Press 0 to exit and any other number to continue"
    read rs
转身以后 2025-01-27 17:38:34

我会为此使用nmap而不是编写自定义Shell脚本。您可以这样扫描IP的范围:

nmap -sn 123.123.123.1-255

或给它在文件中提供一个主机列表:

nmap -iL input_file_name

人页

I would use nmap for this instead of writing a custom shell script. You can scan ranges of IP's like this:

nmap -sn 123.123.123.1-255

or give it a list of hosts in a file:

nmap -iL input_file_name

more info in the man page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文