Added port, limited base OID (default) removed timeout (browser) capturing error to message

This commit is contained in:
fbsanchez 2021-02-08 17:42:41 +01:00
parent 67c51b0e96
commit e711239c69
4 changed files with 89 additions and 106 deletions

View File

@ -25,6 +25,7 @@ if (is_ajax()) {
$method = (string) get_parameter('method', '');
$action = (string) get_parameter('action', '');
$target_ip = (string) get_parameter('target_ip', '');
$target_port = (string) get_parameter('target_port', '');
$community = (string) io_safe_output((get_parameter('community', '')));
$snmp_version = (string) get_parameter('snmp_browser_version', '');
$snmp3_auth_user = io_safe_output(get_parameter('snmp3_browser_auth_user'));
@ -63,7 +64,8 @@ if (is_ajax()) {
$snmp3_privacy_method,
$snmp3_privacy_pass,
'null',
$server_to_exec
$server_to_exec,
$target_port
);
if (! is_array($snmp_tree)) {
echo $snmp_tree;

View File

@ -2001,6 +2001,10 @@ function get_snmpwalk(
) {
global $config;
if (empty($ip_target) === true) {
return [];
}
// Note: quick_print is ignored
// Fix for snmp port
if (!empty($snmp_port)) {

View File

@ -265,94 +265,43 @@ function snmp_browser_get_tree(
$snmp3_privacy_method='',
$snmp3_privacy_pass='',
$snmp3_context_engine_id=null,
$server_to_exec=0
$server_to_exec=0,
$target_port=''
) {
global $config;
if ($server_to_exec != 0) {
$output = get_snmpwalk(
$target_ip,
$version,
$community,
$snmp3_auth_user,
$snmp3_security_level,
$snmp3_auth_method,
$snmp3_auth_pass,
$snmp3_privacy_method,
$snmp3_privacy_pass,
0,
$starting_oid,
'',
$server_to_exec,
'',
''
);
} else {
switch ($version) {
case '1':
$snmp_version = SNMP::VERSION_1;
break;
case '2':
$snmp_version = SNMP::VERSION_2C;
break;
case '2c':
$snmp_version = SNMP::VERSION_2C;
break;
case '3':
$snmp_version = SNMP::VERSION_3;
$community = $snmp3_auth_user;
break;
default:
$snmp_version = SNMP::VERSION_2C;
break;
}
$snmp_session = new SNMP($snmp_version, $target_ip, $community);
$snmp_session->oid_output_format = SNMP_OID_OUTPUT_MODULE;
// Set security if SNMP Version is 3.
if ($snmp_version == SNMP::VERSION_3) {
$snmp_session->setSecurity(
$snmp3_security_level,
$snmp3_auth_method,
$snmp3_auth_pass,
$snmp3_privacy_method,
$snmp3_privacy_pass,
'',
$snmp3_context_engine_id
);
}
$mibs_dir = $config['homedir'].'/attachment/mibs';
$_dir = opendir($mibs_dir);
// Future. Recomemended: Use a global config limit of MIBs loaded.
while (($mib_file = readdir($_dir)) !== false) {
if ($mib_file == '..' || $mib_file == '.') {
continue;
}
$rs = snmp_read_mib($mibs_dir.'/'.$mib_file);
if ($rs !== true) {
error_log('Failed while reading MIB file: '.$mib_file);
}
}
closedir($_dir);
$output = $snmp_session->walk($starting_oid);
if ($output == false) {
$output = $snmp_session->getError();
$snmp_session->close();
return $output;
}
$snmp_session->close();
}
$output = get_snmpwalk(
// Ip_target.
$target_ip,
// Snmp_version.
$version,
// Snmp_community.
$community,
// Snmp3_auth_user.
$snmp3_auth_user,
// Snmp3_security_level.
$snmp3_security_level,
// Snmp3_auth_method.
$snmp3_auth_method,
// Snmp3_auth_pass.
$snmp3_auth_pass,
// Snmp3_privacy_method.
$snmp3_privacy_method,
// Snmp3_privacy_pass.
$snmp3_privacy_pass,
// Quick_print.
0,
// Base_oid.
$starting_oid,
// Snmp_port.
$target_port,
// Server_to_exec.
$server_to_exec,
// Extra_arguments.
'',
// Format.
''
);
// Build the tree if output comes filled.
if (empty($output) === false) {
@ -575,6 +524,7 @@ function snmp_browser_print_oid(
$output = '';
// OID information table
$table = new StdClass();
$table->width = '100%';
$table->size = [];
$table->data = [];
@ -725,15 +675,33 @@ function snmp_browser_print_container(
$table->size = [];
$table->data = [];
$table->data[0][0] = '<strong>'.__('Target IP').'</strong> &nbsp;&nbsp;';
$table->data[0][0] .= html_print_input_text(
'target_ip',
get_parameter('target_ip', ''),
'',
25,
0,
true
$table->size[0] = '30%';
$table->data[0][0] = '<div class="mw500px"><strong>'.__('Target IP').'</strong> &nbsp;&nbsp;';
$table->data[0][0] .= html_print_input(
[
'type' => 'text',
'name' => 'target_ip',
'value' => get_parameter('target_ip', ''),
'required' => true,
'size' => 25,
'maxlength' => 0,
'return' => true,
]
);
$table->data[0][0] .= '&nbsp;&nbsp<strong>'.__('Port').'</strong>&nbsp;&nbsp;';
$table->data[0][0] .= html_print_input(
[
'type' => 'number',
'name' => 'target_port',
'id' => 'target_port',
'value' => get_parameter('target_port', 161),
'required' => true,
'return' => true,
]
);
$table->data[0][0] .= '</div>';
$table->data[0][1] = '<strong>'.__('Community').'</strong> &nbsp;&nbsp;';
$table->data[0][1] .= html_print_input_text(
'community',
@ -746,7 +714,7 @@ function snmp_browser_print_container(
$table->data[0][2] = '<strong>'.__('Starting OID').'</strong> &nbsp;&nbsp;';
$table->data[0][2] .= html_print_input_text(
'starting_oid',
get_parameter('starting_oid', '.1.3.6.1.2'),
get_parameter('starting_oid', '.1.3.6.1.2.1.2.2'),
'',
25,
0,
@ -805,13 +773,16 @@ function snmp_browser_print_container(
true
);
$table->data[1][2] = html_print_button(
__('Browse'),
'browse',
false,
'snmpBrowse()',
'class="sub search" style="margin-top:0px;"',
true
$table->data[1][2] = html_print_input(
[
'type' => 'submit',
'label' => __('Browse'),
'name' => 'browse',
'disabled' => false,
'script' => 'snmpBrowse()',
'attributes' => 'class="sub search" style="margin-top:0px;"',
'return' => true,
]
);
// SNMP v3 options.
@ -1013,8 +984,9 @@ function snmp_browser_print_container(
$output = '<div id="snmp_browser_container" style="'.$display.'">';
$output .= '<div style="text-align: left; width: '.$width.'; height: '.$height.';">';
$output .= '<div style="width: 100%">';
$output .= '<form onsubmit="snmpBrowse(); return false;">';
$output .= html_print_table($table, true);
$output .= '</div>';
$output .= '</form></div>';
if (isset($snmp_version) === false) {
$snmp_version = null;

View File

@ -1,3 +1,4 @@
/* globals $,jQuery */
// Load the SNMP tree via AJAX
function snmpBrowse() {
// Empty the SNMP tree
@ -15,6 +16,7 @@ function snmpBrowse() {
// Read the target IP and community
var target_ip = $("#text-target_ip").val();
var target_port = $("#target_port").val();
var community = $("#text-community").val();
var starting_oid = $("#text-starting_oid").val();
var snmp_version = $("#snmp_browser_version").val();
@ -25,13 +27,13 @@ function snmpBrowse() {
var snmp3_auth_pass = $("#password-snmp3_browser_auth_pass").val();
var snmp3_privacy_method = $("#snmp3_browser_privacy_method").val();
var snmp3_privacy_pass = $("#password-snmp3_browser_privacy_pass").val();
var server_to_exec = $("#server_to_exec").val();
var ajax_url = $("#hidden-ajax_url").val();
// Prepare the AJAX call
var params = {};
params["target_ip"] = target_ip;
params["target_port"] = target_port;
params["community"] = community;
params["starting_oid"] = starting_oid;
params["snmp_browser_version"] = snmp_version;
@ -52,7 +54,6 @@ function snmpBrowse() {
type: "POST",
url: (action = ajax_url),
async: true,
timeout: 120000,
success: function(data) {
// Hide the spinner
$("#spinner").css("display", "none");
@ -62,6 +63,9 @@ function snmpBrowse() {
// Manage click and select events.
snmp_browser_events_manage();
},
error: function(e) {
$("#snmp_browser").html(e);
}
});
}
@ -499,6 +503,7 @@ function checkSNMPVersion() {
function snmpBrowserWindow() {
// Keep elements in the form and the SNMP browser synced
$("#text-target_ip").val($("#text-ip_target").val());
$("#target_port").val($("#text-tcp_port").val());
$("#text-community").val($("#text-snmp_community").val());
$("#snmp_browser_version").val($("#snmp_version").val());
$("#text-snmp3_browser_auth_user").val($("#text-snmp3_auth_user").val());
@ -525,7 +530,7 @@ function snmpBrowserWindow() {
opacity: 0.5,
background: "black"
},
width: 920,
width: 1000,
height: 500
});
}