ssh2_exec()不会使用"cd"更改目录. [英] ssh2_exec() wont change directory with "cd"

查看:447
本文介绍了ssh2_exec()不会使用"cd"更改目录.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ssh_exec()拒绝执行"cd"命令时遇到问题.

I have a problem with ssh_exec() refusing to execute the "cd" command.

如果我直接登录服务器并执行命令,它可以正常工作,因此我认为问题出在我的命令上.

If I log into the server directly and execute the command, it works fine, so I don't think the problem is with my command.

我的代码如下:

$str = ssh2_exec($sshStream, 'cp /var/www/compressed.tar.gz /var/www/vhosts/demo-domain1.com/httpdocs/');
$errstr = ssh2_fetch_stream($str, SSH2_STREAM_STDERR);
stream_set_blocking($str, true);
stream_set_blocking($errstr, true);
echo "Output: " . stream_get_contents($str);
echo "Error: " . stream_get_contents($errstr);

$str = ssh2_exec($sshStream, 'cd /var/www/vhosts/demo-domain1.com/httpdocs/');
$errstr = ssh2_fetch_stream($str, SSH2_STREAM_STDERR);
stream_set_blocking($str, true);
stream_set_blocking($errstr, true);
echo "Output: " . stream_get_contents($str);
echo "Error: " . stream_get_contents($errstr);

$str = ssh2_exec($sshStream, 'tar xzf c-class.tar.gz');
$errstr = ssh2_fetch_stream($str, SSH2_STREAM_STDERR);
stream_set_blocking($str, true);
stream_set_blocking($errstr, true);
echo "Output: " . stream_get_contents($str);
echo "Error: " . stream_get_contents($errstr);

我以root用户身份登录.

I am logged in as root.

第一个命令正确运行,并将文件复制到该位置.第二个命令不执行,但不输出任何错误.第三个命令显示错误(显然是因为上一个cd命令不起作用).

The first command runs correctly and copies the file to the location. The second command doesn't execute, but outputs no errors. The third command displays an error (obviously as the previous cd command doesn't work).

我知道它并没有改变目录,因为当我执行"pwd"时,它返回说它仍然在根目录中.

I know it hasn't changed dirs, as when I execute the "pwd", it returns saying it is in the root dir still.

如前所述,如果我从shell运行命令,它们可以很好地执行,所以我99.9%确信我的语法正确.

As mentioned before, if I run the commands from shell, they execute fine, so I'm 99.9% sure my syntax is correct.

这是1& 1提供的专用服务器,运行CentOS和Plesk 9.

This is a dedicated server provided by 1&1, running CentOS and Plesk 9.

推荐答案

要执行ssh2_exec(),PHP启动一个进程,该进程将执行在远程服务器上执行的ssh.

To execute ssh2_exec() PHP starts a process that will perform a ssh executed on the remote server.

远程创建的shell进程具有其自己的环境,包括当前的工作目录.
cd命令将更改由第二个命令启动的shell的工作目录.

The shell process created remotely has its own environment, including the current working directory.
The cd command will change the working directory of the shell that was started by your second command.

第二条命令结束时,shell随之死亡.连同工作目录信息.

When that second command ends, the shell dies with it. Along with the working dir information.

换句话说,在执行命令 n + 1 时将不会记住命令 n 外壳环境.

In other terms, command n shell environment will not be remembered during the execution of command n+1.

如果您希望Shell命令在相互依赖的环境下运行,则应将多个命令放在唯一的ssh2_exec中,例如

If you want the shell commands to be working while depending on each other in terms of environment, you should put several commands in a unique ssh2_exec like

 $str = ssh2_exec($sshStream, 'cd /var/www/vhosts/demo-domain1.com/httpdocs/;'
                            . 'tar xzf c-class.tar.gz');

 $errstr = ssh2_fetch_stream($str, SSH2_STREAM_STDERR);
 stream_set_blocking($str, true);
 stream_set_blocking($errstr, true);
 echo "Output: " . stream_get_contents($str);
 echo "Error: " . stream_get_contents($errstr);

这篇关于ssh2_exec()不会使用"cd"更改目录.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