本文介绍了phpseclib或ssh2 pecl扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天的帖子: https://stackoverflow.com/questions/14296006/phpseclib-sftp-端口号

好,所以昨天我开始用php学习SSH/SFTP.我搜索了一堆论坛帖子,并推测我需要下载phpseclib.

Ok, so yesterday I started learning about SSH / SFTP with php. I searched a bunch of forum posts and surmised that i needed to download the phpseclib.

对php相对较新,因此从php5开始,我不知道以前的php4不使用__constructor,因此上面的问题/帖子.

Being relatively new to php thus starting on php5 i was not aware of previous php4's non-use of the __constructor, hence the above question/post.

回答是矛盾的,但是对原始问题的回答有点偏离主题,这使我提出了一个我认为需要回答的问题,然后再继续:

The responses were conflicting, and a little off topic to the original Q however has delivered me to a question that I feel needs answering before i continue:

ssh2 pecl扩展名 phpseclib 哪个更好用?

这个问题: phpseclib vs libssh2 是相同的,但我现在觉得有点过时了2010年11月5日,17:37

This question: phpseclib vs libssh2 is the same but I feel a little outdated now as asked on Nov 5 '10 at 17:37

推荐答案

我说phpseclib.就个人而言,我认为您可以使用phpseclib获得更好的支持,并且API更好,但是主观原因也更少:

I say phpseclib. Personally, I think you get better support with phpseclib and that the API is better but there are less subjective reasons too:

更便携.

我试图在Ubuntu 12.04上安装libssh2-php,"sudo apt-get install libssh2-php"对我不起作用.即使这样做,也可能不会获得最新版本.因此,我必须自己编译libssh2和PECL扩展,这总是很麻烦,而且不是很多管理员愿意做的事情.

I tried to install libssh2-php on Ubuntu 12.04 and "sudo apt-get install libssh2-php" didn't work for me. And even if it did it likely wouldn't get the latest version. So I had to compile libssh2 and the PECL extension myself which is always a hassle and not something a lot of admins are going to be willing to do.

即使您愿意编译内容,也可以说您的硬盘驱动器发生故障,并且您必须重建服务器.如果您愿意编译libssh2,则可能还会编译其他东西.这意味着您不能只启动另一个框-您必须记住在旧框上所做的所有更改并重新应用它们.而且,如果您不记得他们怎么办?或者,如果其中一个尚未更新为可与另一个的最新版本一起使用,该怎么办?

And even if you are willing to compile stuff let's say your hard drive fails and you have to rebuild the server. If you're willing to compile libssh2 you'll have probably compiled other stuff too. Which means you can't just fire up another box - you have to remember all the changes you made on your old box and reapply them. And what if you don't remember them? Or what if one of them hasn't been updated to work with the latest version of another?

phpseclib除了PHP不需要任何其他东西.如果可用,它将使用mcrypt,gmp,bcmath或openssl,但如果不可用,也可以.它甚至不需要PHP5,尽管它确实支持它.

phpseclib, in contrast, doesn't require much of anything other than PHP. It'll use mcrypt, gmp, bcmath or openssl if they're available but if they're not, that's okay, too. And it doesn't even require PHP5, although it certainly supports it.

更好的公钥支持.

如何使用libssh2:

How you do it with libssh2:

<?php
$ssh = ssh2_connect('domain.tld');
ssh2_auth_pubkey_file($ssh, 'username', '/home/ubuntu/pubkey', '/home/ubuntu/privkey'/*, 'password'*/);

$stream = ssh2_exec($ssh, 'ls -la');
echo stream_get_contents($stream);

两者的格式也必须正确.如果您没有使用ssh-keygen来生成密钥,那么在转换它们时会很幸运.

Both have to be of the right format too. If you didn't use ssh-keygen to generate your keys good luck in converting them.

使用phpseclib:

With phpseclib:

<?php
include('Net/SSH2.php');
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey('...');

$ssh = new Net_SSH2('domain.tld');
$ssh->login('username', $rsa);
//$ssh->setPassword('password');

echo $ssh->exec('ls -la');

暂时忽略API,有几种明确的方法使phpseclib出现在此处:

Ignoring the API for the time being there are a few clear ways phpseclib comes out on top here:

  • phpseclib接受字符串-而不是文件路径.如果要创建文件,则可以执行file_get_contents.
  • phpseclib不需要公共密钥.大多数私钥格式都在其中嵌入了公钥.如果他们不... phpseclib也支持.
  • phpseclib几乎可以采用任何标准格式,从PKCS#1格式的密钥到PuTTY密钥,再到XML签名密钥

交互式外壳.

让我们尝试在远程系统上执行sudo.

Let's try to do sudo on the remote system.

使用phpseclib: http://phpseclib.sourceforge.net/ssh/examples.html#password,sudo

With phpseclib:http://phpseclib.sourceforge.net/ssh/examples.html#password,sudo,

使用libssh2吗?我没有线索.我最好的猜测(无效):

With libssh2? I have no clue. My best guess (doesn't work):

<?php
$ssh = ssh2_connect('domain.tld');
ssh2_auth_password($ssh, 'username', 'password');

$shell = ssh2_shell($ssh);
echo fread($shell, 1024*1024);
fwrite($shell, "sudo ls -la\n");
$output = fread($shell, 1024*1024);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
    fwrite($shell, "password\n");
}
echo fread($shell, 1024*1024);

我也无法与libssh2一起工作,但是它与phpseclib可以很好地工作:

I can't get top to work with libssh2 either but it works fine with phpseclib:

http://phpseclib.sourceforge.net/ssh/examples.html#password,top

诊断问题

为什么top或sudo无法正常工作?在phpseclib上,您可以获得日志:

Why didn't top or sudo work? On phpseclib you can get logs:

http://phpseclib.sourceforge.net/ssh/examples.html#password,oneoff ,记录

它们看起来像这样:

http://phpseclib.sourceforge.net/ssh/log.txt

您也可以执行print_r($ssh->getErrors())echo $ssh->getLastError().

更改目录

http://php.net/ssh2 上没有看到任何cd或chdir函数. phpseclib,但是有它-Net_SFTP::chdir(...)

I don't see any cd or chdir functions at http://php.net/ssh2 . phpseclib, however, has it - Net_SFTP::chdir(...)

速度

libssh2:

<?php
$ssh = ssh2_connect('domain.tld');
ssh2_auth_password($ssh, 'username', 'password');

$start = microtime(true);
$sftp = ssh2_sftp($ssh);

$fp = fopen('ssh2.sftp://'.$sftp.'/home/username/1mb', 'w');

fwrite($fp, str_repeat('a', 1024 * 1024));
$elapsed = microtime(true) - $start;

echo "took $elapsed seconds";

25.71秒.

phpseclib:

phpseclib:

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('domain.tld');
$sftp->login('username', 'password');

$start = microtime(true);
$sftp->put('1mb', str_repeat('a', 1024*1024));
$elapsed = microtime(true) - $start;

echo "took $elapsed seconds";

11.70秒.

因此phpseclib的速度是后者的两倍以上.

So phpseclib is more than twice as fast.

这篇关于phpseclib或ssh2 pecl扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 08:49