if ($scanType == "discovery") { // get all existing IP addresses $addresses = getIpAddressesBySubnetId($subnetId); // set start and end IP address $calc = calculateSubnetDetailsNew($subnet, $mask, 0, 0, 0, 0); $max = $calc['maxhosts']; // we should support only up to 4094 hosts! if ($max - sizeof($addresses) > 4094) { $res['errors'] = "Scanning from GUI is only available for subnets up to /20 or 4094 hosts!"; $res = json_encode($res); print_r($res); die; } // loop and get all IP addresses for ping for ($m = 1; $m <= $max; $m++) { $ip[] = transform2decimal($subnet) + $m; } // remove already existing foreach ($addresses as $a) { $key = array_search($a['ip_addr'], $ip); if ($key !== false) { unset($ip[$key]); } } //reindex array for pinging $ip = array_values($ip); } elseif ($scanType == "update") { // get all existing IP addresses $addresses = getIpAddressesBySubnetId($subnetId); // we should support only up to 4096 hosts! if (sizeof($addresses) > 4094) {
/** * Reformat incomplete IPv4 address to decimal for search! */ function reformatIPv4forSearch($ip) { //remove % sign if present $ip = str_replace("%", "", $ip); //remove last . $size = count($ip); $lastChar = substr($ip, -1); if ($lastChar == ".") { $ip = substr($ip, 0, -1); } /* check if subnet provided, then we have all we need */ if (strpos($ip, "/") > 0) { require_once 'PEAR/Net/IPv4.php'; $net = Net_IPv4::parseAddress($ip); $result['low'] = transform2decimal($net->network); $result['high'] = transform2decimal($net->broadcast); } else { /* if subnet is not provided maye wildcard is, so explode it to array */ $ip = explode(".", $ip); //4 is ok if (sizeof($ip) == 4) { $temp = implode(".", $ip); $result['low'] = $result['high'] = transform2decimal($temp); } else { if (sizeof($ip) == 3) { $ip[3] = 0; $result['low'] = transform2decimal(implode(".", $ip)); $ip[3] = 255; $result['high'] = transform2decimal(implode(".", $ip)); } else { if (sizeof($ip) == 2) { $ip[2] = 0; $ip[3] = 0; $result['low'] = transform2decimal(implode(".", $ip)); $ip[2] = 255; $ip[3] = 255; $result['high'] = transform2decimal(implode(".", $ip)); } else { if (sizeof($ip) == 1) { $ip[1] = 0; $ip[2] = 0; $ip[3] = 0; $result['low'] = transform2decimal(implode(".", $ip)); $ip[1] = 255; $ip[2] = 255; $ip[3] = 255; $result['high'] = transform2decimal(implode(".", $ip)); } else { $result['low'] = implode(".", $ip); $result['high'] = implode(".", $ip); } } } } } //return result! return $result; }
$ip['stop'] = $range[1]; /* verify both IP addresses */ $verify1 = VerifyIpAddress($ip['start'], $ip['subnet'], $nostrict); $verify2 = VerifyIpAddress($ip['stop'], $ip['subnet'], $nostrict); /* die if wrong IP or not in correct subnet */ if ($verify1) { die('<div class="alert alert-danger">' . _('Error') . ': ' . $verify1 . ' (' . $ip['start'] . ')</div>'); } if ($verify2) { die('<div class="alert alert-danger">' . _('Error') . ': ' . $verify2 . ' (' . $ip['stop'] . ')</div>'); } /* set update for update */ $ip['type'] = "series"; /* go from start to stop and insert / update / delete IPs */ $start = transform2decimal($ip['start']); $stop = transform2decimal($ip['stop']); /* we can add only 200 IP's at once! */ $size = gmp_strval(gmp_sub($stop, $start)); if ($size > 255) { die('<div class="alert alert-danger">' . _('Only 255 IP addresses at once') . '!</div>'); } /* set limits */ $m = gmp_strval($start); $n = gmp_strval(gmp_add($stop, 1)); /* for each IP */ while (gmp_cmp($m, $n) != 0) { //reset IP address field $ip['ip_addr'] = transform2long($m); //modify action - if delete ok, dynamically reset add / edit -> if IP already exists set edit if ($ip['action'] != "delete") { if (checkDuplicate($ip['ip_addr'], $ip['subnetId'])) {
/** * Insert scan results */ function insertScanResults($res, $subnetId) { global $database; # set queries foreach ($res as $ip) { //escape strings $ip['description'] = mysqli_real_escape_string($database, $ip['description']); $query[] = "insert into `ipaddresses` (`ip_addr`,`subnetId`,`description`,`dns_name`,`lastSeen`) values ('" . transform2decimal($ip['ip_addr']) . "', '{$subnetId}', '{$ip['description']}', '{$ip['dns_name']}', NOW()); "; } # glue $query = implode("\n", $query); # execute query try { $database->executeMultipleQuerries($query); } catch (Exception $e) { $error = $e->getMessage(); print "<div class='alert alert-danger'>{$error}</div>"; return false; } # default ok return true; }
/** * Insert scan results */ function insertScanResults($res, $subnetId) { global $db; # get variables from config file $database = new database($db['host'], $db['user'], $db['pass'], $db['name']); # open db # set queries foreach ($res as $ip) { // <eNovance> // Finds the GLPI id of a new ip address. $database_glpi = new database($db['glpi_host'], $db['glpi_user'], $db['glpi_pass'], $db['glpi_name']); $query_glpi = "SELECT DISTINCT glpi_networkports.items_id "; $query_glpi .= "FROM glpi_networkports "; $query_glpi .= "INNER JOIN glpi_computers ON glpi_networkports.ip = '" . $ip['ip_addr'] . "' "; $query_glpi .= "AND glpi_computers.is_deleted = 0 "; $query_glpi .= "AND glpi_networkports.items_id = glpi_computers.id;"; $glpiId = $database_glpi->getRow($query_glpi); if (count($glpiId > 0)) { $ip['glpiId'] = $glpiId[0]; } // </eNovance> $query[] = "insert into `ipaddresses` (`ip_addr`,`subnetId`,`description`,`dns_name`,`lastSeen`, `glpiId`) values ('" . transform2decimal($ip['ip_addr']) . "', '{$subnetId}', '{$ip['description']}', '{$ip['dns_name']}', NOW(), '{$ip['glpiId']}'); "; } # glue $query = implode("\n", $query); # execute query try { $database->executeMultipleQuerries($query); } catch (Exception $e) { $error = $e->getMessage(); print "<div class='alert alert-error'>{$error}</div>"; return false; } # default ok return true; }