验证和搜索IP地址

巴特克

我正在尝试做两件事:1.测试IP地址的有效性。2.在具有多个IP地址的文件中搜索IP地址。“ ips”是文件。到目前为止,这就是我所拥有的。每次我运行脚本时,都会得到以下信息:ip地址是否在文件中都无所谓,我总是会得到“未找到并保存IP地址”。请帮忙。谢谢高级

$ ./test1
 IP address: 202
 Not Valid. Please re-enter the ip address
 IP address: 202.124.64.0
 IP address NOT found, and saved
 IP address: 109.234..232.0
 Not Valid. Please re-enter the ip address
 IP address: 109.234.232.0
 IP address NOT found, and saved

剧本:

 #!/bin/bash

 while true; do

 echo -e "IP address: \c"
 read ip

 function valid_ip()
 {
   local stat=1

     if [[ $ip =~ [0-9]{1,3}\.[0-9{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 \
           && ${ip[3]} -le 255 ]]
         stat=$?
     fi
     return $stat
 }

 if valid_ip $ip; then
    if grep 'valid_ip $ip' ips; then
       echo "IP address found, and saved"
      else
       echo "IP address NOT found, and saved"
    fi
   else
    echo "Not Valid. Please re-enter the ip address"
 fi

 done
阿努巴瓦

保持这样的功能:

valid_ip() {
   local stat=1
   ip="$1"
   if [[ $ip =~ [0-9]{1,3}\.[0-9{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        a=($ip)
        IFS=$OIFS
        [[ ${a[0]} -le 255 && ${a[1]} -le 255 && ${a[2]} -le 255 && ${a[3]} -le 255 ]]
        stat=$?
   fi
   return $stat
}

然后将其称为:

 while true; do
 reap -p "Enter an IP address: " ip

 if valid_ip $ip; then
     echo "IP address found, and valid"
     break;
 else
    echo "Not Valid. Please re-enter the ip address"
 fi

 done

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章