shell脚本+短语法验证有效的IP [英] shell script + short syntax to verify valid IP
问题描述
我的问题是,我想获得一些短期和奇思妙想来验证IP地址,然后我的例子
my question is , I want to get some short and smart ideas to verify the IP address then my example
也许一些Perl语法,我可以在我的ksh脚本结合
Maybe some perl syntax that I can combine in my ksh script
利迪娅
推荐答案
不要重新发明轮子。
use strict;
use warnings;
use Regexp::Common qw/net/;
# see http://search.cpan.org/dist/Regexp-Common/lib/Regexp/Common/net.pm
my $Address = '...';
# adapted from the module's synopsis
for ( $Address ) {
/$RE{net}{IPv4}/ and print "Dotted decimal IP address";
/$RE{net}{IPv4}{hex}/ and print "Dotted hexadecimal IP address";
/$RE{net}{IPv4}{oct}{-sep => ':'}/ and
print "Colon separated octal IP address";
/$RE{net}{IPv4}{bin}/ and print "Dotted binary IP address";
/$RE{net}{MAC}/ and print "MAC address";
/$RE{net}{MAC}{oct}{-sep => " "}/ and
print "Space separated octal MAC address";
}
使用一个你需要的。
如果您无法安装模块,然后只需通过模块的code潜伏,并得到正确的正则表达式来使用,这取决于什么样的IP地址,你想匹配。
If you cannot install the module, then just lurk through the module's code and get the correct regexp to use, depending on what kind of IP address you'd like to match.
或者只是使用类似上面并调用同一子,如果地址匹配任何您想要的符号,或者类似的规定。
Or, just use something like the above and call the same sub if the address matches any of the notations you want, or something along those lines.
从您的shell脚本使用这将是大致相同的:
Using it from your shell script would be along the lines of:
return perl -e'use Regexp::Common qw/net/;$ip=shift;if ($ip =~ /$RE{net}{IPv4}/){exit 0}else{exit 1}' "$Address";
以上会取代你的完整案块。
The above would replace your complete "case" block.
此外,如果需要通过读取模块的code内联perl脚本正则表达式叫你可以这样做。
Again, if you need to inline the regex in the perl script call you can do so by reading the module's code.
这篇关于shell脚本+短语法验证有效的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!