function snmpgeneric_snmpconfig($object_id) { $object = spotEntity('object', $object_id); //$object['attr'] = getAttrValues($object_id); $endpoints = findAllEndpoints($object_id, $object['name']); addJS('function showsnmpv3(element) { var style; if(element.value != \'v3\') { style = \'none\'; document.getElementById(\'snmp_community_label\').style.display=\'\'; } else { style = \'\'; document.getElementById(\'snmp_community_label\').style.display=\'none\'; } var elements = document.getElementsByName(\'snmpv3\'); for(var i=0;i<elements.length;i++) { elements[i].style.display=style; } };', TRUE); addJS('function shownewobject(element) { var style; if(element.checked) { style = \'\'; } else { style = \'none\'; } var elements = document.getElementsByName(\'newobject\'); for(var i=0;i<elements.length;i++) { elements[i].style.display=style; } };', TRUE); addJS('function checkInput() { var host = document.getElementById(\'host\'); if(host.value == "-1") { var newvalue = prompt("Enter Hostname or IP Address",""); if(newvalue != "") { host.options[host.options.length] = new Option(newvalue, newvalue); host.value = newvalue; } } if(host.value != "-1" && host.value != "") return true; else return false; };', TRUE); echo '<body onload="document.getElementById(\'submitbutton\').focus(); showsnmpv3(document.getElementById(\'snmpversion\')); shownewobject(document.getElementById(\'asnewobject\'));">'; foreach ($endpoints as $key => $value) { $endpoints[$value] = $value; unset($endpoints[$key]); } unset($key); unset($value); foreach (getObjectIPv4Allocations($object_id) as $ip => $value) { $ip = ip_format($ip); if (!in_array($ip, $endpoints)) { $endpoints[$ip] = $ip; } } unset($ip); unset($value); foreach (getObjectIPv6Allocations($object_id) as $value) { $ip = ip_format(ip_parse($value['addrinfo']['ip'])); if (!in_array($ip, $endpoints)) { $endpoints[$ip] = $ip; } } unset($value); /* ask for ip/host name on submit see js checkInput() */ $endpoints['-1'] = 'ask me'; // saved snmp settings $snmpstr = strtok($object['comment'], "\n\r"); $snmpstrarray = explode(':', $snmpstr); if ($snmpstrarray[0] == "SNMP") { /* keep it compatible with older version */ switch ($snmpstrarray[2]) { case "1": $snmpstrarray[2] = 'v1'; break; case "2": case "v2C": $snmpstrarray[2] = 'v2c'; break; case "3": $snmpstrarray[2] = 'v3'; break; } $snmpnames = array('SNMP', 'host', 'version', 'community'); if ($snmpstrarray[2] == "v3") { $snmpnames = array_merge($snmpnames, array('sec_level', 'auth_protocol', 'auth_passphrase', 'priv_protocol', 'priv_passphrase')); } $snmpvalues = array(); foreach ($snmpnames as $key => $value) { if (isset($snmpstrarray[$key])) { switch ($key) { case 6: case 8: $snmpvalues[$value] = base64_decode($snmpstrarray[$key]); break; default: $snmpvalues[$value] = $snmpstrarray[$key]; } } } unset($snmpvalues['SNMP']); $snmpconfig = $snmpvalues; } else { $snmpconfig = array(); } $snmpconfig += $_POST; if (!isset($snmpconfig['host'])) { $snmpconfig['host'] = -1; /* try to find first FQDN or IP */ foreach ($endpoints as $value) { if (preg_match('/^[^ .]+(\\.[^ .]+)+\\.?/', $value)) { $snmpconfig['host'] = $value; break; } } unset($value); } // sg_var_dump_html($endpoints); if (!isset($snmpconfig['version'])) { $snmpconfig['version'] = mySNMP::SNMP_VERSION; } if (!isset($snmpconfig['community'])) { $snmpconfig['community'] = getConfigVar('DEFAULT_SNMP_COMMUNITY'); } if (empty($snmpconfig['community'])) { $snmpconfig['community'] = mySNMP::SNMP_COMMUNITY; } if (!isset($snmpconfig['sec_level'])) { $snmpconfig['sec_level'] = NULL; } if (!isset($snmpconfig['auth_protocol'])) { $snmpconfig['auth_protocol'] = NULL; } if (!isset($snmpconfig['auth_passphrase'])) { $snmpconfig['auth_passphrase'] = NULL; } if (!isset($snmpconfig['priv_protocol'])) { $snmpconfig['priv_protocol'] = NULL; } if (!isset($snmpconfig['priv_passphrase'])) { $snmpconfig['priv_passphrase'] = NULL; } if (!isset($snmpconfig['asnewobject'])) { $snmpconfig['asnewobject'] = NULL; } if (!isset($snmpconfig['object_type_id'])) { $snmpconfig['object_type_id'] = '8'; } if (!isset($snmpconfig['object_name'])) { $snmpconfig['object_name'] = NULL; } if (!isset($snmpconfig['object_label'])) { $snmpconfig['object_label'] = NULL; } if (!isset($snmpconfig['object_asset_no'])) { $snmpconfig['object_asset_no'] = NULL; } if (!isset($snmpconfig['save'])) { $snmpconfig['save'] = true; } // sg_var_dump_html($snmpconfig); // $snmpv3displaystyle = ($snmpconfig['version'] == "3" ? "style=\"\"" : "style=\"display:none;\""); echo '<h1 align=center>SNMP Config</h1>'; echo '<form method=post name="snmpconfig" onsubmit="return checkInput()" action=' . $_SERVER['REQUEST_URI'] . ' />'; echo '<table cellspacing=0 cellpadding=5 align=center class=widetable> <tr><th class=tdright>Host:</th><td>'; //if($snmpconfig['asnewobject'] == '1' ) if ($snmpconfig['host'] != '-1' and !isset($endpoints[$snmpconfig['host']])) { $endpoints[$snmpconfig['host']] = $snmpconfig['host']; } echo getSelect($endpoints, array('id' => 'host', 'name' => 'host'), $snmpconfig['host'], FALSE); echo '</td></tr> <tr> <th class=tdright><label for=snmpversion>Version:</label></th> <td class=tdleft>'; echo getSelect(array("v1" => 'v1', "v2c" => 'v2c', "v3" => 'v3'), array('name' => 'version', 'id' => 'snmpversion', 'onchange' => 'showsnmpv3(this)'), $snmpconfig['version'], FALSE); echo '</td> </tr> <tr> <th id="snmp_community_label" class=tdright><label for=community>Community:</label></th> <th name="snmpv3" style="display:none;" class=tdright><label for=community>Security Name:</label></th> <td class=tdleft><input type=text name=community value=' . $snmpconfig['community'] . ' ></td> </tr> <tr name="snmpv3" style="display:none;"> <th></th> </tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label">Security Level:</label></th> <td class=tdleft>'; echo getSelect(array('noAuthNoPriv' => 'no Auth and no Priv', 'authNoPriv' => 'auth without Priv', 'authPriv' => 'auth with Priv'), array('name' => 'sec_level'), $snmpconfig['sec_level'], FALSE); echo '</td></tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label>Auth Type:</label></th> <td class=tdleft> <input name=auth_protocol type=radio value=MD5 ' . ($snmpconfig['auth_protocol'] == 'MD5' ? ' checked="checked"' : '') . '/><label>MD5</label> <input name=auth_protocol type=radio value=SHA ' . ($snmpconfig['auth_protocol'] == 'SHA' ? ' checked="checked"' : '') . '/><label>SHA</label> </td> </tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label>Auth Key:</label></th> <td class=tdleft><input type=password id=auth_passphrase name=auth_passphrase value="' . $snmpconfig['auth_passphrase'] . '"></td> </tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label>Priv Type:</label></th> <td class=tdleft> <input name=priv_protocol type=radio value=DES ' . ($snmpconfig['priv_protocol'] == 'DES' ? ' checked="checked"' : '') . '/><label>DES</label> <input name=priv_protocol type=radio value=AES ' . ($snmpconfig['priv_protocol'] == 'AES' ? ' checked="checked"' : '') . '/><label>AES</label> </td> </tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label>Priv Key</label></th> <td class=tdleft><input type=password name=priv_passphrase value="' . $snmpconfig['priv_passphrase'] . '"></td> </tr> </tr> <tr> <th></th> <td class=tdleft> <input name=asnewobject id=asnewobject type=checkbox value=1 onchange="shownewobject(this)"' . ($snmpconfig['asnewobject'] == '1' ? ' checked="checked"' : '') . '> <label>Create as new object</label></td> </tr>'; // $newobjectdisplaystyle = ($snmpconfig['asnewobject'] == '1' ? "" : "style=\"display:none;\""); echo '<tr name="newobject" style="display:none;"> <th class=tdright>Type:</th><td class=tdleft>'; $typelist = withoutLocationTypes(readChapter(CHAP_OBJTYPE, 'o')); $typelist = cookOptgroups($typelist); printNiftySelect($typelist, array('name' => "object_type_id"), $snmpconfig['object_type_id']); echo '</td></tr> <tr name="newobject" style="display:none;"> <th class=tdright>Common name:</th><td class=tdleft><input type=text name=object_name value=' . $snmpconfig['object_name'] . '></td></tr> <tr name="newobject" style="display:none;"> <th class=tdright>Visible label:</th><td class=tdleft><input type=text name=object_label value=' . $snmpconfig['object_label'] . '></td></tr> <tr name="newobject" style="display:none;"> <th class=tdright>Asset tag:</th><td class=tdleft><input type=text name=object_asset_no value=' . $snmpconfig['object_asset_no'] . '></td></tr> <tr> <th></th> <td class=tdleft> <input name=save id=save type=checkbox value=1' . ($snmpconfig['save'] == '1' ? ' checked="checked"' : '') . '> <label>Save SNMP settings for object</label></td> </tr> <td colspan=2> <input type=hidden name=snmpconfig value=1> <input type=submit id="submitbutton" tabindex="1" value="Show List"></td></tr> </table></form>'; }
function doSNMPmining($object_id, $snmpsetup) { global $objectInfo; $objectInfo = spotEntity('object', $object_id); $objectInfo['attrs'] = getAttrValues($object_id); $endpoints = findAllEndpoints($object_id, $objectInfo['name']); if (count($endpoints) == 0) { showFuncMessage(__FUNCTION__, 'ERR1'); // endpoint not found return; } if (count($endpoints) > 1) { showFuncMessage(__FUNCTION__, 'ERR2'); // can't pick an address return; } switch ($objectInfo['objtype_id']) { case 7: // Router // Router case 8: // Network switch // Network switch case 965: // Wireless // Wireless case 1503: // Network chassis $device = new RTSNMPDevice($endpoints[0], $snmpsetup); return doGenericSNMPmining($device); case 2: $device = new APCPowerSwitch($endpoints[0], $snmpsetup); return doPDUSNMPmining($device); } }
function setSwitchVLANs($object_id = 0, $setcmd) { global $remote_username; $objectInfo = spotEntity('object', $object_id); $endpoints = findAllEndpoints($object_id, $objectInfo['name']); if (count($endpoints) == 0) { throw new RTGatewayError('no management address set'); } if (count($endpoints) > 1) { throw new RTGatewayError('cannot pick management address'); } $hwtype = $swtype = 'unknown'; foreach (getAttrValues($object_id) as $record) { if ($record['name'] == 'SW type' && strlen($record['o_value'])) { $swtype = strtr(execGMarker($record['o_value']), ' ', '+'); } if ($record['name'] == 'HW type' && strlen($record['o_value'])) { $hwtype = strtr(execGMarker($record['o_value']), ' ', '+'); } } $endpoint = str_replace(' ', '+', $endpoints[0]); $data = queryGateway('switchvlans', array("connect {$endpoint} {$hwtype} {$swtype} {$remote_username}", $setcmd)); // Finally we can parse the response into message array. foreach (explode(';', substr($data[1], strlen('OK!'))) as $text) { $message = 'gw: ' . substr($text, 2); if (strpos($text, 'I!') === 0) { showSuccess($message); } elseif (strpos($text, 'W!') === 0) { showWarning($message); } elseif (strpos($text, 'E!') === 0) { showError($message); } else { // All improperly formatted messages must be treated as error conditions. showError('unexpected line from gw: ' . $text); } } }
function gwDeployDeviceConfig($object_id, $breed, $text) { if ($text == '') { throw new InvalidArgException('text', '', 'deploy text is empty'); } $objectInfo = spotEntity('object', $object_id); $endpoints = findAllEndpoints($object_id, $objectInfo['name']); if (count($endpoints) == 0) { throw new RTGatewayError('no management address set'); } if (count($endpoints) > 1) { throw new RTGatewayError('cannot pick management address'); } $endpoint = str_replace(' ', '\\ ', str_replace(' ', '+', $endpoints[0])); $tmpfilename = tempnam('', 'RackTables-deviceconfig-'); if (FALSE === file_put_contents($tmpfilename, $text)) { unlink($tmpfilename); throw new RTGatewayError('failed to write to temporary file'); } try { queryGateway('deviceconfig', array("deploy {$endpoint} {$breed} {$tmpfilename}")); unlink($tmpfilename); } catch (RTGatewayError $e) { unlink($tmpfilename); throw $e; } }
function queryTerminal($object_id, $commands, $tolerate_remote_errors = TRUE) { $objectInfo = spotEntity('object', $object_id); $endpoints = findAllEndpoints($object_id, $objectInfo['name']); if (count($endpoints) == 0) { throw new RTGatewayError('no management address set'); } if (count($endpoints) > 1) { throw new RTGatewayError('cannot pick management address'); } // telnet prompt and mode specification switch ($breed = detectDeviceBreed($object_id)) { case 'ios12': case 'fdry5': case 'ftos8': $protocol = 'netcat'; // default is netcat mode $prompt = '^(Login|Username|Password): $|^\\S+[>#]$'; // set the prompt in case user would like to specify telnet protocol break; case 'air12': $protocol = 'telnet'; # Aironet IOS is broken $prompt = '^(Username|Password): $|^\\S+[>#]$'; break; case 'vrp53': case 'vrp55': $protocol = 'telnet'; $prompt = '^\\[[^[\\]]+\\]$|^<[^<>]+>$|^(Username|Password):$|(?:\\[Y\\/N\\]|\\(Y\\/N\\)\\[[YN]\\]):?$'; break; case 'nxos4': $protocol = 'telnet'; $prompt = '(^([Ll]ogin|[Pp]assword):|[>#]) $'; break; case 'xos12': $protocol = 'telnet'; $prompt = ': $|\\.\\d+ # $|\\?\\s*\\([Yy]\\/[Nn]\\)\\s*$'; break; case 'jun10': $protocol = 'telnet'; $prompt = '^login: $|^Password:$|^\\S+@\\S+[>#] $'; break; case 'eos4': $protocol = 'telnet'; # strict RFC854 implementation, netcat won't work $prompt = '^(\\xf2?login|Username|Password): $|^\\S+[>#]$'; break; case 'ros11': $protocol = 'netcat'; # see ftos8 case $prompt = '^(User Name|\\rPassword):$|^\\r?\\S+# $'; break; case 'iosxr4': $protocol = 'telnet'; $prompt = '^\\r?(Login|Username|Password): $|^\\r?\\S+[>#]$'; break; case 'ucs': $protocol = 'ucssdk'; break; default: $protocol = 'netcat'; $prompt = NULL; } // set the default settings before calling user-defined callback $settings = array('hostname' => $endpoints[0], 'protocol' => $protocol, 'port' => NULL, 'prompt' => $prompt, 'username' => NULL, 'password' => NULL, 'timeout' => 15, 'connect_timeout' => 2, 'prompt_delay' => 0.001, 'sudo_user' => NULL, 'identity_file' => NULL); if (is_callable('terminal_settings')) { call_user_func('terminal_settings', $objectInfo, array(&$settings)); } // override settings if (!isset($settings['port']) and $settings['protocol'] == 'netcat') { $settings['port'] = 23; } $params = array($settings['hostname']); $params_from_settings = array(); switch ($settings['protocol']) { case 'telnet': case 'netcat': // prepend command list with vendor-specific disabling pager command switch ($breed) { case 'ios12': $commands = "terminal length 0\n" . $commands; break; case 'nxos4': case 'air12': case 'ftos8': $commands = "terminal length 0\nterminal no monitor\n" . $commands; break; case 'xos12': $commands = "disable clipaging\n" . $commands; break; case 'vrp55': $commands = "screen-length 0 temporary\n" . $commands; break; case 'fdry5': $commands = "skip-page-display\n" . $commands; break; case 'jun10': $commands = "set cli screen-length 0\n" . $commands; break; case 'eos4': $commands = "enable\nno terminal monitor\nterminal length 0\n" . $commands; break; case 'ros11': $commands = "terminal datadump\n" . $commands; $commands .= "\n\n"; # temporary workaround for telnet server break; case 'iosxr4': $commands = "terminal length 0\nterminal monitor disable\n" . $commands; break; case 'dlink': $commands = "disable clipaging\n" . $commands; break; } // prepend telnet commands by credentials if (isset($settings['password'])) { $commands = $settings['password'] . "\n" . $commands; } if (isset($settings['username'])) { $commands = $settings['username'] . "\n" . $commands; } // command-line options are specific to client: telnet or netcat switch ($settings['protocol']) { case 'telnet': $params_from_settings['port'] = 'port'; $params_from_settings['prompt'] = 'prompt'; $params_from_settings['connect-timeout'] = 'connect_timeout'; $params_from_settings['timeout'] = 'timeout'; $params_from_settings['prompt-delay'] = 'prompt_delay'; break; case 'netcat': $params_from_settings['p'] = 'port'; $params_from_settings['w'] = 'timeout'; $params_from_settings['b'] = 'ncbin'; break; } break; case 'ssh': $params_from_settings['port'] = 'port'; $params_from_settings['proto'] = 'proto'; $params_from_settings['username'] = '******'; $params_from_settings['i'] = 'identity_file'; $params_from_settings['sudo-user'] = '******'; $params_from_settings['connect-timeout'] = 'connect_timeout'; break; case 'ucssdk': # remote XML through a Python backend $params = array(); # reset # UCS in its current implementation besides the terminal_settings() provides # an additional username/password feed through the HTML from. Whenever the # user provides the credentials through the form, use these instead of the # credentials [supposedly] set by terminal_settings(). if ($script_mode != TRUE && !isCheckSet('use_terminal_settings')) { genericAssertion('ucs_login', 'string'); genericAssertion('ucs_password', 'string'); $settings['username'] = $_REQUEST['ucs_login']; $settings['password'] = $_REQUEST['ucs_password']; } foreach (array('hostname', 'username', 'password') as $item) { if (empty($settings[$item])) { throw new RTGatewayError("{$item} not available, check terminal_settings()"); } } $commands = "login {$settings['hostname']} {$settings['username']} {$settings['password']}\n" . $commands; break; default: throw RTGatewayError("Invalid terminal protocol '{$settings['protocol']}' specified"); } foreach ($params_from_settings as $param_name => $setting_name) { if (isset($settings[$setting_name])) { if (is_int($param_name)) { $params[] = $settings[$setting_name]; } else { $params[$param_name] = $settings[$setting_name]; } } } callHook('alterTerminalParams', $object_id, $tolerate_remote_errors, array(&$settings['protocol']), array(&$params)); $ret_code = callScript($settings['protocol'], $params, $commands, $out, $errors); if ($settings['protocol'] != 'ssh' || !$tolerate_remote_errors) { if (!empty($errors)) { throw new RTGatewayError("{$settings['protocol']} error: " . rtrim($errors)); } elseif ($ret_code !== 0) { throw new RTGatewayError("{$settings['protocol']} error: result code {$ret_code}"); } } elseif (!empty($errors)) { // ssh and tolerate and non-empty $errors foreach (explode("\n", $errors) as $line) { if (strlen($line)) { showWarning("{$settings['protocol']} {$settings['hostname']}: {$line}"); } } } return strtr($out, array("\r" => "")); // cut ^M symbols }
function doSNMPmining($object_id, $snmpsetup) { $objectInfo = spotEntity('object', $object_id); $objectInfo['attrs'] = getAttrValues($object_id); $endpoints = findAllEndpoints($object_id, $objectInfo['name']); if (count($endpoints) == 0) { return showFuncMessage(__FUNCTION__, 'ERR1'); } // endpoint not found if (count($endpoints) > 1) { return showFuncMessage(__FUNCTION__, 'ERR2'); } // can't pick an address switch ($objectInfo['objtype_id']) { case 7: case 8: $device = new RTSNMPDevice($endpoints[0], $snmpsetup); return doSwitchSNMPmining($objectInfo, $device); case 2: $device = new APCPowerSwitch($endpoints[0], $snmpsetup); return doPDUSNMPmining($objectInfo, $device); } }
function queryTerminal($object_id, $commands, $tolerate_remote_errors = TRUE) { $objectInfo = spotEntity('object', $object_id); $endpoints = findAllEndpoints($object_id, $objectInfo['name']); if (count($endpoints) == 0) { throw new RTGatewayError('no management address set'); } if (count($endpoints) > 1) { throw new RTGatewayError('cannot pick management address'); } // telnet prompt and mode specification switch ($breed = detectDeviceBreed($object_id)) { case 'ios12': case 'ftos8': $protocol = 'netcat'; // default is netcat mode $prompt = '^(Login|[Uu]sername|Password): $|^\\S+[>#]$|\\[[^][]*\\]\\? $'; // set the prompt in case user would like to specify telnet protocol $commands = "terminal length 0\nterminal no monitor\n" . $commands; break; case 'air12': $protocol = 'telnet'; # Aironet IOS is broken $prompt = '^(Username|Password): $|^\\S+[>#]$'; $commands = "terminal length 0\nterminal no monitor\n" . $commands; break; case 'fdry5': $protocol = 'netcat'; // default is netcat mode $prompt = '^(Login|Username|Password): $|^\\S+[>#]$'; // set the prompt in case user would like to specify telnet protocol $commands = "skip-page-display\n" . $commands; break; case 'vrp55': case 'vrp85': $commands = "screen-length 0 temporary\n" . $commands; /* fall-through */ /* fall-through */ case 'vrp53': $protocol = 'telnet'; $prompt = '^\\[[^[\\]]+\\]$|^<[^<>]+>$|^(Username|Password):$|\\[[Yy][^\\[\\]]*\\]\\s*:?\\s*$'; break; case 'nxos4': $protocol = 'telnet'; $prompt = '(^([Ll]ogin|[Pp]assword):|[>#]) $'; $commands = "terminal length 0\nterminal no monitor\n" . $commands; break; case 'xos12': $protocol = 'telnet'; $prompt = ': $|\\.\\d+ # $|\\?\\s*\\([Yy]\\/[Nn]\\)\\s*$'; $commands = "disable clipaging\n" . $commands; break; case 'jun10': $protocol = 'telnet'; $prompt = '^login: $|^Password:$|^\\S+@\\S+[>#] $'; $commands = "set cli screen-length 0\n" . $commands; break; case 'eos4': $protocol = 'telnet'; # strict RFC854 implementation, netcat won't work $prompt = '^\\xf2?(login|Username|Password): $|^\\S+[>#]$'; $commands = "enable\nno terminal monitor\nterminal length 0\n" . $commands; break; case 'ros11': $protocol = 'netcat'; # see ftos8 case $prompt = '^(User Name|\\rPassword):$|^\\r?\\S+# $'; $commands = "terminal datadump\n" . $commands; $commands .= "\n\n"; # temporary workaround for telnet server break; case 'iosxr4': $protocol = 'telnet'; $prompt = '^\\r?(Login|Username|Password): $|^\\r?\\S+[>#]$'; $commands = "terminal length 0\nterminal monitor disable\n" . $commands; break; case 'ucs': $protocol = 'ucssdk'; break; case 'dlink': $protocol = 'netcat'; $commands = "disable clipaging\n" . $commands; break; } if (!isset($protocol)) { $protocol = 'netcat'; } if (!isset($prompt)) { $prompt = NULL; } // set the default settings before calling user-defined callback $settings = array('hostname' => $endpoints[0], 'protocol' => $protocol, 'port' => NULL, 'prompt' => $prompt, 'username' => NULL, 'password' => NULL, 'timeout' => 15, 'connect_timeout' => 2, 'prompt_delay' => 0.001, 'sudo_user' => NULL, 'identity_file' => NULL); // override default settings if (is_callable('terminal_settings')) { call_user_func('terminal_settings', $objectInfo, array(&$settings)); } // make gateway-specific CLI params out of settings $params = callHook('makeGatewayParams', $object_id, $tolerate_remote_errors, array(&$settings), array(&$commands)); // call gateway $ret_code = callScript($settings['protocol'], $params, $commands, $out, $errors); if ($settings['protocol'] != 'ssh' || !$tolerate_remote_errors) { if (!empty($errors)) { throw new RTGatewayError("{$settings['protocol']} error: " . rtrim($errors)); } elseif ($ret_code !== 0) { throw new RTGatewayError("{$settings['protocol']} error: result code {$ret_code}"); } } elseif (!empty($errors)) { // ssh and tolerate and non-empty $errors foreach (explode("\n", $errors) as $line) { if (strlen($line)) { showWarning("{$settings['protocol']} {$settings['hostname']}: {$line}"); } } } return strtr($out, array("\r" => "")); // cut ^M symbols }
function snmpgeneric_snmpconfig($object_id) { echo '<body onload="document.getElementById(\'submitbutton\').focus();">'; $object = spotEntity('object', $object_id); //$object['attr'] = getAttrValues($object_id); $endpoints = findAllEndpoints($object_id, $object['name']); addJS('function showsnmpv3(element) { var style; if(element.value != \'' . SNMPgeneric::VERSION_3 . '\') { style = \'none\'; document.getElementById(\'snmp_community_label\').style.display=\'\'; } else { style = \'\'; document.getElementById(\'snmp_community_label\').style.display=\'none\'; } var elements = document.getElementsByName(\'snmpv3\'); for(var i=0;i<elements.length;i++) { elements[i].style.display=style; } };', TRUE); addJS('function checkInput() { var host = document.getElementById(\'host\'); if(host.value == "-1") { var newvalue = prompt("Enter Hostname or IP Address",""); if(newvalue != "") { host.options[host.options.length] = new Option(newvalue, newvalue); host.value = newvalue; } } if(host.value != "-1" && host.value != "") return true; else return false; };', TRUE); foreach ($endpoints as $key => $value) { $endpoints[$value] = $value; unset($endpoints[$key]); } unset($key); unset($value); foreach (getObjectIPv4Allocations($object_id) as $ip => $value) { $ip = ip_format($ip); if (!in_array($ip, $endpoints)) { $endpoints[$ip] = $ip; } } unset($ip); unset($value); foreach (getObjectIPv6Allocations($object_id) as $value) { $ip = ip_format(ip_parse($value['addrinfo']['ip'])); if (!in_array($ip, $endpoints)) { $endpoints[$ip] = $ip; } } unset($value); /* ask for ip/host name on submit see js checkInput() */ $endpoints['-1'] = 'ask me'; $snmpconfig = $_POST; if (!isset($snmpconfig['host'])) { $snmpconfig['host'] = -1; /* try to find first FQDN or IP */ foreach ($endpoints as $value) { if (preg_match('/^[^ .]+(\\.[^ .]+)+\\.?/', $value)) { $snmpconfig['host'] = $value; break; } } unset($value); } // sg_var_dump_html($endpoints); if (!isset($snmpconfig['snmpversion'])) { $snmpconfig['version'] = mySNMP::SNMP_VERSION; } if (!isset($snmpconfig['community'])) { $snmpconfig['community'] = getConfigVar('DEFAULT_SNMP_COMMUNITY'); } if (empty($snmpconfig['community'])) { $snmpconfig['community'] = mySNMP::SNMP_COMMUNITY; } if (!isset($snmpconfig['sec_level'])) { $snmpconfig['sec_level'] = NULL; } if (!isset($snmpconfig['auth_protocol'])) { $snmpconfig['auth_protocol'] = NULL; } if (!isset($snmpconfig['auth_passphrase'])) { $snmpconfig['auth_passphrase'] = NULL; } if (!isset($snmpconfig['priv_protocol'])) { $snmpconfig['priv_protocol'] = NULL; } if (!isset($snmpconfig['priv_passphrase'])) { $snmpconfig['priv_passphrase'] = NULL; } echo '<h1 align=center>SNMP Config</h1>'; echo '<form method=post name="snmpconfig" onsubmit="return checkInput()" action=' . $_SERVER['REQUEST_URI'] . ' />'; echo '<table cellspacing=0 cellpadding=5 align=center class=widetable> <tr><th class=tdright>Host:</th><td>'; echo getSelect($endpoints, array('id' => 'host', 'name' => 'host'), $snmpconfig['host'], FALSE); echo '</td></tr> <tr> <th class=tdright><label for=snmpversion>Version:</label></th> <td class=tdleft>'; echo getSelect(array(SNMPgeneric::VERSION_1 => 'v1', SNMPgeneric::VERSION_2C => 'v2c', SNMPgeneric::VERSION_3 => 'v3'), array('name' => 'version', 'id' => 'snmpversion', 'onchange' => 'showsnmpv3(this)'), $snmpconfig['version'], FALSE); echo '</td> </tr> <tr> <th id="snmp_community_label" class=tdright><label for=community>Community:</label></th> <th name="snmpv3" style="display:none" class=tdright><label for=community>Security Name:</label></th> <td class=tdleft><input type=text name=community value=' . $snmpconfig['community'] . ' ></td> </tr> <tr name="snmpv3" style="display:none;"> <th></th> </tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label">Security Level:</label></th> <td class=tdleft>'; echo getSelect(array('noAuthNoPriv' => 'no Auth and no Priv', 'authNoPriv' => 'auth without Priv', 'authPriv' => 'auth with Priv'), array('name' => 'sec_level'), $snmpconfig['sec_level'], FALSE); echo '</td></tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label>Auth Type:</label></th> <td class=tdleft> <input name=auth_protocol type=radio value=MD5 ' . ($snmpconfig['auth_protocol'] == 'MD5' ? ' checked="checked"' : '') . '/><label>MD5</label> <input name=auth_protocol type=radio value=SHA ' . ($snmpconfig['auth_protocol'] == 'SHA' ? ' checked="checked"' : '') . '/><label>SHA</label> </td> </tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label>Auth Key:</label></th> <td class=tdleft><input type=password id=auth_passphrase name=auth_passphrase value="' . $snmpconfig['auth_passphrase'] . '"></td> </tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label>Priv Type:</label></th> <td class=tdleft> <input name=priv_protocol type=radio value=DES ' . ($snmpconfig['priv_protocol'] == 'DES' ? ' checked="checked"' : '') . '/><label>DES</label> <input name=priv_protocol type=radio value=AES ' . ($snmpconfig['priv_protocol'] == 'AES' ? ' checked="checked"' : '') . '/><label>AES</label> </td> </tr> <tr name="snmpv3" style="display:none;"> <th class=tdright><label>Priv Key</label></th> <td class=tdleft><input type=password name=priv_passphrase value="' . $snmpconfig['priv_passphrase'] . '"></td> </tr> </tr> <td colspan=2> <input type=hidden name=snmpconfig value=1> <input type=submit id="submitbutton" tabindex="1" value="Show List"></td></tr> </table></form>'; }