Return a JSON Array with shell script to use in PHP

Asked

Viewed 272 times

0

Good evening, you guys, I’ve been hitting myself with something for a few days. I am developing an application to connect via SSH in an OLT with shell script and return the "Shows" to a web frontend.

I have the following page: inserir a descrição da imagem aqui

I have a shell script file that connects via SSH in the OLT command below:

show interface gpon 1/1/1 onu

And play the return of the command to the variable list where I treat the result giving some sed and awk.

list=`showOnu | sed '1,2d' | awk '{print $1 "," $2 "," $3 "," $5}'`

    echo $list

    echo "<table class='table table-hover'>"
    echo "  <thead>"
    echo "    <tr>"
    echo "      <th scope='col'>ID</th>"
    echo "      <th scope='col'>Serial Number</th>"
    echo "      <th scope='col'>Oper State</th>"
    echo "      <th scope='col'>Name</th>"
    echo "      <th scope='col'></th>"
    echo "    </tr>"
    echo "  </thead>"
    echo "  <tbody>"

    for I in $list
    do
            idonu=`echo $I | cut -d, -f1`
            serialnumber=`echo $I | cut -d, -f2`
            name=`echo $I | cut -d, -f3`
            status=`echo $I | cut -d, -f4`
            echo "    <tr>"
            echo "      <th scope='row'>$idonu</th>"
            echo "      <td>$serialnumber</td>"
            echo "      <td>$name</td>"
            echo "      <td>$status</td>"
            echo "      <td class='text-right'><a href='onu_edit.php?oltid=1&oltinterface=1' class='btn btn-sm btn-info'>Editar</a></td>"
            echo "    </tr>"
    done
    echo "  </tbody>"
    echo "</table>"

Inside the script I adjust the html to create the result.

And I use this in PHP to call the script on the page:

<?php 
$a = popen('./conectOlt -f interfaceOnuList -a '.$olt['ip'].' -i '.$slot.' -e '.$olt['id'].'', 'r'); 
while($b = fgets($a, 2048)) { 
echo $b; 
ob_flush();flush(); 
} 
pclose($a); ?>

And generates the result that I have in the first image, until there everything right, this working well.

But I was thinking here, there would be no way in the place to create the <table> in the shell script create a json and return only json to php and work with it creating the table directly in php.

Basically what I want is to create a json with arrey and maybe use a foreach in php to create each line with the information.

1 answer

1


Friend, you can create your entire JSON as a string, and in php you get that string (as you already do with the table) but instead of giving one echo in it you use the method json_decode to cover this string in a JSON object, then you can work with it normally.
Take this example:

<?php
$json = '{
    "alunos": [{
        "nome": "Joao",
        "idade": 18
    }, {
        "nome": "Maria",
        "idade": 20
    }, {
        "nome": "Claudia",
        "idade": 30
    }]
}';
$jsonObj = json_decode($json,TRUE);
print_r($jsonObj["alunos"]);

?>

You can test the example above here

  • Bro, I’m sorry it took so long....

  • @Giovanicórdoba, I’m glad it worked out! Note: If the answer helped you solve the problem please consider marking it as correct to help others with a question similar to yours!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.