diff --git a/pandora_console/extensions/net_tools.php b/pandora_console/extensions/net_tools.php index e6ec05977b..ba66cae68e 100644 --- a/pandora_console/extensions/net_tools.php +++ b/pandora_console/extensions/net_tools.php @@ -1,26 +1,60 @@ ".__('The agent hasn\'t got IP').''; - return; - } - - echo " - "; - - echo '
'; - echo "
"; - echo ""; - echo ''; - echo ''; - echo "'; - echo '
'; - echo __('Operation'); - ui_print_help_tip( - __('You can set the command path in the menu Administration -> Extensions -> Config Network Tools') - ); - echo ''; - echo "'; - echo ''; - echo __('IP address'); - echo ''; - echo "'; - echo '"; - echo __('SNMP Community').' '; - echo ""; - echo ''; - echo ""; - echo '
'; - echo '
'; - - $operation = get_parameter('operation', 0); - $community = get_parameter('community', 'public'); - $ip = get_parameter('select_ips'); - if (!validate_address($ip)) { - ui_print_error_message(__('The ip or dns name entered cannot be resolved')); + ui_print_error_message(__('The ip or dns name entered cannot be resolved')); } else { switch ($operation) { case 1: @@ -175,7 +147,7 @@ function main_net_tools() } else { echo '

'.__('Traceroute to ').$ip.'

'; echo '
';
-                    echo system("$traceroute $ip");
+                    echo system($traceroute.' '.$ip);
                     echo '
'; } break; @@ -187,7 +159,7 @@ function main_net_tools() } else { echo '

'.__('Ping to %s', $ip).'

'; echo '
';
-                    echo system("$ping -c 5 $ip");
+                    echo system($ping.' -c 5 '.$ip);
                     echo '
'; } break; @@ -199,7 +171,7 @@ function main_net_tools() } else { echo '

'.__('Basic TCP Scan on ').$ip.'

'; echo '
';
-                    echo system("$nmap -F $ip");
+                    echo system($nmap.' -F '.$ip);
                     echo '
'; } break; @@ -212,7 +184,7 @@ function main_net_tools() ui_print_error_message(__('Dig executable does not exist.')); } else { echo '
';
-                    echo system("dig $ip");
+                    echo system('dig '.$ip);
                     echo '
'; } @@ -221,51 +193,227 @@ function main_net_tools() ui_print_error_message(__('Whois executable does not exist.')); } else { echo '
';
-                    echo system("whois $ip");
+                    echo system('whois '.$ip);
                     echo '
'; } break; case 3: + $snmp_obj = [ + 'ip_target' => $ip, + 'snmp_version' => $snmp_version, + 'snmp_community' => $community, + 'format' => '-Oqn', + ]; + + $snmp_obj['base_oid'] = '.1.3.6.1.2.1.1.3.0'; + $result = get_h_snmpwalk($snmp_obj); echo '

'.__('SNMP information for ').$ip.'

'; - - $snmpget = whereis_the_command('snmpget'); - if (empty($snmpget)) { - ui_print_error_message(__('SNMPget executable does not exist.')); + echo '

'.__('Uptime').'

'; + echo '
';
+                if (empty($result)) {
+                    ui_print_error_message(__('Target unreachable.'));
+                    break;
                 } else {
-                    echo '

'.__('Uptime').'

'; - echo '
';
-                    echo exec("$snmpget -Ounv -v1 -c $community $ip .1.3.6.1.2.1.1.3.0 ");
-                    echo '
'; - echo '

'.__('Device info').'

'; - echo '
';
-
-                    echo system("$snmpget -Ounv -v1 -c $community $ip .1.3.6.1.2.1.1.1.0 ");
-                    echo '
'; - - echo '

Interface Information

'; - echo ''; - echo '
'.__('Interface'); - echo ''.__('Status'); - - $int_max = exec("$snmpget -Oqunv -v1 -c $community $ip .1.3.6.1.2.1.2.1.0 "); - - for ($ax = 0; $ax < $int_max; $ax++) { - $interface = exec("$snmpget -Oqunv -v1 -c $community $ip .1.3.6.1.2.1.2.2.1.2.$ax "); - $estado = exec("$snmpget -Oqunv -v1 -c $community $ip .1.3.6.1.2.1.2.2.1.8.$ax "); - echo "
$interface$estado"; - } - - echo '
'; + echo array_pop($result); } + + echo '
'; + echo '

'.__('Device info').'

'; + echo '
';
+                $snmp_obj['base_oid'] = '.1.3.6.1.2.1.1.1.0';
+                $result = get_h_snmpwalk($snmp_obj);
+                if (empty($result)) {
+                    ui_print_error_message(__('Target unreachable.'));
+                    break;
+                } else {
+                    echo array_pop($result);
+                }
+
+                echo '
'; + + echo '

Interface Information

'; + + $table = new StdClass(); + $table->class = 'databox'; + $table->head = []; + $table->head[] = __('Interface'); + $table->head[] = __('Status'); + + $i = 0; + + $base_oid = '.1.3.6.1.2.1.2.2.1'; + $idx_oids = '.1'; + $names_oids = '.2'; + $status_oids = '.8'; + + $snmp_obj['base_oid'] = $base_oid.$idx_oids; + $idx = get_h_snmpwalk($snmp_obj); + + $snmp_obj['base_oid'] = $base_oid.$names_oids; + $names = get_h_snmpwalk($snmp_obj); + + $snmp_obj['base_oid'] = $base_oid.$status_oids; + $statuses = get_h_snmpwalk($snmp_obj); + + foreach ($idx as $k => $v) { + $index = str_replace($base_oid.$idx_oids, '', $k); + $name = $names[$base_oid.$names_oids.$index]; + + $status = $statuses[$base_oid.$status_oids.$index]; + + $table->data[$i][0] = $name; + $table->data[$i++][1] = $status; + } + + html_print_table($table); + break; + + default: + // Ignore. break; } } +} + + +/** + * Main function. + * + * @return void + */ +function main_net_tools() +{ + $operation = get_parameter('operation', 0); + $community = get_parameter('community', 'public'); + $ip = get_parameter('select_ips'); + $snmp_version = get_parameter('select_version'); + + // Show form. + $id_agente = get_parameter('id_agente', 0); + $principal_ip = db_get_sql( + sprintf( + 'SELECT direccion FROM tagente WHERE id_agente = %d', + $id_agente + ) + ); + + $list_address = db_get_all_rows_sql( + sprintf( + 'SELECT id_a FROM taddress_agent WHERE id_agent = %d', + $id_agente + ) + ); + foreach ($list_address as $address) { + $ids[] = join(',', $address); + } + + $ips = db_get_all_rows_sql( + sprintf( + 'SELECT ip FROM taddress WHERE id_a IN (%s)', + join($ids) + ) + ); + + if ($ips == '') { + echo "
".__('The agent hasn\'t got IP').'
'; + return; + } + + // Javascript. + ?> + + '; + echo "
"; + echo ""; + echo ''; + echo ''; + echo "'; + echo '
'; + echo __('Operation'); + ui_print_help_tip( + __('You can set the command path in the menu Administration -> Extensions -> Config Network Tools') + ); + echo ''; + + html_print_select( + [ + 1 => __('Traceroute'), + 2 => __('Ping host & Latency'), + 3 => __('SNMP Interface status'), + 4 => __('Basic TCP Port Scan'), + 5 => __('DiG/Whois Lookup'), + ], + 'operation', + $operation, + 'mostrarColumns(this.value)', + __('Please select') + ); + + echo ''; + echo __('IP address'); + echo ''; + + $ips_for_select = array_reduce( + $ips, + function ($carry, $item) { + $carry[$item['ip']] = $item['ip']; + return $carry; + } + ); + + html_print_select( + $ips_for_select, + 'select_ips', + $principal_ip + ); + echo '"; + echo __('SNMP Version'); + html_print_select( + [ + '1' => 'v1', + '2c' => 'v2c', + ], + 'select_version', + $snmp_version + ); + echo ''; + echo __('SNMP Community').' '; + html_print_input_text('community', $community); + echo ''; + echo ""; + echo '
'; + echo '
'; + + if ($operation) { + // Execute form. + net_tools_execute($operation, $ip, $community, $snmp_version); + } + echo '
'; } +/** + * Add option. + * + * @return void + */ function godmode_net_tools() { global $config; diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index fea9131e09..9232854317 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -1836,6 +1836,52 @@ function array_key_to_offset($array, $key) } +/** + * Undocumented function + * + * @param array $arguments Following format: + * [ + * 'ip_target' + * 'snmp_version' + * 'snmp_community' + * 'snmp3_auth_user' + * 'snmp3_security_level' + * 'snmp3_auth_method' + * 'snmp3_auth_pass' + * 'snmp3_privacy_method' + * 'snmp3_privacy_pass' + * 'quick_print' + * 'base_oid' + * 'snmp_port' + * 'server_to_exec' + * 'extra_arguments' + * 'format' + * ] + * + * @return array SNMP result. + */ +function get_h_snmpwalk(array $arguments) +{ + return get_snmpwalk( + $arguments['ip_target'], + $arguments['snmp_version'], + isset($arguments['snmp_community']) ? $arguments['snmp_community'] : '', + isset($arguments['snmp3_auth_user']) ? $arguments['snmp3_auth_user'] : '', + isset($arguments['snmp3_security_level']) ? $arguments['snmp3_security_level'] : '', + isset($arguments['snmp3_auth_method']) ? $arguments['snmp3_auth_method'] : '', + isset($arguments['snmp3_auth_pass']) ? $arguments['snmp3_auth_pass'] : '', + isset($arguments['snmp3_privacy_method']) ? $arguments['snmp3_privacy_method'] : '', + isset($arguments['snmp3_privacy_pass']) ? $arguments['snmp3_privacy_pass'] : '', + isset($arguments['quick_print']) ? $arguments['quick_print'] : 0, + isset($arguments['base_oid']) ? $arguments['base_oid'] : '', + isset($arguments['snmp_port']) ? $arguments['snmp_port'] : '', + isset($arguments['server_to_exec']) ? $arguments['server_to_exec'] : 0, + isset($arguments['extra_arguments']) ? $arguments['extra_arguments'] : '', + isset($arguments['format']) ? $arguments['format'] : '-Oa' + ); +} + + /** * Make a snmpwalk and return it. * @@ -1956,11 +2002,16 @@ function get_snmpwalk( exec($command_str, $output, $rc); } - // Parse the output of snmpwalk + // Parse the output of snmpwalk. $snmpwalk = []; foreach ($output as $line) { - // Separate the OID from the value - $full_oid = explode(' = ', $line); + // Separate the OID from the value. + if (strpos($format, 'q') === false) { + $full_oid = explode(' = ', $line, 2); + } else { + $full_oid = explode(' ', $line, 2); + } + if (isset($full_oid[1])) { $snmpwalk[$full_oid[0]] = $full_oid[1]; }