-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalice-unused-addr.php
120 lines (81 loc) · 2.79 KB
/
alice-unused-addr.php
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* alice-unused-addr.php
*
* Lists unused addresses from alice-lg json
*
* @package ixf-parse
* @author Luiz Gustavo Barros <luzigb@gmail.com>
* @version 1.0.0
* @link https://github.com/luizgb/ixf-parse
*/
if (!function_exists('curl_init')) {
echo "curl_init() function is not available. Install php-curl module".PHP_EOL;
exit();
}
if (!filter_var($argv[1], FILTER_VALIDATE_URL)) {
echo "USAGE: php alice-unused-addr.php <url> <network>".PHP_EOL;
echo "Example: php alice-unused-addr.php https://lg.apps.uepg.br/api/v1/routeservers/rs1-v4/neighbors 45.186.143.0/24".PHP_EOL;
echo "ERROR: Parameter <url> is not a valid URL".PHP_EOL;
exit();
}
if (!filter_var(explode("/", $argv[2])[0], FILTER_VALIDATE_IP)) {
echo "USAGE: php alice-unused-addr.php <url> <network>".PHP_EOL;
echo "Example: php alice-unused-addr.php https://lg.apps.uepg.br/api/v1/routeservers/rs1-v4/neighbors 45.186.143.0/24".PHP_EOL;
echo "ERROR: Parameter <network> is not a valid network".PHP_EOL;
exit();
}
$url=$argv[1];
$cidr=$argv[2];
$data=json_decode(getJson($url), true);
$member_address=null;
if (is_array($data['neighbours'])) {
foreach ($data['neighbours'] as $neighbour) {
$member_address[]=$neighbour['address'];
}
}
if (is_array($member_address)){
echo "### CIDR: ".$cidr.PHP_EOL;
$ips=printIpDiff($member_address, $cidr);
foreach ($ips as $ip){
echo $ip.PHP_EOL;
}
}
else {
echo "Empty array or wrong query".PHP_EOL;
}
// functions
function getJson($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
curl_close($curl);
return $curl_response;
}
function printIpDiff($ip_list, $cidr)
{
$ips = array();
$range = getIpRange($cidr);
for ($ip = $range['firstIP']; $ip <= $range['lastIP']; $ip++) {
if (in_array(long2ip($ip), $ip_list) === false) {
$ips[] = long2ip($ip);
}
}
return $ips;
}
function getIpRange( $cidr) {
list($ip, $mask) = explode('/', $cidr);
$maskBinStr =str_repeat("1", $mask ) . str_repeat("0", 32-$mask ); //net mask binary string
$inverseMaskBinStr = str_repeat("0", $mask ) . str_repeat("1", 32-$mask ); //inverse mask
$ipLong = ip2long( $ip );
$ipMaskLong = bindec( $maskBinStr );
$inverseIpMaskLong = bindec( $inverseMaskBinStr );
$netWork = $ipLong & $ipMaskLong;
$start = $netWork+1;//ignore network ID(eg: 192.168.1.0)
$end = ($netWork | $inverseIpMaskLong) -1 ; //ignore brocast IP(eg: 192.168.1.255)
return array('firstIP' => $start, 'lastIP' => $end );
}
?>