PHP telnet数组回复解析存储在mysql数据库中



大家好,我有一个小项目正在进行,这个项目需要我通过telnet向设备发送cmd,我得到了回复,它在数组上发出了答案。。我需要解析我可以存储在mysql表中的这些数据。。

这是来自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)# 
)

基本上,我需要分离F/S 0/0 p=4 ONT ID=1,2,3,4取决于cmd回复,这可能会变化多达64个设备,MAC CONTROL=设备MAC。xx:xx:xx:xx:xx运行=活动配置=在线,匹配描述=成功匹配

在mysql表上,我们有以下行

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

我有点迷路了,我已经尽力过滤了。。但它没有正确存储数据。。。

edit-1

好的,所以我可以分解数组并过滤细节,但我现在陷入了循环。。它只是过滤lad ONT id设备数据。。我试了一下前臂,但不起作用。。下面附上的代码,并显示它只过滤最后一个ONT ID并输出结果。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; 

}

您应该将响应分成几个部分,然后将第3部分分成几行。Regexps是你的朋友:

// 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表中插入数据:

// 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时,您必须在遍历行时构造查询:

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:


$mysqli_sth = mysqli_prepare($mysqli, $query);
$mysqli_sth->bind_param(
str_repeat('s', count( $query_data )),
...$query_data
);

但我建议转到PDO。

大家好,回复晚了,但最终在mysql表上获得了代码工作更新。附加的工作代码,telnet日志上有一些escape安全特征,它们破坏了输出在中间。显然,我必须感谢所有参与回复和评论的人(@sammitch和@jared(对我的帮助,并为我指出了正确的步骤。。。

$show_pon[0] = "show ont info 1 all"; 

最新更新