2013-01-13 14:05
浏览 82

phpseclib或ssh2 pecl扩展名

My post from yesterday:

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.

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:

Which is better to use, ssh2 pecl extension OR phpseclib?

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

图片转代码服务由CSDN问答提供 功能建议


好的,昨天我开始学习SSH / SFTP与PHP。 我搜索了一堆论坛帖子,并推测我需要下载phpseclib。

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

这些回复是相互矛盾的,然而对原始问题有点偏离主题却让我想到了一个我觉得在继续之前需要回答的问题: / p>

哪个更好用, ssh2 pecl extension phpseclib

这个问题: phpseclib vs libssh2 是一样的,但我觉得现在有点过时,正如2010年11月5日在17日被问到的那样 :37

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douwengzao5790 2013-01-13 17:04

    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:

    More portable.

    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.

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

    Better public key support.

    How you do it with libssh2:

    $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);

    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.

    With phpseclib:

    $rsa = new Crypt_RSA();
    $ssh = new Net_SSH2('domain.tld');
    $ssh->login('username', $rsa);
    echo $ssh->exec('ls -la');

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

    • phpseclib takes in strings - not file paths. If you want to do a file you can do file_get_contents.
    • phpseclib doesn't require a public key. Most private key formats have the public key embedded within them. And if they don't... phpseclib supports that too.
    • phpseclib can take in pretty much any standardized format, from PKCS#1 formatted keys, to PuTTY keys, to XML Signature keys

    Interactive shell.

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

    With phpseclib:,sudo,

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

    $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
    $output = fread($shell, 1024*1024);
    echo $output;
    if (preg_match('#[pP]assword[^:]*:#', $output)) {
        fwrite($shell, "password
    echo fread($shell, 1024*1024);

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

    Diagnosing problems

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

    They look like this:

    You can also do print_r($ssh->getErrors()) or echo $ssh->getLastError().

    Changing Directories

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



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


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

    So phpseclib is more than twice as fast.

    打赏 评论

相关推荐 更多相似问题