예제 #1
0
function perform_snmp_scan($net)
{
    global $stats, $config, $debug, $vdebug;
    echo 'Range: ' . $net->network . '/' . $net->bitmask . PHP_EOL;
    $config['snmp']['timeout'] = 1;
    $config['snmp']['retries'] = 0;
    $config['fping_options']['retries'] = 0;
    $start = ip2long($net->network);
    $end = ip2long($net->broadcast) - 1;
    while ($start++ < $end) {
        $stats['count']++;
        $host = long2ip($start);
        if (match_network($config['autodiscovery']['nets-exclude'], $host)) {
            echo '|';
            continue;
        }
        $test = isPingable($host);
        if ($test['result'] === false) {
            echo '.';
            continue;
        }
        if (ip_exists($host)) {
            $stats['known']++;
            echo '*';
            continue;
        }
        foreach (array('udp', 'tcp') as $transport) {
            try {
                addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $config['distributed_poller_group']);
                $stats['added']++;
                echo '+';
                break;
            } catch (HostExistsException $e) {
                $stats['known']++;
                echo '*';
                break;
            } catch (HostUnreachablePingException $e) {
                echo '.';
                break;
            } catch (HostUnreachableException $e) {
                if ($debug) {
                    print_error($e->getMessage() . " over {$transport}");
                    foreach ($e->getReasons() as $reason) {
                        echo "  {$reason}\n";
                    }
                }
                if ($transport == 'tcp') {
                    // tried both udp and tcp without success
                    $stats['failed']++;
                    echo '-';
                }
            }
        }
    }
    echo PHP_EOL;
}
예제 #2
0
function perform_snmp_scan($net)
{
    global $stats, $config, $quiet;
    echo 'Range: ' . $net->network . '/' . $net->bitmask . PHP_EOL;
    $config['snmp']['timeout'] = 1;
    $config['snmp']['retries'] = 0;
    $config['fping_options']['retries'] = 0;
    $start = ip2long($net->network);
    $end = ip2long($net->broadcast) - 1;
    while ($start++ < $end) {
        $stats['count']++;
        $device_id = false;
        $host = long2ip($start);
        $test = isPingable($host);
        if ($test['result'] === false) {
            echo '.';
            continue;
        }
        if (ip_exists($host)) {
            $stats['known']++;
            echo '*';
            continue;
        }
        foreach (array('udp', 'tcp') as $transport) {
            if ($device_id !== false && $device_id > 0) {
                $stats['added']++;
                echo '+';
            } else {
                if ($device_id === 0) {
                    $stats['failed']++;
                    echo '-';
                    break;
                }
            }
            $device_id = addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $quiet, $config['distributed_poller_group'], 0);
        }
    }
    echo PHP_EOL;
}
예제 #3
0
function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0', $poller_group = '0', $force_add = '0')
{
    global $config;
    list($hostshort) = explode(".", $host);
    // Test Database Exists
    if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') {
        if ($config['addhost_alwayscheckip'] === TRUE) {
            $ip = gethostbyname($host);
        } else {
            $ip = $host;
        }
        if (ip_exists($ip) === false) {
            // Test reachability
            if ($force_add == 1 || isPingable($host)) {
                if (empty($snmpver)) {
                    // Try SNMPv2c
                    $snmpver = 'v2c';
                    $ret = addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
                    if (!$ret) {
                        //Try SNMPv3
                        $snmpver = 'v3';
                        $ret = addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
                        if (!$ret) {
                            // Try SNMPv1
                            $snmpver = 'v1';
                            return addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
                        } else {
                            return $ret;
                        }
                    } else {
                        return $ret;
                    }
                }
                if ($snmpver === "v3") {
                    // Try each set of parameters from config
                    foreach ($config['snmp']['v3'] as $v3) {
                        $device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3);
                        if ($quiet == '0') {
                            print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
                        }
                        if ($force_add == 1 || isSNMPable($device)) {
                            $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                            if (empty($snmphost) or $snmphost == $host || ($hostshort = $host)) {
                                $device_id = createHost($host, NULL, $snmpver, $port, $transport, $v3, $poller_group);
                                return $device_id;
                            } else {
                                if ($quiet == '0') {
                                    print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
                                }
                            }
                        } else {
                            if ($quiet == '0') {
                                print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using {$snmpver}");
                            }
                        }
                    }
                } elseif ($snmpver === "v2c" or $snmpver === "v1") {
                    // try each community from config
                    foreach ($config['snmp']['community'] as $community) {
                        $device = deviceArray($host, $community, $snmpver, $port, $transport, NULL);
                        if ($quiet == '0') {
                            print_message("Trying community {$community} ...");
                        }
                        if ($force_add == 1 || isSNMPable($device)) {
                            $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                            if (empty($snmphost) || $snmphost && ($snmphost == $host || ($hostshort = $host))) {
                                $device_id = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group);
                                return $device_id;
                            } else {
                                if ($quiet == '0') {
                                    print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
                                }
                            }
                        } else {
                            if ($quiet == '0') {
                                print_error("No reply on community {$community} using {$snmpver}");
                            }
                        }
                    }
                } else {
                    if ($quiet == '0') {
                        print_error("Unsupported SNMP Version \"{$snmpver}\".");
                    }
                }
                if (!$device_id) {
                    // Failed SNMP
                    if ($quiet == '0') {
                        print_error("Could not reach {$host} with given SNMP community using {$snmpver}");
                    }
                }
            } else {
                // failed Reachability
                if ($quiet == '0') {
                    print_error("Could not ping {$host}");
                }
            }
        } else {
            if ($quiet == 0) {
                print_error("Already have host with this IP {$host}");
            }
        }
    } else {
        // found in database
        if ($quiet == '0') {
            print_error("Already got host {$host}");
        }
    }
    return 0;
}
예제 #4
0
        if (mysql_num_rows($query_run) >= 1) {
            return true;
        }
    }
}
function ip_add($ip)
{
    $query = "INSERT INTO `hits_ip` VALUES ('{$ip}')";
    $query_run = mysql_query($query);
}
function update_count()
{
    $query = "SELECT `count` FROM `hits_count`";
    if ($query_run = mysql_query($query)) {
        $count = mysql_result($query_run, 0, 'count');
        $count_inc = $count + 1;
        $query_update = "UPDATE `hits_count` SET `count` = '{$count_inc}'";
        $query_update_run = mysql_query($query_update);
    }
}
if (!ip_exists($user_ip)) {
    update_count();
    ip_add($user_ip);
    echo $user_ip . ' Does not exist written to ' . '<br>';
} else {
    echo $user_ip . ' Already Exists!!' . '<br>';
}
?>


