-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc4
executable file
·45 lines (40 loc) · 1.27 KB
/
ipc4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/php
<?php
function ipcount($version, $size) {
switch ($version) {
case "4":
$power = 32 - $size;
$cmd = sprintf("echo 2^%s | bc", $power);
$total = `$cmd`;
break;
case "6":
$power = 128 - $size;
$cmd = sprintf("echo 2^%s | bc", $power);
$total = `$cmd`;
break;
}
return str_replace("\n", "", $total);
}
function csplice($number) {
$number = str_replace("\n", "", $number);
$na = str_split($number, 1);
for ($i = strlen($number) - 3; $i > 0; $i-=3) {
array_splice($na, $i, 0, ",");
}
$ret = implode("", $na);
return $ret;
}
function subnetCount($version, $first, $second) {
return csplice(ipcount($version, $second) / ipcount($version, $first));
}
$file = $argv[0];
array_shift($argv);
if ($argc == 2) {
echo sprintf("IPv4 /%s: %s\n", $argv[0], csplice(ipcount(4, $argv[0])));
} else if ($argc == 3) {
if ($argv[0] < $argv[1]) {
die(sprintf("Syntax error: %s <version> <larger subnet> <smaller subnet>\n", $file));
}
echo sprintf("IPv4: %s /%s's in a /%s\n", subnetCount(4, $argv[0], $argv[1]), $argv[0], $argv[1]);
}
?>