my $ip = '1.2.3.4';
my $block1 = '1.2.3.0/27';
if(checkip($ip, $block1)) {
print STDOUT "$ip is in $block1\n";
}
else {
print STDOUT "$ip is not in $block1\n";
}
sub checkip() {
my $ip = shift;
my $block = shift;
@ip1 = split(/\./, $ip);
$ip1 = $ip1[0] * 2**24 + $ip1[1] * 2**16 + $ip1[2] * 2**8 + $ip1[3];
my @temp = split(/\//, $block);
$ip2 = $temp[0];
my $netmask = $temp[1];
@ip2 = split(/\./, $ip2);
$ip2 = $ip2[0] * 2**24 + $ip2[1] * 2**16 + $ip2[2] * 2**8 + $ip2[3];
if( $ip1 >> (32-$netmask) == $ip2 >> (32-$netmask) ) {
return 1;
}
return 0;
}
Categories
- Apache
- blogging
- bookmarks
- Calendaring
- cx_Oracle
- Debian
- Email Servers
- ESX
- Exim
- Folly
- fpm
- gcc
- git
- grub
- How Tos
- iCal
- IDE
- IIS
- ip addressing
- ipv4
- jumpstart
- libc
- libmcrypt
- lightning
- linux
- LVM
- Mail Post
- Mcrouter
- mcrypt
- microsoft
- mysql
- networking
- offsiteHowTo
- OpenSUSE
- Oracle
- Oracle Linux
- perl
- php
- phpbb
- POP3
- postfix
- Programming
- python
- qmail
- RBL
- redhat
- rpm
- SLES
- SMTP
- Software
- solaris
- spam
- SQL
- SQLAlchemy
- Sunbird
- thrift
- thunderbird
- Unix
- Wangle
- WebDav
- windows 2003
- wordpress
- zypper