예제 #5
0
function perform_snmp_scan($net, $force_network, $force_broadcast)
{
    global $stats, $config, $debug, $vdebug;
    echo 'Range: ' . $net->network . '/' . $net->bitmask . PHP_EOL;
    $config['snmp']['timeout'] = 1;
    $config['snmp']['retries'] = 0;
    $config['fping_options']['retries'] = 0;
    $start = ip2long($net->network);
    $end = ip2long($net->broadcast) - 1;
    if ($force_network === true) {
        //Force-scan network address
        d_echo("Forcing network address scan" . PHP_EOL);
        $start = $start - 1;
    }
    if ($force_broadcast === true) {
        //Force-scan broadcast address
        d_echo("Forcing broadcast address scan" . PHP_EOL);
        $end = $end + 1;
    }
    if ($net->bitmask === "31") {
        //Handle RFC3021 /31 prefixes
        $start = ip2long($net->network) - 1;
        $end = ip2long($net->broadcast);
        d_echo("RFC3021 network, hosts " . long2ip($start + 1) . " and " . long2ip($end) . PHP_EOL . PHP_EOL);
    } elseif ($net->bitmask === "32") {
        //Handle single-host /32 prefixes
        $start = ip2long($net->network) - 1;
        $end = $start + 1;
        d_echo("RFC3021 network, hosts " . long2ip($start + 1) . " and " . long2ip($end) . PHP_EOL . PHP_EOL);
    } else {
        d_echo("Network:   " . $net->network . PHP_EOL);
        d_echo("Broadcast: " . $net->broadcast . PHP_EOL . PHP_EOL);
    }
    while ($start++ < $end) {
        $stats['count']++;
        $host = long2ip($start);
        if ($vdebug) {
            echo "Scanning: " . $host . PHP_EOL;
        }
        if (match_network($config['autodiscovery']['nets-exclude'], $host)) {
            if ($vdebug) {
                echo "Excluded by config.php" . PHP_EOL . PHP_EOL;
            } else {
                echo '|';
            }
            continue;
        }
        $test = isPingable($host);
        if ($test['result'] === false) {
            if ($vdebug) {
                echo "Unpingable Device" . PHP_EOL . PHP_EOL;
            } else {
                echo '.';
            }
            continue;
        }
        if (ip_exists($host)) {
            $stats['known']++;
            if ($vdebug) {
                echo "Known Device" . PHP_EOL;
            } else {
                echo '*';
            }
            continue;
        }
        foreach (array('udp', 'tcp') as $transport) {
            try {
                addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $config['distributed_poller_group']);
                $stats['added']++;
                if ($vdebug) {
                    echo "Added Device" . PHP_EOL . PHP_EOL;
                } else {
                    echo '+';
                }
                break;
            } catch (HostExistsException $e) {
                $stats['known']++;
                if ($vdebug) {
                    echo "Known Device" . PHP_EOL . PHP_EOL;
                } else {
                    echo '*';
                }
                break;
            } catch (HostUnreachablePingException $e) {
                if ($vdebug) {
                    echo "Unpingable Device" . PHP_EOL . PHP_EOL;
                } else {
                    echo '.';
                }
                break;
            } catch (HostUnreachableException $e) {
                if ($debug) {
                    print_error($e->getMessage() . " over {$transport}");
                    foreach ($e->getReasons() as $reason) {
                        echo "  {$reason}" . PHP_EOL;
                    }
                }
                if ($transport === 'tcp') {
                    // tried both udp and tcp without success
                    $stats['failed']++;
                    if ($vdebug) {
                        echo "Failed to Add Device" . PHP_EOL . PHP_EOL;
                    } else {
                        echo '-';
                    }
                }
            }
        }
    }
    echo PHP_EOL;
}
extract($_POST);
$obj_setting = new common();
$obj = new validation();
/* Get Current Date Time Stamp */
$currentTimestamp = getCurrentTimestamp();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $error = '';
    $obj->add_fields($ip_address, 'req', 'Please Enter Page Title');
    $error = $obj->validate();
    if ($error) {
        $errorMsg = "<font color='#FF0000' family='verdana' size=2>Please fill all required fields.</font>";
    } else {
        if (!filter_var($ip_address, FILTER_VALIDATE_IP)) {
            $errorMsg = "<font color='#FF0000' family='verdana' size=2>Please enter a valid IP Address.</font>";
        } else {
            if (ip_exists($ip_address)) {
                $errorMsg = "<font color='#FF0000' family='verdana' size=2>IP address already exists.</font>";
            } else {
                $_SESSION['success_msg'] = 'New IP address has been saved successfully.';
                $dataArr = array('ip_address' => $ip_address);
                $update_site = $obj_setting->save(TBL_IP, $dataArr);
                echo '<script>location.href="' . DEFAULT_ADMIN_URL . '/ip/view.php";</script>';
                exit;
            }
        }
    }
}
function ip_exists($ip_address)
{
    $rsObj = mysql_query("SELECT id  FROM `whitelist_ips` WHERE `ip_address` =  '" . $ip_address . "'");
    if (mysql_num_rows($rsObj) > 0) {
예제 #7
0
/**
 * Add a device to LibreNMS
 *
 * @param string $host dns name or ip address
 * @param string $snmp_version If this is empty, try v2c,v3,v1.  Otherwise, use this specific version.
 * @param string $port the port to connect to for snmp
 * @param string $transport udp or tcp
 * @param string $poller_group the poller group this device will belong to
 * @param boolean $force_add add even if the device isn't reachable
 * @param string $port_assoc_mode snmp field to use to determine unique ports
 *
 * @return int returns the device_id of the added device
 *
 * @throws HostExistsException This hostname already exists
 * @throws HostIpExistsException We already have a host with this IP
 * @throws HostUnreachableException We could not reach this device is some way
 * @throws HostUnreachablePingException We could not ping the device
 * @throws InvalidPortAssocModeException The given port association mode was invalid
 * @throws SnmpVersionUnsupportedException The given snmp version was invalid
 */
function addHost($host, $snmp_version = '', $port = '161', $transport = 'udp', $poller_group = '0', $force_add = false, $port_assoc_mode = 'ifIndex')
{
    global $config;
    // Test Database Exists
    if (host_exists($host) === true) {
        throw new HostExistsException("Already have host {$host}");
    }
    // Valid port assoc mode
    if (!is_valid_port_assoc_mode($port_assoc_mode)) {
        throw new InvalidPortAssocModeException("Invalid port association_mode '{$port_assoc_mode}'. Valid modes are: " . join(', ', get_port_assoc_modes()));
    }
    // check if we have the host by IP
    if ($config['addhost_alwayscheckip'] === true) {
        $ip = gethostbyname($host);
    } else {
        $ip = $host;
    }
    if (ip_exists($ip)) {
        throw new HostIpExistsException("Already have host with this IP {$host}");
    }
    // Test reachability
    if (!$force_add) {
        $address_family = snmpTransportToAddressFamily($transport);
        $ping_result = isPingable($host, $address_family);
        if (!$ping_result['result']) {
            throw new HostUnreachablePingException("Could not ping {$host}");
        }
    }
    // if $snmpver isn't set, try each version of snmp
    if (empty($snmp_version)) {
        $snmpvers = array('v2c', 'v3', 'v1');
    } else {
        $snmpvers = array($snmp_version);
    }
    $host_unreachable_exception = new HostUnreachableException("Could not connect, please check the snmp details and snmp reachability");
    // try different snmp variables to add the device
    foreach ($snmpvers as $snmpver) {
        if ($snmpver === "v3") {
            // Try each set of parameters from config
            foreach ($config['snmp']['v3'] as $v3) {
                $device = deviceArray($host, null, $snmpver, $port, $transport, $v3, $port_assoc_mode);
                if ($force_add === true || isSNMPable($device)) {
                    if ($force_add !== true) {
                        $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                    }
                    $result = createHost($host, null, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode, $snmphost, $force_add);
                    if ($result !== false) {
                        return $result;
                    }
                } else {
                    $host_unreachable_exception->addReason("SNMP {$snmpver}: No reply with credentials " . $v3['authname'] . "/" . $v3['authlevel']);
                }
            }
        } elseif ($snmpver === "v2c" || $snmpver === "v1") {
            // try each community from config
            foreach ($config['snmp']['community'] as $community) {
                $device = deviceArray($host, $community, $snmpver, $port, $transport, null, $port_assoc_mode);
                if ($force_add === true || isSNMPable($device)) {
                    if ($force_add !== true) {
                        $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                    }
                    $result = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group, $port_assoc_mode, $snmphost, $force_add);
                    if ($result !== false) {
                        return $result;
                    }
                } else {
                    $host_unreachable_exception->addReason("SNMP {$snmpver}: No reply with community {$community}");
                }
            }
        } else {
            throw new SnmpVersionUnsupportedException("Unsupported SNMP Version \"{$snmpver}\", must be v1, v2c, or v3");
        }
    }
    throw $host_unreachable_exception;
}
예제 #8
0
         /* free result set */
         $result_2->close();
     }
     ?>
                </tbody>
              </table>
              </div>
            </form>
              <?php 
 } elseif ($page == "rootserver?edit=" . $row["id"]) {
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         if (isset($_POST['confirm'])) {
             $error = false;
             $ip = htmlentities($_POST['ip']);
             $port = htmlentities($_POST['port']);
             if (ip_exists($ip, $row["id"])) {
                 $msg = _dedicated_message_ip_exists;
                 $error = true;
             }
             if (!preg_match("/^[0-9]+\$/", $port)) {
                 $msg = _dedicated_message_port_exists . "<br>";
                 $error = true;
             }
             if (isValidIP($ip) == false) {
                 $msg = _dedicated_message_ip_invalid . "<br>";
                 $error = true;
             }
             if ($error == false) {
                 $stmtz = $mysqli->prepare("SELECT ip FROM dedicated WHERE id = ?");
                 $stmtz->bind_param('i', $row["id"]);
                 $stmtz->execute();
예제 #9
0
파일: 123.php 프로젝트: SUDIP2222/php-Basic
<?php

require 'connect.inc.php';
$user_ip = $_SERVER['REMOTE_ADDR'];
function ip_exists($ip)
{
    global $user_ip;
    echo $user_ip;
}
function update_count()
{
}
ip_exists('1');