Skip to content
Advertisement

PHP telnet array reply parsing to store on mysql database

Hi guys i have a small project going on, and this project requires me to send a cmd via telnet to a device and i get a reply and it troughs out the ansewer on a array.. i need to parse this data só i can store in on a mysql table..

this is the data output reply from the telnet cmd..

 Array ( 
 [0] => show ont info 0/0 4 all 
 [1] => ----------------------------------------------------------------------------- 
 [2] => F/S P ONT MAC Control Run Config Match Desc 
 [3] => ID flag state state state 
 [4] => ---------------------------------------------------------------------------- 
 [5] => 0/0 4 1 70:A8:E3:B4:5C:5C active online success match 
 [6] => 0/0 4 2 E0:67:B3:7E:20:05 active online success match 
 [7] => 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match 
 [8] => 0/0 4 4 E0:E8:E6:4E:69:B8 active online success match 
 [9] => ----------------------------------------------------------------------------- 
 [10] => Total: 4, online 4 
 [11] => 
 [12] => OLT(config)# 
 [13] => 
 [14] => OLT(config)# 
 )

basically i need to separate F/S 0/0 P = 4 ONT ID = 1,2,3,4 depending ond cmd reply this can vary up to 64 devices , MAC CONTROL= device mac. xx:xx:xx:xx:xx RUN =active Config= online, MATCH DESCRIPTION = success match

on the mysql table we have the following rows

 FRAME SLOT = 0/0 
 PON = PON DEVICE Number
 ONT ID = ID of devices
 MAC CONTROL
 RUN
 CONFIG
 MATCH DESC

i am kind of lost i have tried foreach to filter it .. but its not storing the data correctly…

edit-1

ok so i can explode the array and filter the details but i am stuck on the loop now.. its just filtering the lad ONT id device data.. i triead a foreach but its not working.. attached code below and picture its only filtering the last ONT ID and outputing the result.pic-test

/*PON4*/
   $show_pon4[0] = "show ont info 0/0 4 all";
   $telnet->DoCommand($show_pon1, $pon4_reply);
   $showpon4_res_exp = explode("n", $pon4_reply);
   for ($h=0; $h<=count($showpon4_res_exp); $h++) {

                if (strpos($showpon4_res_exp[$h], "ID") !== FALSE) {
                        $j=1;
                        do{
                            $returns = str_replace ( "                   ", " ", $showpon4_res_exp[$h+1+$j]);
                            $returns = str_replace ( "    ", " ", $returns);
                            $returns = str_replace ( "    ", " ", $returns);
                            $returns = str_replace ( "    ", " ", $returns);
                            $returns = str_replace ( "  ", " ", $returns);
                            $returns = str_replace ( "  ", " ", $returns);
                            $returns = str_replace ( "  ", " ", $returns);
                            $dt_returns = explode(" ", trim($returns)); 
                            //die(print_r($dt_returns, true));
                            $fsp = str_replace ( "epon-slot_", "", $dt_returns[0]);
                            $fspExp = explode("/", $fsp);                     // Frame e slot from OLT 0/0  
                            $ret["returns"][$j]["frame"] = $fspExp[0];        // Read Frame 0
                            $ret["returns"][$j]["slot"] = $fspExp[1];         // Read Slot  0
                            $ret["returns"][$j]["pon"] = $dt_returns[1];              // Returns PON Nº
                            $ret["returns"][$j]["ont_id"] = $dt_returns[2];           // ONT ID 
                            $ret["returns"][$j]["mac"] = $dt_returns[3];              // Returns MAC
                            $ret["returns"][$j]["control-flag"] = $dt_returns[4];     // Returns Control Flag
                            $ret["returns"][$j]["run-state"] = $dt_returns[5];        // Returns Run-State
                            $ret["returns"][$j]["config-state"] = $dt_returns[6];     // Returns Config-State
                            $ret["returns"][$j]["match-state"] = $dt_returns[7];      // Returns Match-state
                        
                                                  

                            
                            $j++;
                        } while( strpos($showpon4_res_exp[$h+1+$j],"-----------------------------------------------------------------------------") === false );

                        $qtdOnts += $j;
                      
                    }

                 

                $ret["info"][0]["qtd"] = $qtdOnts; 
                
            }

Advertisement

Answer

Hi guys late reply but eventually managed to get the code working update on the mysql table attached working code, there were some escape security caracteres on the telnet log which were breaking the output in the middle. Obviously i must thank all the inputs from everyone (@sammitch and @jared) who participated on the replies and comments for helping me also, pointing me out on the correct steps…

$show_pon[0] = "show ont info 1 all"; 
   $telnet->DoCommand($show_pon, $poninfo);
    
    
// Will replace numeric keys with these later
 
 $test = trim(str_replace('', '', $poninfo));
 $test2 = trim(str_replace('.', '', $test));
 $sections = preg_split('/-{50,}s*r?n/s', $test2);
 $rows = preg_split('/r?n/', trim($sections[2]));

 $query_chunks = [];
 $query_data = [];
 


 foreach( $rows as $row )
{      
$query_chunks[] = "(?,?,?,?,?,?,?,?,?)";
$query_data = array_merge($query_data, preg_split("/s+/", trim($row)));
}
//die(print_r($query_data, true)); 
$query = "INSERT INTO ont_onu (frame_slot, pon, ont_id, mac_address, control_flag, run_state, config_state, match_state, description) VALUES "  . implode( ', ', $query_chunks );

$pdo_dbh = new PDO('mysql:host=localhost;dbname=midebe', 'iuzer', 'pausord');
$sth = $pdo_dbh->prepare( $query );
$sth->execute( $query_data );

die(print_r($query_data, true)); 
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement