简体   繁体   English

使用phpseclib的PHP SSH命令

[英]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. 我正在使用http://phpseclib.sourceforge.net/连接到SSH并为节点服务器运行一些命令。

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 , 引用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() 连续调用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. 如果在交互式Shell上完成,则第一个密码的输出(取决于系统的设置)将与第二个密码的输出不同。 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. 原因是一旦运行exec()并删除通道后,您对一次性shell所做的任何“状态更改”都将消失。

You can workaround this on Linux by doing $ssh->exec('cd /; pwd') 您可以在Linux上通过执行$ssh->exec('cd /; pwd')解决此问题$ssh->exec('cd /; pwd')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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