简体   繁体   中英

Using phpseclib with a custom shell wrapper

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 dokku@my.node.org 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 dokku@my.node.org

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM