简体   繁体   中英

PHP SSH Commands Using phpseclib

I am using http://phpseclib.sourceforge.net/ to connect to my SSH and run some commands for my node server.

Its working perfectly using:

echo "Restart...<br>";
echo "<pre>";
echo $ssh->exec('forever restartall');
echo "</pre>";

However, as soon as I use the following code it doesn't display anything or run anything:

echo "Starting...<br>";
echo "<pre>";
echo $ssh->exec('cd /var/www/html/game-api');
echo $ssh->exec('forever start server.js > stdout.txt 2> stderr.txt &');
echo "</pre>";

The command works perfectly if I run it direct from the terminal.

Any idea?

Quoting http://phpseclib.sourceforge.net/ssh/examples.html#chdir ,

 echo $ssh->exec('pwd'); // outputs /home/username $ssh->exec('cd /'); echo $ssh->exec('pwd'); // (despite the previous command) outputs /home/username 

Successive calls to exec()

If done on an interactive shell, the output you'd receive for the first pwd would (depending on how your system is setup) be different than the output of the second pwd. The above code snippet, however, will yield two identical lines.

The reason for this is that any "state changes" you make to the one-time shell are gone once the exec() has been ran and the channel has been deleted.

You can workaround this on Linux by doing $ssh->exec('cd /; pwd')

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