ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

为什么通过Putty的SSH命令与PHP的phpseclib不同?

2019-10-07 16:39:05  阅读:318  来源: 互联网

标签:php ssh putty phpseclib


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

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

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

cd /www/
ls -la    #outputs contents of /www

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

<?php
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,但没有区别.

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

解决方法:

From the documentation:

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有