php用putty安装吗,为什么通过Putty的SSH命令与PHP的phpseclib不同?

我正在编写一个脚本来自动部署从我的Windows开发PC到共享托管服务器.

根据我是通过Putty还是PHP(都在我的PC上运行)执行命令,我得到的结果会有所不同.

在putty中,当我通过SSH登录服务器时,我可以运行如下命令:

cd /www/

ls -la #outputs contents of /www

但是当我通过PHP使用phpseclib执行此操作时,如下所示,任何cd命令都被完全忽略:

require_once __DIR__.'/vendor/autoload.php';

use phpseclib\Net\SSH2;

$ssh = new SSH2('ssh.mydomain.com');

if (!$ssh->login('mydomain.com', 'mypassword')) {

trigger_error("Login Failed", E_ERROR);

}

echo $ssh->exec('pwd');

$ssh->exec('cd /www/');

echo $ssh->exec('pwd'); // unchanged

echo $ssh->exec('ls -la'); // does NOT output contents of /www/

echo $ssh->exec('ls /www/ -la'); // DOES output contents of /www/

在上面指定绝对URL是可接受的解决方法.但是,以下是一个主要问题.

如果我上传一个文件,stuff.zip到/ www /然后尝试提取它,以下通过Putty工作:

unzip /www/stuff.zip -d /www/

但是如果我通过PHP尝试这个:

echo $ssh->exec('unzip /www/stuff.zip -d /www/');

我收到错误:

unzip: cannot find or open /www/stuff.zip, /www/stuff.zip.zip or /www/stuff.zip.ZIP.

我试过chmod 777 /www/stuff.zip,但没有区别.

我该如何诊断/解决这个问题?

解决方法:

Successive calls to exec()

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. 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.

我看到两个选择.选项1:

尝试使用read()和write()函数的interactive shell examples,这对我来说非常难看.

选项2:

将您的脚本编写为服务器上的shell脚本,并使用单个$ssh-> exec()调用来调用脚本.

标签:php,ssh,putty,phpseclib

来源: https://codeday.me/bug/20191007/1868127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值