嘿,我正在尝试使用 PHP SSH2 函数,我在 GitHub 上使用了一个类:Here但似乎没有按预期工作。
我正在尝试 cd进入一个目录,然后列出文件,但似乎忽略了 cd当我使用 ls命令它只显示根目录。
这是我执行正常 ls 的时候在我的根目录上:
drwx------ 6 root root 4096 Aug 12 18:23 .
drwxr-xr-x 22 root root 4096 Aug 11 08:28 ..
-rw------- 1 root root 2963 Aug 12 18:33 .bash_history
-rw-r--r-- 1 root root 3106 Oct 22 2015 .bashrc
drwx------ 3 root root 4096 Aug 11 21:05 .cache
drwxr-xr-x 3 root root 4096 Aug 12 18:21 .local
drwxr-xr-x 2 root root 4096 Aug 12 18:22 .nano
drwxr-xr-x 18 root root 4096 Aug 11 21:07 myOtherDirectory
-rw-r--r-- 1 root root 148 Aug 17 2015 .profile
-rw------- 1 root root 1024 Aug 12 18:27 .rnd
-rw-r--r-- 1 root root 252 Aug 12 18:28 .wget-hsts
然后我尝试访问
myOtherDirectory通过 PHP 使用 SSH
$ssh = new ssh("ip", "root", "pass");
$ssh("cd myOtherDirectory");
$result = $ssh("ls -la");
print_r($result);
请您参考如下方法:
SSH 协议(protocol)允许您与远程主机建立一个 SSH 连接(称为 session ),然后通过单个连接运行多个 channel 。单个 channel 可以表示交互式 session 、单个 SFTP 传输等。每个 channel 都独立于其他 channel 。
$ssh = new ssh("ip", "root", "pass");
$ssh("cd myOtherDirectory");
$result = $ssh("ls -la");
您在这里所做的是创建到远程主机的单个 SSH session ,然后创建两个 channel 。第一个 channel 运行
cd然后退出。第二个 channel 运行
ls然后退出。两个 channel 相互独立,所以
cd调用对
ls 的工作目录没有影响调用。
您想在单个 session 中运行这两个命令。假设远程系统正在使用
bash或类似的 shell 来运行命令,这应该可以工作:
$ssh = new ssh("ip", "root", "pass");
$result = $ssh("cd myOtherDirectory && ls -la");
或者,您可以在
ls 中指定正确的远程目录。命令:
$ssh = new ssh("ip", "root", "pass");
$result = $ssh("ls -la myOtherDirectory");




