沃梦达 / 编程问答 / php问题 / 正文

ssh2_exec() 不会用“cd"改变目录

ssh2_exec() wont change directory with quot;cdquot;(ssh2_exec() 不会用“cd改变目录)

本文介绍了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的shell环境在命令N+1的执行过程中不会被记住.

In other terms, the shell environment of the command N 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"改变目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ssh2_exec() 不会用“cd"改变目录

基础教程推荐