-
PHP ssh2_exec Streams
Hi everyone,
I've been working on a project which connect through SSH to make a certain command and then get the output, this needs to be 3 times to get 3 differents informations which i get from SNMP.
I have tested my command, and manually they all work.
It all works for the first loop, i am able to get the data out of the command, sadly for the 2 others in the loop they are empty, it's like no value has been attached to the variable.
But I tested that when I change $ServerCommandRadioSignal to $ServerCommandRadioModel i get the same data as the first loop when $i == 0..
I am out of options, I have try unsetting streams or closing the variables SSH2_Exec, but none work.. Does someone have an idea?
for($i = 0; $i < 3; $i++) {
//echo $i;
if($i == 0) {
$stream0 = ssh2_exec($sshconn, $ServerCommandRadioModel);
stream_set_blocking($stream0, true);
$stream0_out = ssh2_fetch_stream($stream0, SSH2_STREAM_STDIO);
$UserRadioModel = stream_get_contents($stream0_out);
echo $UserRadioModel;
fclose($stream0);
fclose($stream0_out);
}
if($i == 1) {
$stream1 = ssh2_exec($sshconn, $ServerCommandRadioSignal);
stream_set_blocking($stream1, true);
$stream1_out = ssh2_fetch_stream($stream1, SSH2_STREAM_STDIO);
$UserRadioSignal = stream_get_contents($stream1_out);
echo $UserRadioSignal;
fclose($stream1);
fclose($stream1_out);
}
if($i == 2) {
$stream2 = ssh2_exec($sshconn, $ServerCommandRadioSSID);
stream_set_blocking($stream2, true);
$stream2_out = ssh2_fetch_stream($stream2, SSH2_STREAM_STDIO);
$UserRadioSSID = stream_get_contents($stream2_out);
echo $UserRadioSSID;
fclose($stream2);
fclose($stream2_out);
}
}
When the script runs these are the outcome.
1st Loop = "somedata1"
2nd Loop = nothing
3rd Loop = nothing
-
Please close this ticket, I have found the solution:
$ServerCommands = [$ServerCommandRadioModel, $ServerCommandRadioSignal, $ServerCommandRadioSSID];
$UserRadioOutput = [];
$i = 0;
foreach($ServerCommands as $ServerUniqueCommand) {
$stream = ssh2_exec($sshconn, $ServerUniqueCommand);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
if($i == 0) {
$UserRadioOutput[0] = stream_get_contents($stream_out);
echo $UserRadioOutput[0];
} elseif($i == 1) {
$UserRadioOutput[1] = stream_get_contents($stream_out);
echo $UserRadioOutput[1];
} elseif($i == 2) {
$UserRadioOutput[2] = stream_get_contents($stream_out);
echo $UserRadioOutput[2];
}
//echo $i;
$i++;
}
ssh2_exec($sshconn, 'exit');
unset($sshconn);