my $block = new Net::Netmask('84.22.142.192/27');
if($block){
print $block->nth(1); #выводит 84.22.142.193
print $block->mask(); #выводит маску 255.255.255.224
}
function getip4mask($bits) {
$ret = 0;
for($i = 0; $i < $bits; $i++)
$ret |= 1 << 31-$i;
return $ret;
}
$cidr = '84.22.142.192/27';
$matches = null;
if(preg_match('/^((?:(?:(2(?:5[0-5]|[0-4]\d))|1*[0-9]{0,2})\.){3}(?3))\/(\d|[1-2]\d|3[0-2])$/', $cidr, $matches))
{
list('1' => $ip4, '3' => $bits) = $matches;
$ip4l = ip2long($ip4);
$maskl = getip4mask($bits);
$first_ip = $bits == 32 ? $ip4l : ($ip4l & $maskl) + 1;
echo long2ip($first_ip),"\n";
echo long2ip($maskl),"\n";
}
<?php
class Net
{
public static function netMask($cidr)
{
$ipc = explode('/', $cidr);
$ip = $ipc[0];
$bc = $ipc[1];
$netmask = str_split(str_pad(str_pad('', $bc, '1'), 32, '0'), 8);
foreach ($netmask as &$element) {
$element = bindec($element);
}
return array($ip, join('.', $netmask));
}
}
print_r(Net::netMask('84.22.142.192/27'));
// Выхлоп:
// Array
// (
// [0] => 84.22.142.192
// [1] => 255.255.255.224
// )
<?php
$output = shell_exec(`ip -o -f inet addr show | awk '/scope global/ {print $4}'`);
echo "<pre>$output</pre>";
?>