PHP telnet 数组回复解析以存储在 mysql 数据库中
PHP telnet array reply parsing to store on mysql database
大家好,我有一个正在进行的小项目,这个项目需要我通过 telnet 向设备发送一个 cmd,我得到了一个回复,它通过数组中的答案..我需要解析这个数据所以我可以存储在 mysql table..
这是来自 telnet 命令的数据输出回复..
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 运行 =active Config= online, MATCH DESCRIPTION = success match
在 mysql table 上我们有以下行
FRAME SLOT = 0/0
PON = PON DEVICE Number
ONT ID = ID of devices
MAC CONTROL
RUN
CONFIG
MATCH DESC
我有点迷路了我已经尝试用 foreach 来过滤它..但是它没有正确存储数据...
编辑-1
好的,所以我可以展开数组并过滤细节,但我现在卡在循环中了..它只是过滤了小伙子的 ONT id 设备数据..我尝试了一个 foreach 但它不起作用..下面附上代码并想象它只过滤最后一个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 部分分成几行。正则表达式是你的朋友:
// 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 table 中插入数据:
// 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。
大家好,我有一个正在进行的小项目,这个项目需要我通过 telnet 向设备发送一个 cmd,我得到了一个回复,它通过数组中的答案..我需要解析这个数据所以我可以存储在 mysql table..
这是来自 telnet 命令的数据输出回复..
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 运行 =active Config= online, MATCH DESCRIPTION = success match
在 mysql table 上我们有以下行
FRAME SLOT = 0/0
PON = PON DEVICE Number
ONT ID = ID of devices
MAC CONTROL
RUN
CONFIG
MATCH DESC
我有点迷路了我已经尝试用 foreach 来过滤它..但是它没有正确存储数据...
编辑-1
好的,所以我可以展开数组并过滤细节,但我现在卡在循环中了..它只是过滤了小伙子的 ONT id 设备数据..我尝试了一个 foreach 但它不起作用..下面附上代码并想象它只过滤最后一个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 部分分成几行。正则表达式是你的朋友:
// 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 table 中插入数据:
// 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。