ssh2_exec()不更改目录:( [英] ssh2_exec() wont change directory :(

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

问题描述

使用ssh_exec()拒绝执行cd命令时遇到一个噩梦。

Having a nightmare of a problem with ssh_exec() refusing the 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).

我知道它没有改变dir,执行pwd,它返回说它在根目录中。

I know it hasn't changed dir's, 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的专用服务器。

Some additional info, though probably shouldn't be nessecary. This is a dedicated server provided by 1&1, running CentOS and Plesk 9.

推荐答案

c $ c> ssh2_exec() PHP启动一个将在远程服务器上执行 ssh 的进程。

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

远程创建的shell进程具有自己的环境,包括当前工作目录。

cd 命令将更改

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会死机。 shell 用它。

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

另外,在执行命令 n时,命令 n shell环境不会被记住+1

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

如果您希望shell命令在环境方面依赖于彼此工作,您应该将几个命令放在唯一 ssh2_exec like

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()不更改目录:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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