通过 Regex 和 PHP 获取控制台数据
Getting console data via Regex and PHP
我想通过 SSH 输出获取应用程序的一些数据;
root@serve:~# eshtr --statuscheck true --trackerid 06897ea6-ed4d-43c4-bf94-ec8643628943
[ESHTR][INTERNALSERVER][StatusChecker] Status: Standby_Mode
Available status: shutdown, reset, reboot, rightmove, leftmove, rebootgps, rebootping, rebootlights, uwpring
Tracker ID: 06897ea6-ed4d-43c4-bf94-ec8643628943
正则表达式:'/(\bAvailable status: \b)(?!.*)/m';
preg_match_all($pattern['availableStatus'], $sshResult, $matches, PREG_SET_ORDER, 0);
if (isset($matches[0])) {
foreach ($matches[0] as $match) {
$options = explode(',' $match);
// shutdown
// reset
// reboot
// rightmove
//...
}
}
我想将其添加到表单结构中的 options
,但如何从 SSH 结果中获取状态选项?
只需对 RegEx 稍作调整,似乎就可以正常工作。以下还使用 array_walk
将结果过滤为每个状态代码的 trim 多余空格
$response='root@serve:~# eshtr --statuscheck true --trackerid 06897ea6-ed4d-43c4-bf94-ec8643628943
[ESHTR][INTERNALSERVER][StatusChecker] Status: Standby_Mode
Available status: shutdown, reset, reboot, rightmove, leftmove, rebootgps, rebootping, rebootlights, uwpring
Tracker ID: 06897ea6-ed4d-43c4-bf94-ec8643628943';
$pttn='/(Available status: )(.*)/m';
preg_match( $pttn, $response, $results );
$data=explode( ',', $results[ count( $results )-1 ] );
array_walk($data,function( &$item ){
$item=trim( $item );
});
printf('<pre>%s</pre>',print_r($data,true));
产生:
Array
(
[0] => shutdown
[1] => reset
[2] => reboot
[3] => rightmove
[4] => leftmove
[5] => rebootgps
[6] => rebootping
[7] => rebootlights
[8] => uwpring
)
我想通过 SSH 输出获取应用程序的一些数据;
root@serve:~# eshtr --statuscheck true --trackerid 06897ea6-ed4d-43c4-bf94-ec8643628943
[ESHTR][INTERNALSERVER][StatusChecker] Status: Standby_Mode
Available status: shutdown, reset, reboot, rightmove, leftmove, rebootgps, rebootping, rebootlights, uwpring
Tracker ID: 06897ea6-ed4d-43c4-bf94-ec8643628943
正则表达式:'/(\bAvailable status: \b)(?!.*)/m';
preg_match_all($pattern['availableStatus'], $sshResult, $matches, PREG_SET_ORDER, 0);
if (isset($matches[0])) {
foreach ($matches[0] as $match) {
$options = explode(',' $match);
// shutdown
// reset
// reboot
// rightmove
//...
}
}
我想将其添加到表单结构中的 options
,但如何从 SSH 结果中获取状态选项?
只需对 RegEx 稍作调整,似乎就可以正常工作。以下还使用 array_walk
$response='root@serve:~# eshtr --statuscheck true --trackerid 06897ea6-ed4d-43c4-bf94-ec8643628943
[ESHTR][INTERNALSERVER][StatusChecker] Status: Standby_Mode
Available status: shutdown, reset, reboot, rightmove, leftmove, rebootgps, rebootping, rebootlights, uwpring
Tracker ID: 06897ea6-ed4d-43c4-bf94-ec8643628943';
$pttn='/(Available status: )(.*)/m';
preg_match( $pttn, $response, $results );
$data=explode( ',', $results[ count( $results )-1 ] );
array_walk($data,function( &$item ){
$item=trim( $item );
});
printf('<pre>%s</pre>',print_r($data,true));
产生:
Array
(
[0] => shutdown
[1] => reset
[2] => reboot
[3] => rightmove
[4] => leftmove
[5] => rebootgps
[6] => rebootping
[7] => rebootlights
[8] => uwpring
)