function ios12ReadInterfaces($input) { $result = array(); $state = 'start'; foreach (explode("\n", $input) as $line) { switch ($state) { case 'start': if (preg_match('/^[a-zA-Z0-9-_]*#show interfaces$/', $line)) { $state = 'intSearch'; } break; case 'intSearch': //check if line doesn't start with whitespace if (preg_match('/^\\w.*/', $line)) { $field_list = preg_split('/\\s+/', $line); $size = count($field_list); if ($size < 8) { break; } $portname = ios12ShortenIfName($field_list[0]); if (substr($field_list[$size - 1], 0, 1) == "(") { $result[$portname]['status'] = substr($field_list[$size - 1], 1, -1); } else { $result[$portname]['status'] = "down"; if ($field_list[2] == "administratively") { $result[$portname]['status'] = "disabled"; } if ($field_list[6] == "up") { $result[$portname]['status'] = "connected"; } } $result[$portname]['name'] = $field_list[0]; $result[$portname]['desc'] = ""; //next search: last input $state = 'descriptionSearch'; } break; case 'descriptionSearch': //search for description (this line may not appear!) if (preg_match('/ Description: (.*)$/', $line, $matches)) { $result[$portname]['desc'] = htmlspecialchars($matches[1]); //next search: last input $state = 'lastInputSearch'; } //keep searching as description is optional! //keep searching as description is optional! case 'lastInputSearch': //search for last input if (preg_match('/ Last input (.*?), output.*/i', $line, $matches)) { $result[$portname]['last'] = $matches[1]; //next search: last clearing $state = 'lastClearSearch'; } break; case 'lastClearSearch': //search for last clearing if (preg_match('/ Last clearing of \\"show interface\\" counters (.*)/i', $line, $matches)) { $result[$portname]['clear'] = $matches[1]; //next search: next input packets $state = 'pktInputSearch'; } break; case 'pktInputSearch': if (preg_match('/ ([0-9]*) packets input/i', $line, $matches)) { $result[$portname]['in_pkts'] = $matches[1]; //next search: output packets $state = 'pktOutputSearch'; } break; case 'pktOutputSearch': if (preg_match('/ ([0-9]*) packets output/i', $line, $matches)) { $result[$portname]['out_pkts'] = $matches[1]; //next search: next interface $state = 'intSearch'; } break; } } return $result; }
function shortenIfName($if_name, $breed = NULL, $object_id = NULL) { // this is a port name we invented in snmp.php, do not translate it if (preg_match('/^AC-in(-[12])?$/', $if_name)) { return $if_name; } global $current_query_breed; if (!isset($breed)) { if (isset($object_id)) { $breed = detectDeviceBreed($object_id); } elseif (isset($current_query_breed)) { $breed = $current_query_breed; } } switch ($breed) { case 'ios12': return ios12ShortenIfName_real($if_name); case 'vrp53': case 'vrp55': return vrp5xShortenIfName($if_name); case 'vrp85': return vrp85ShortenIfName($if_name); case 'iosxr4': return iosxr4ShortenIfName($if_name); } // default case is outside of switch() return ios12ShortenIfName($if_name); }
function ros11ReadMacList($text) { $result = array(); $got_header = FALSE; foreach (explode("\n", $text) as $line) { if (!$got_header) { $got_header = 1 == preg_match('/Vlan\\s+Mac Address\\s+Port\\s+Type\\b/', $line); } elseif (preg_match('/\\b(\\d+)\\s+([a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2})\\s+(\\S+)\\b/', $line, $m)) { $result[ios12ShortenIfName($m[3])][] = array('mac' => $m[2], 'vid' => $m[1]); } elseif (!preg_match('/^--------/', $line)) { # ruler break; } } # end of table foreach (array_keys($result) as $portname) { usort($result[$portname], 'maclist_sort'); } return $result; }