Question

I am attempting to install an application using phpseclib (using apt-get) and want to handle the different output results I could receive... however I am unsure how to get it working. I currently have the following:

$first = $ssh->read('root@DMZ-Server:~#');
$ssh->write("sudo apt-get install emacs\n");
echo "<br />";
echo "sudo apt-get install emacs";
echo str_repeat(' ',1024*64);
if ($unexpected_output = $ssh->read('root@DMZ-Server:~#')){
  echo "<pre>$unexpected_output</pre>";
  echo str_repeat(' ',1024*64);
}   elseif ($continue = $ssh->read('Do you want to continue [Y/n]?')){
    echo "<pre>$continue</pre>";
    echo str_repeat(' ',1024*64);
    $ssh->write("Y\n");
    $continue_end = $ssh->read('root@DMZ-Server:~#');
    echo "<pre>$continue_end</pre>";
    echo str_repeat(' ',1024*64);
} 

This if loop only works with the first "if"... That is if there is unexpected output (You go back to the shell without being asked "Do you want to continue", which means already install, etc) then this script will work just fine.

But if the application is not installed it will never go to the elseif loop. I'm pretty sure the reason is because the first ssh->read will continue until php times out, waiting for that result... how do I write it so that it will look at both options simultaneously, and pick the correct one.

I am thinking about using CASE for this, but am not sure how to write that...

There also might be a better way to do this using phpseclib... but not sure how to do that.

Thanks!

Was it helpful?

Solution

Why don't you instead change how you call apt-get to apt-get install -y emacs so it doesn't prompt yes or no? You may also want to use -q so it doesn't print progress.

See apt-get(8)

The problem with your solution is that $ssh->read wait until the given pattern is found.

OTHER TIPS

You could also do $ssh->setTimeout(10) or whatever and then when the connection times out do $ssh->getLog() to see what's actually being sent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top