bash 读取 IP 地址并检查是否有效/范围之间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14751293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:30:27  来源:igfitidea点击:

read IP address and check if valid/between range

bashloopsif-statementip-address

提问by Zippyduda

My current script:

我目前的脚本:

#!/usr/local/bin/bash
echo -n "Enter VPS IP address:"
read userinput
lookupip="vps $userinput"

if [[ $userinput -lt 80.* || $userinput -gt 255.* ]] #checks input is in the range
 then
   echo "Input outside acceptable range."
 else

#The grep removes all from VPS tool output except primary IP address

$lookupip | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | sed '1 ! d' | xargs ping -oc 1000 -Q

fi

The lowest IP address range is in the 80.X.X.X range, I've tried using:

最低的 IP 地址范围在 80.XXX 范围内,我尝试使用:

8*
80*
80...*

8*
80*
80 .. .*

But it always errors with:

但它总是错误:

line 10: [[: 80.X.X.X: syntax error in expression (error token is ".X.X.X")

What would be the best way to define a range of IP address less than (lt) and gt (greater than)?

定义小于 (lt) 和 gt (大于) 的 IP 地址范围的最佳方法是什么?

回答by Ajit

if you just want to check if the IP address is valid or not, you can use ipcalc command in bash to check this.

如果您只想检查 IP 地址是否有效,您可以在 bash 中使用 ipcalc 命令进行检查。

ipcalc -c $userinput
example
ipcalc -c 10.20.30.401
ipcalc: bad IPv4 address: 10.20.30.401

回答by KBart

Probably not the best solution, but as a quick fix for your script should do:

可能不是最好的解决方案,但作为脚本的快速修复应该这样做:

#!/usr/local/bin/bash
echo -n "Enter VPS IP address:"
read userinput
lookupip="vps $userinput"
first_octet=`echo "$userinput" | cut -d'.' -f1`

if [[ $first_octet -lt 80 || $first_octet -gt 255 ]]
 then
   echo "Input outside acceptable range."
 else

#The grep removes all from VPS tool output except primary IP address

$lookupip | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | sed '1 ! d' | xargs ping -oc 1000 -Q

fi

EDITED: a better solutionwould be to take all three IP addresses (the one under inspection, lowest and highest) as parameters, convert them to 32bit number (that's what inet_aton()function does) and check ranges:

编辑:更好的解决方案是将所有三个 IP 地址(正在检查的 IP 地址,最低和最高)作为参数,将它们转换为 32 位数字(这就是inet_aton()函数所做的)并检查范围:

#!/usr/local/bin/bash

inet_aton ()
{
    local IFS=. ipaddr ip32 i
    ipaddr=()
    for i in 3 2 1 0
    do
        (( ip32 += ipaddr[3-i] * (256 ** i) ))
    done

    return $ip32
}

echo -n "Enter VPS IP address, min IP address, max IP address:"
read userinput

ip1=`echo "$userinput" | cut -d' ' -f1`
ip2=`echo "$userinput" | cut -d' ' -f2`
ip3=`echo "$userinput" | cut -d' ' -f3`

lookupip="vps $ip1"

ip=`inet_aton $ip1`
min=`inet_aton $ip2`
max=`inet_aton $ip3`

if [[ $ip -lt $min || $ip -gt $max ]]
 then
   echo "Input outside acceptable range."
 else

#The grep removes all from VPS tool output except primary IP address

$lookupip | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | sed '1 ! d' | xargs ping -oc 1000 -Q

fi

The only difference would be that you have to enter 3 IP addresses, not one as before. Of course, the lowest and highest IP addresses could be hard-coded or taken from elsewhere, but I leave that, along with parameter validation and error checking, up to you.

唯一的区别是您必须输入 3 个 IP 地址,而不是像以前那样输入一个。当然,最低和最高 IP 地址可以硬编码或从其他地方获取,但我将其与参数验证和错误检查一起留给您。