siip-available-ips/script_obtener_ips.php

49 lines
1.2 KiB
PHP

<?php
if ($argc != 2) {
echo "Uso: php obtener_ip_segmento_api.php <segmento>\n";
exit(1);
}
$segmento = $argv[1];
$api_url = "https://sistema.siip.mx/nms/api/v2.1/devices/ips?suspended=false&management=true&includeObsolete=true";
$token = "393eb3d0-9b46-4a47-b9b4-473e4e24a89c";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'accept: application/json',
"x-auth-token: $token"
]);
$result = curl_exec($ch);
if ($result === false) {
echo "Error llamando a la API\n";
exit(1);
}
curl_close($ch);
$ips_en_uso = json_decode($result, true);
$segmento_prefix = "172.16.$segmento.";
$segment_ips = [];
foreach ($ips_en_uso as $ip) {
if (strpos($ip, $segmento_prefix) === 0) {
$segment_ips[] = $ip;
}
}
usort($segment_ips, function($a, $b) {
return intval(explode('.', $a)[3]) - intval(explode('.', $b)[3]);
});
$todas = [];
for ($i=1; $i<=254; $i++) {
$todas[] = "172.16.$segmento.$i";
}
$libres = array_values(array_diff($todas, $segment_ips));
echo "IPs disponibles en el segmento 172.16.$segmento.x:\n";
foreach ($libres as $ip) {
echo "$ip\n";
}