1

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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh, okay, I must admit I didn't know about that option... I will use that, although I wish there was a way to use a CASE statement so I could search for "emacs is already the latest version" or "package not found", etc so the user could get an exact error msg, but this solves my immediate problem, thanks.
If you use my method you can then use $ssh->exec and grep the text it outputs for latest version
Oh great! That was helpful, I just got that working using stristr to search $ssh->exec output... works well, thank you.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.