【问题标题】:read IP address and check if valid/between range读取 IP 地址并检查是否有效/范围之间
【发布时间】:2013-02-07 12:36:36
【问题描述】:

我当前的脚本:

#!/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

最低 IP 地址范围在 80.X.X.X 范围内,我试过使用:

8*
80*
80...*

但它总是出错:

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

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

【问题讨论】:

  • 不要在变量中隐藏你正在运行的命令。它使您的代码可读性降低,并且不会像您认为的那样经常工作。只需输入vps $userinput | grep ...
  • 好的,这是有道理的,但是我如何在 if 循环中巧妙地调用它?

标签: bash loops if-statement ip-address


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    #!/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
    

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

    #!/usr/local/bin/bash
    
    inet_aton ()
    {
        local IFS=. ipaddr ip32 i
        ipaddr=($1)
        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
    

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

    【讨论】:

    • 那行得通,谢谢 :) 理想情况下,它会查看整个 IP 地址并检查它是否在我拥有的范围列表中。有 10 个独特的范围,仅在第 3 和第 4 个八位字节变化。
    • 使用IFS="." read -a octets <<< $userinput。然后分别检查${octets[0]}${octets[1]}等中的每一个。
    猜你喜欢
    • 2013-08-22
    • 1970-01-01
    • 2017-05-21
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    相关资源
    最近更新 更多