-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathixp-unused-addr.php
158 lines (112 loc) · 3.87 KB
/
ixp-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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* ixp-unused-addr.php
*
* Lists unused addresses from ixf 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 ixp-unused-addr.php <url>".PHP_EOL;
echo "ERROR: Parameter <url> is not a valid URL".PHP_EOL;
exit();
}
$url=$argv[1];
$data=json_decode(getJson($url), true);
$ixp_prefix_v4=null;
$ixp_prefix_v6=null;
if (is_array($data['ixp_list'])) {
foreach ($data['ixp_list'] as $ixp) {
//print_r($ixp);
//echo $ixp['shortname'];
if (is_array($ixp['vlan'])) {
foreach ($ixp['vlan'] as $vlan) {
//print_r($vlan);
if (array_key_exists('ipv4', $vlan)){
$ixp_prefix_v4[]=$vlan['ipv4']['prefix']."/".$vlan['ipv4']['mask_length'];
}
if (array_key_exists('ipv6', $vlan)){
$ixp_prefix_v6=$vlan['ipv6']['prefix']."/".$mask6=$vlan['ipv6']['mask_length'];
}
}
}
}
}
//print_r($ixp_prefix_v4);
//print_r($ixp_prefix_v6);
$member_address_v4=null;
$member_address_v6=null;
if (is_array($data['member_list'])) {
foreach ($data['member_list'] as $member) {
//echo "ASN:".$member['asnum'].PHP_EOL;
if (is_array($member['connection_list'])) {
foreach ($member['connection_list'] as $connection) {
if (is_array($connection['vlan_list'])) {
foreach ($connection['vlan_list'] as $vlan) {
$address4=null;
$address6=null;
if (array_key_exists('ipv4', $vlan)){
$member_address_v4[]=trim($vlan['ipv4']['address']);
//$mac=$vlan['ipv4']['mac_addresses'][0];
}
if (array_key_exists('ipv6', $vlan)){
$member_address_v6[]=$address6=trim($vlan['ipv6']['address']);
}
}
}
}
}
}
}
//print_r($member_address_v4);
//print_r($member_address_v6);
// imprime lista de IPs que nao estao alocados
foreach ($ixp_prefix_v4 as $cidr_v4) {
echo "### CIDR: ".$cidr_v4.PHP_EOL;
$ips=printIpDiff($member_address_v4, $cidr_v4);
foreach ($ips as $ip){
echo $ip.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 );
}
?>