简体   繁体   English

无法使用 phpseclib 库更改目录

[英]Can't change directory using phpseclib library

I want to change directory in PHP with phpseclib library but it's doesn't works.我想使用 phpseclib 库更改 PHP 中的目录,但它不起作用。

$ssh = new Net_SSH2('localhost');
if (!$ssh->login('root', 'pass')) {
    exit('Failed to connect!');
}
$ssh->exec('cd /home/');
echo $ssh->exec('ls');

Why this is always output only root directory files/folders... and don't change the directory to home?为什么这总是 output 仅根目录文件/文件夹......并且不将目录更改为主目录?

If you read the documentation , it states:如果您阅读文档,它会指出:

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 上完成,那么您收到的第一个密码 output 将(取决于您的系统设置方式)与第二个密码的 Z78E6221F6393D1356681DB398F14CE6 不同。 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')您可以通过 $ssh->exec('cd /; pwd') 在 Linux 上解决此问题

So you need to instead put both commands in the same exec:因此,您需要将两个命令放在同一个 exec 中:

$ssh->exec('cd /home/; ls');

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

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