Home > database >  Getting console data via Regex and PHP
Getting console data via Regex and PHP

Time:06-04

I want to get some data of an application via SSH output;

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

Regex: '/(\bAvailable status: \b)(?!.*\1)/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
//...
    }
}

I want to add it to the options in the form structure, but how can I get the status options from the SSH result?

CodePudding user response:

Just a little tweak to the RegEx and this seems to work fine. The following also filters the results to trim excess whitespace from each status code using 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));

Which yields:

Array
(
    [0] => shutdown
    [1] => reset
    [2] => reboot
    [3] => rightmove
    [4] => leftmove
    [5] => rebootgps
    [6] => rebootping
    [7] => rebootlights
    [8] => uwpring
)
  • Related