Using phpseclib with a custom shell wrapper

303 views Asked by At

I am using PHPSeclib to access server with dokku-alt installed:

http://dokku-alt.github.io/how-it-works.html

By following a typical example, I manage to send commands to a custom shell on my account:

    $ssh=$this->connect();
    echo trim($ssh->exec("version");

This is equivalent to

ssh [email protected] version

and works as expected. If I however try to execute a command which expects me to send data through STDIN, there is a problem. According to Net_SSH2 documentation, I need to write the data into SSH stream instead of using exec(). Unfortunately my next example does not work, because custom shell does not receive any argument and responds with help page:

    $ssh=$this->connect();
    $ssh->write("mysql mariadb:console myapp newdb\n");
    $ssh->write("show tables\n");
    $ssh->read('[prompt]');

The result of this is identical to

ssh [email protected]

which simply responds with help page.

How can I combine the "exec" functionality and still be able to write data? Something like this does not work either:

    $ssh=$this->connect();
    $ssh->exec("mysql mariadb:console myapp newdb");
    $ssh->write("show tables\n");
    $ssh->read('[prompt]');

Thank you.

1

There are 1 answers

1
neubert On BEST ANSWER

I think PTY mode is what you're looking for? eg.

$ssh->enablePTY(); 
$ssh->exec('mysql mariadb:console myapp newdb'); 
echo $ssh->read('mysql>'); 
$ssh->write("show tables\n");
echo $ssh->read('mysql>');

More info:

http://phpseclib.sourceforge.net/ssh/pty.html