首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP telnet数组应答解析存储在mysql数据库中

PHP telnet数组应答解析存储在mysql数据库中
EN

Stack Overflow用户
提问于 2022-04-14 18:53:24
回答 2查看 144关注 0票数 -1

嗨,伙计们,我有一个小项目正在进行,这个项目要求我通过telnet发送一个cmd到一个设备上,我得到一个回复,它会把一个数组上的下水道弄出来。我需要解析这个数据,sói可以存储在mysql表中。

这是来自telnet的数据输出回复。

代码语言:javascript
复制
 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)# 
 )

基本上,我需要分离F/S 0/0 P=4 ONT ID = 1,2,3,4取决于第二cmd答复,这可以改变多达64个设备,mac CONTROL=设备MAC。xx:xx RUN =active Config= online,MATCH DESCRIPTION = success match

在mysql表中,我们有以下行

代码语言:javascript
复制
 FRAME SLOT = 0/0 
 PON = PON DEVICE Number
 ONT ID = ID of devices
 MAC CONTROL
 RUN
 CONFIG
 MATCH DESC

我有点迷茫,我试着把它过滤掉..但它没有正确地存储数据..。

编辑-1

好的,我可以爆炸数组并过滤细节,但是我现在被困在循环中了。它只是过滤了lad的id设备数据..。我试了一试,但它不起作用。下面附加了代码,并显示了它只过滤了最后一个ONT ID并输出结果。图-试验

代码语言:javascript
复制
/*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; 
                
            }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-14 21:20:52

嗨,伙计们迟了回复,但最终还是设法让mysql表上附加的工作代码上的代码工作更新,在telnet日志上出现了一些逃逸安全隐患,它们破坏了中间的输出。显然,我必须感谢所有参与回复和评论的每个人(@sammitch和@jared)的投入,他们也帮助了我,指出了正确的步骤.

$show_pon =“显示ont 1 all";

代码语言:javascript
复制
   $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)); 
票数 0
EN

Stack Overflow用户

发布于 2022-04-15 04:49:01

您应该将响应分解为部分,然后将第3部分分解为行。雷杰普斯是你的朋友

代码语言:javascript
复制
// Will replace numeric keys with these later
$fields = ["frame","slot","pon","mac","control-flag","run-state","config-state","match-state"];

// Data is a raw string
$sections = preg_split('/-{50,}\s*\r?\n/s', $data);

// I asume your rows are always on section with index 2
// If not, we could autodetect it, matching section with a "/^\d\/\d/" regexp
$rows = preg_split('/\r?\n/', trim($sections[2]));

$entries = [];
foreach( $rows as $row ){

  $entry = preg_split("/\s+/", trim($row));

  // Move values from numeric keys to string keys
  foreach( $fields as $i => $field ){
    $entry[ $field ] = $entry[ $i ];
    unset( $entry[$i] );
  }

  $entries[] = $entry;

}

print_r($entries);

下面的代码应该是通过PDO在MySql表中插入数据:

代码语言:javascript
复制
// Data is a raw string
$sections = preg_split('/-{50,}\s*\r?\n/s', $data);

// I asume your rows are always on section with index 2
// If not, we could autodetect it, matching section with a "/^\d\/\d/" regexp
$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)));

}

$query = 
  "INSERT INTO ont_onu (frame_slot, pon, ont_id, mac_address, control_flag, run_state, config_state, match_state) VALUES "
  . implode( ', ', $query_chunks ) ;

$pdo_dbh = new PDO('mysql:host=localhost;dbname=DATABASE_NAME', 'DATABASE_USER', 'DATABASE_PASSWORD');
$sth = $pdo_dbh->prepare( $query );
$sth->execute( $query_data );

当您运行mysqli而不是PDO时,您必须在遍历行时构造查询:

代码语言:javascript
复制
foreach( $rows as $row ){

  $query_chunks[] = '('.implode(', ', array_map(function( $value ) use ($mysqli) {

    return "'" . mysqli_real_escape_string($mysqli, $value) . "'";

  })).')';

}

$query = 
  "INSERT INTO ont_onu (frame_slot, pon, ont_id, mac_address, control_flag, run_state, config_state, match_state) VALUES "
  . implode( ', ', $query_chunks ) ;

mysqli_query($mysqli, $sql);  

或者调用bind_param

代码语言:javascript
复制
$mysqli_sth = mysqli_prepare($mysqli, $query);
$mysqli_sth->bind_param(
  str_repeat('s', count( $query_data )),
  ...$query_data
);

但我建议搬到PDO去。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71876395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档