function cacti_snmp_getnext($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $method = SNMP_VALUE_LIBRARY, $environ = SNMP_POLLER) { global $config; /* determine default retries */ if ($retries == 0 || !is_numeric($retries)) { $retries = read_config_option('snmp_retries'); if ($retries == '') { $retries = 3; } } /* do not attempt to poll invalid combinations */ if ($version == 0 || !is_numeric($version) || !is_numeric($port) || !is_numeric($retries) || !is_numeric($timeout) || $community == '' && $version != 3) { return 'U'; } if (snmp_get_method($version) == SNMP_METHOD_PHP && (!strlen($context) || $version != 3)) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); /* set the output format to numeric */ snmp_set_valueretrieval($method); if ($version == '1') { $snmp_value = @snmpgetnext("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } elseif ($version == '2') { $snmp_value = @snmp2_getnext("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } else { if ($priv_proto == '[None]') { $proto = 'authNoPriv'; $priv_proto = ''; } else { $proto = 'authPriv'; } $snmp_value = @snmp3_getnext("{$hostname}:{$port}", "{$username}", $proto, $auth_proto, "{$password}", $priv_proto, "{$priv_pass}", "{$oid}", $timeout * 1000, $retries); } if ($snmp_value === false) { cacti_log("WARNING: SNMP GetNext Timeout for Host:'{$hostname}', and OID:'{$oid}'", false); } } else { /* ucd/net snmp want the timeout in seconds */ $timeout = ceil($timeout / 1000); if ($version == '1') { $snmp_auth = '-c ' . snmp_escape_string($community); /* v1/v2 - community string */ } elseif ($version == '2') { $snmp_auth = '-c ' . snmp_escape_string($community); /* v1/v2 - community string */ $version = '2c'; /* ucd/net snmp prefers this over '2' */ } elseif ($version == '3') { if ($priv_proto == '[None]') { $proto = 'authNoPriv'; $priv_proto = ''; } else { $proto = 'authPriv'; } if (strlen($priv_pass)) { $priv_pass = '******' . snmp_escape_string($priv_pass) . ' -x ' . snmp_escape_string($priv_proto); } else { $priv_pass = ''; } if (strlen($context)) { $context = '-n ' . snmp_escape_string($context); } else { $context = ''; } $snmp_auth = trim('-u ' . snmp_escape_string($username) . ' -l ' . snmp_escape_string($proto) . ' -a ' . snmp_escape_string($auth_proto) . ' -A ' . snmp_escape_string($password) . ' ' . $priv_pass . ' ' . $context); /* v3 - username/password */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } exec(cacti_escapeshellcmd(read_config_option('path_snmpgetnext')) . " -O fntev {$snmp_auth} -v {$version} -t {$timeout} -r {$retries} " . cacti_escapeshellarg($hostname) . ":{$port} " . cacti_escapeshellarg($oid), $snmp_value); } if (isset($snmp_value)) { /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(' ', $snmp_value); } } if (substr_count($snmp_value, 'Timeout:')) { cacti_log("WARNING: SNMP GetNext Timeout for Host:'{$hostname}', and OID:'{$oid}'", false); } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value, false); return $snmp_value; }
function cacti_snmp_getnext($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; /* determine default retries */ if ($retries == 0 || !is_numeric($retries)) { $retries = read_config_option("snmp_retries"); if ($retries == "") { $retries = 3; } } /* do not attempt to poll invalid combinations */ if ($version == 0 || !is_numeric($version) || !is_numeric($port) || !is_numeric($retries) || !is_numeric($timeout) || $community == "" && $version != 3) { return "U"; } if (snmp_get_method($version) == SNMP_METHOD_PHP && (!strlen($context) || $version != 3)) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); if ($version == "1") { $snmp_value = @snmpgetnext("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } elseif ($version == "2") { $snmp_value = @snmp2_getnext("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } else { if ($priv_proto == "[None]" || $priv_pass == '') { $proto = "authNoPriv"; $priv_proto = ""; } else { $proto = "authPriv"; } $snmp_value = @snmp3_getnext("{$hostname}:{$port}", "{$username}", $proto, $auth_proto, "{$password}", $priv_proto, "{$priv_pass}", "{$oid}", $timeout * 1000, $retries); } if ($snmp_value === false) { cacti_log("WARNING: SNMP GetNext Timeout for Host:'{$hostname}', and OID:'{$oid}'", false); } } else { $snmp_value = ''; /* ucd/net snmp want the timeout in seconds */ $timeout = ceil($timeout / 1000); if ($version == "1") { $snmp_auth = read_config_option("snmp_version") == "ucd-snmp" ? snmp_escape_string($community) : "-c " . snmp_escape_string($community); /* v1/v2 - community string */ } elseif ($version == "2") { $snmp_auth = read_config_option("snmp_version") == "ucd-snmp" ? snmp_escape_string($community) : "-c " . snmp_escape_string($community); /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ } elseif ($version == "3") { if ($priv_proto == "[None]" || $priv_pass == '') { $proto = "authNoPriv"; $priv_proto = ""; } else { $proto = "authPriv"; } if (strlen($priv_pass)) { $priv_pass = "******" . snmp_escape_string($priv_pass) . " -x " . snmp_escape_string($priv_proto); } else { $priv_pass = ""; } if (strlen($context)) { $context = "-n " . snmp_escape_string($context); } else { $context = ""; } $snmp_auth = trim("-u " . snmp_escape_string($username) . " -l " . snmp_escape_string($proto) . " -a " . snmp_escape_string($auth_proto) . " -A " . snmp_escape_string($password) . " " . $priv_pass . " " . $context); /* v3 - username/password */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } if (read_config_option("snmp_version") == "ucd-snmp") { /* escape the command to be executed and vulnerable parameters * numeric parameters are not subject to command injection * snmp_auth is treated seperately, see above */ exec(cacti_escapeshellcmd(read_config_option("path_snmpgetnext")) . " -O vt -v{$version} -t {$timeout} -r {$retries} " . cacti_escapeshellarg($hostname) . ":{$port} {$snmp_auth} " . cacti_escapeshellarg($oid), $snmp_value); } else { exec(cacti_escapeshellcmd(read_config_option("path_snmpgetnext")) . " -O fntev {$snmp_auth} -v {$version} -t {$timeout} -r {$retries} " . cacti_escapeshellarg($hostname) . ":{$port} " . cacti_escapeshellarg($oid), $snmp_value); } } if (isset($snmp_value)) { /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } if (substr_count($snmp_value, "Timeout:")) { cacti_log("WARNING: SNMP GetNext Timeout for Host:'{$hostname}', and OID:'{$oid}'", false); } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value, false); return $snmp_value; }
function cacti_snmp_getnext($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; /* determine default retries */ if (($retries == 0) || (!is_numeric($retries))) { $retries = read_config_option("snmp_retries"); if ($retries == "") $retries = 3; } /* do not attempt to poll invalid combinations */ if (($version == 0) || (($community == "") && ($version != 3))) { return "U"; } if ((snmp_get_method($version) == SNMP_METHOD_PHP) && (!strlen($context) || ($version != 3))) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); if ($version == "1") { $snmp_value = @snmpgetnext("$hostname:$port", "$community", "$oid", ($timeout * 1000), $retries); }elseif ($version == "2") { $snmp_value = @snmp2_getnext("$hostname:$port", "$community", "$oid", ($timeout * 1000), $retries); }else{ if ($priv_proto == "[None]") { $proto = "authNoPriv"; $priv_proto = ""; }else{ $proto = "authPriv"; } $snmp_value = @snmp3_getnext("$hostname:$port", "$username", $proto, $auth_proto, "$password", $priv_proto, "$priv_pass", "$oid", ($timeout * 1000), $retries); } }else { /* ucd/net snmp want the timeout in seconds */ $timeout = ceil($timeout / 1000); if ($version == "1") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? cacti_escapeshellarg($community): "-c " . cacti_escapeshellarg($community); /* v1/v2 - community string */ }elseif ($version == "2") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? cacti_escapeshellarg($community): "-c " . cacti_escapeshellarg($community); /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ }elseif ($version == "3") { if ($priv_proto == "[None]") { $proto = "authNoPriv"; $priv_proto = ""; }else{ $proto = "authPriv"; } if (strlen($priv_pass)) { $priv_pass = "******" . cacti_escapeshellarg($priv_pass) . " -x " . cacti_escapeshellarg($priv_proto); }else{ $priv_pass = ""; } if (strlen($context)) { $context = "-n " . cacti_escapeshellarg($context); }else{ $context = ""; } $snmp_auth = trim("-u " . cacti_escapeshellarg($username) . " -l " . cacti_escapeshellarg($proto) . " -a " . cacti_escapeshellarg($auth_proto) . " -A " . cacti_escapeshellarg($password) . " " . $priv_pass . " " . $context); /* v3 - username/password */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } if (read_config_option("snmp_version") == "ucd-snmp") { exec(read_config_option("path_snmpgetnext") . " -O vt -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid", $snmp_value); }else { exec(read_config_option("path_snmpgetnext") . " -O fntev $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid", $snmp_value); } } if (isset($snmp_value)) { /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value, false); return $snmp_value; }
function cacti_snmp_getnext($hostname, $community, $oid, $version, $username, $password, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; /* determine default retries */ if (($retries == 0) || (!is_numeric($retries))) { $retries = read_config_option("snmp_retries"); if ($retries == "") $retries = 3; } /* do not attempt to poll invalid combinations */ if (($version == 0) || (($community == "") && ($version != 3))) { return "U"; } if (snmp_get_method($version) == SNMP_METHOD_PHP) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); if ($version == "1") { $snmp_value = @snmpgetnext("$hostname:$port", "$community", "$oid", ($timeout * 1000), $retries); }elseif ($version == "2") { $snmp_value = @snmp2_getnext("$hostname:$port", "$community", "$oid", ($timeout * 1000), $retries); }else{ $snmp_value = @snmp3_getnext("$hostname:$port", $username, "authNoPriv", "MD5", $password, "", "", $oid, ($timeout * 1000), $retries); } }else { /* ucd/net snmp want the timeout in seconds */ $timeout = ceil($timeout / 1000); if ($version == "1") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ }elseif ($version == "2") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ }elseif ($version == "3") { $snmp_auth = "-u $username -l authNoPriv -a MD5 -A $password"; /* v3 - username/password */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } if (read_config_option("snmp_version") == "ucd-snmp") { exec(read_config_option("path_snmpgetnext") . " -O vt -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid", $snmp_value); }else { exec(read_config_option("path_snmpgetnext") . " -O fntev $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid", $snmp_value); } } if (isset($snmp_value)) { /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value); return $snmp_value; }
<?php require_once dirname(__FILE__) . '/snmp_include.inc'; //EXPECTF format is quickprint OFF snmp_set_quick_print(false); snmp_set_valueretrieval(SNMP_VALUE_PLAIN); echo "Single OID\n"; var_dump(snmpgetnext($hostname, $community, '.1.3.6.1.2.1.1.1.0', $timeout, $retries)); echo "Single OID in array\n"; var_dump(snmpgetnext($hostname, $community, array('.1.3.6.1.2.1.1.1.0'), $timeout, $retries)); echo "Multiple OID\n"; var_dump(snmpgetnext($hostname, $community, array('.1.3.6.1.2.1.1.1.0', '.1.3.6.1.2.1.1.6.0'), $timeout, $retries));
function cacti_snmp_getnext($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; /* determine default retries */ if ($retries == 0 || !is_numeric($retries)) { $retries = read_config_option("snmp_retries"); if ($retries == "") { $retries = 3; } } /* do not attempt to poll invalid combinations */ if ($version == 0 || $community == "" && $version != 3) { return "U"; } if (snmp_get_method($version) == SNMP_METHOD_PHP) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); if ($version == "1") { $snmp_value = @snmpgetnext("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } elseif ($version == "2") { $snmp_value = @snmp2_getnext("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } else { if ($auth_proto == "[None]") { $proto = "authNoPriv"; } else { $proto = "authPriv"; } $snmp_value = @snmp3_getnext("{$hostname}:{$port}", $username, $proto, $auth_proto, "{$password}", "{$priv_pass}", $priv_proto, "{$oid}", $timeout * 1000, $retries); } } else { /* ucd/net snmp want the timeout in seconds */ $timeout = ceil($timeout / 1000); if ($version == "1") { $snmp_auth = read_config_option("snmp_version") == "ucd-snmp" ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ } elseif ($version == "2") { $snmp_auth = read_config_option("snmp_version") == "ucd-snmp" ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ } elseif ($version == "3") { if ($auth_proto == "[None]") { $proto = "authNoPriv"; } else { $proto = "authPriv"; } if (strlen($priv_pass)) { $priv_pass = "******"{$priv_pass}\" -x {$priv_proto}"; } else { $priv_pass = ""; } $snmp_auth = "-u {$username} -l {$proto} -a {$auth_proto} -A {$password} {$priv_pass}"; /* v3 - username/password */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } if (read_config_option("snmp_version") == "ucd-snmp") { exec(read_config_option("path_snmpgetnext") . " -O vt -v{$version} -t {$timeout} -r {$retries} {$hostname}:{$port} {$snmp_auth} {$oid}", $snmp_value); } else { exec(read_config_option("path_snmpgetnext") . " -O fntev {$snmp_auth} -v {$version} -t {$timeout} -r {$retries} {$hostname}:{$port} {$oid}", $snmp_value); } } if (isset($snmp_value)) { /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value); return $snmp_value; }