Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting console data via Regex and PHP

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading