- 论坛徽章:
- 0
|
perl里面,有没有判断ip地址函数?
自己写了一个不知道对否?
- sub iPAddressCheck
- {
- my $intFlag = 0; #Set to 1 when matched
- my $intSingleColon = 0; #For count ":" when check IPv6 address
- my $intDoubleColon = 0; #For count "::" when check IPv6 address
-
- my ($strIP) = @_;
- $strIP =~s/^\s*//; #Delete front blanks
- $strIP =~s/\s*$//; #Delete end blanks
-
- #If $strIP obser the IPv4 format, print "ipv4"
- if($strIP =~/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/)
- {
- if($1 !~/^0\d+$/ && $2 !~/^0\d+$/ && $3 !~/^0\d+$/ && $4 !~/^0\d+$/)
- {
- if($1<224 && $1!= 127 && $2<255 && $3<255 && $4<255 && $4!=0)
- {
- $intFlag = 1;
- }
- }
- }
-
- #If $strIP obser the IPv6 format, print "ipv6"
- if($strIP !~/[^0-9A-Fa-f:]/ && $strIP !~/[0-9A-Fa-f]{5,}/ && $strIP !~/^:[0-9A-Fa-f]/ && $strIP !~/[0-9A-Fa-f]:$/)
- {
- while($strIP =~ /:/g)
- {
- ++$intSingleColon;
- }
- while($strIP =~ /::/g)
- {
- ++$intDoubleColon;
- }
- if(($intDoubleColon==0&&$intSingleColon==7) or ($intDoubleColon==1&&$intSingleColon<=7))
- {
- $intFlag = 1;
- }
- }
- retrun $intFlag;
- }
复制代码 |
|