Check for IP validity

后端 未结 13 1344
庸人自扰
庸人自扰 2020-12-05 07:13

How do I check the validity of an IP address in a shell script, that is within the range 0.0.0.0 to 255.255.255.255?

相关标签:
13条回答
  • 2020-12-05 07:53

    Perl has a great module Regexp::Common for validating various things:

    perl -MRegexp::Common=net -e 'exit(shift() !~ /^$RE{net}{IPv4}$/)' $ipaddr
    

    You may need to sudo cpan install Regexp::Common first

    I'd wrap it in a function:

    valid_ip() {
      perl -MRegexp::Common=net -e 'exit(shift() !~ /^$RE{net}{IPv4}$/)' "$1"
    }
    
    if valid_ip 123.234.345.456; then
      echo OK
    else
      echo INVALID
    fi
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题