• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • ssh2://

    Secure Shell 2

    说明

    ssh2.shell://ssh2.exec://ssh2.tunnel://ssh2.sftp://ssh2.scp://PHP 4.3.0 and up(PECL)

    Note:该封装器默认没有激活
    为了使用ssh2.*://封装协议,你必须安装来自» PECL的» SSH2扩展。

    除了支持传统的 URI 登录信息,ssh2 封装协议也支持通过 URL 的主机(host)部分来复用打开连接。

    用法

    • ssh2.shell://user:pass@example.com:22/xterm
    • ssh2.exec://user:pass@example.com:22/usr/local/bin/somecmd
    • ssh2.tunnel://user:pass@example.com:22/192.168.0.1:14
    • ssh2.sftp://user:pass@example.com:22/path/to/filename

    可选项

    封装协议概要
    属性ssh2.shellssh2.execssh2.tunnelssh2.sftpssh2.scp
    受allow_url_fopen影响YesYesYesYesYes
    允许读取YesYesYesYesYes
    允许写入YesYesYesYesNo
    允许追加NoNoNoYes(当服务器支持的时候)No
    允许同时读和写YesYesYesYesNo
    支持stat()NoNoNoYesNo
    支持unlink()NoNoNoYesNo
    支持rename()NoNoNoYesNo
    支持mkdir()NoNoNoYesNo
    支持rmdir()NoNoNoYesNo
    上下文选项(Context)
    名称用法默认
    session重复使用预连接的 ssh2 资源
    sftp重复使用预先分配的 sftp 资源
    methods密钥交换(key exchange)、主机密钥(hostkey)、cipher、压缩和 MAC 方法
    callbacks
    username以该用户名连接
    password使用的密码来进行密码验证
    pubkey_file用于验证的公钥(public key)文件
    privkey_file用于验证的私钥(private key)文件
    env需要设置的环境变量的关联数组
    term在分配一个 pty 时请求的终端类型
    term_width在分配一个 pty 时请求的终端宽度
    term_height在分配一个 pty 时请求的终端宽度高度
    term_unitsterm_width 和 term_height 的单位SSH2_TERM_UNIT_CHARS

    范例

    Example #1 从一个活动连接中打开字节流

    <?php
    $session = ssh2_connect('example.com', 22);
    ssh2_auth_pubkey_file($session, 'username', '/home/username/.ssh/id_rsa.pub',
                                                '/home/username/.ssh/id_rsa', 'secret');
    $stream = fopen("ssh2.tunnel://$session/remote.example.com:1234", 'r');
    ?>
    

    Example #2 This$sessionvariable must be kept available!

    In order to use thessh2.*://$sessionwrappers you must keep the$sessionresouce variable. The code below will not have the desired effect:

    <?php
    $session = ssh2_connect('example.com', 22);
    ssh2_auth_pubkey_file($session, 'username', '/home/username/.ssh/id_rsa.pub',
                                                '/home/username/.ssh/id_rsa', 'secret');
    $connection_string = "ssh2.sftp://$session/";
    unset($session);
    $stream = fopen($connection_string . "path/to/file", 'r');
    ?>
    

    unset()closes the session, because$connection_stringdoes not hold a reference to the$sessionvariable, just a string cast derived from it. This also happens when the unset() is implicit because of leaving scope(like in a function).

    Be aware that opendir is currently broken on sftp root directories, but you can work around it by appending a dot. See https://bugs.php.net/bug.php?id=64169 and http://stackoverflow.com/a/16238476/69173.
    The "password" context option can also be used to provide the passphrase for the keyfile supplied by "privkey_file" and "pubkey_file".
    Note this bug: https://bugs.php.net/bug.php?id=58573
    Encrypted keys may not work unless you build libssh2 against openssl. (It only worked for me on Debian Wheezy once I recompiled the library).
    Please beware of a PHP bug, noted by thomas at gielfeldt dot dk, that you must intval() the connection variable before putting it in the connection string :
    <?php
    $connection = ssh2_connect('shell.example.com', 22);
    ssh2_auth_password($connection, 'username', 'password');
    $sftp = ssh2_sftp($connection);
    // See: https://bugs.php.net/bug.php?id=73597
    $stream = fopen("ssh2.sftp://" . intval($sftp) . "/path/to/file", 'r');
    ?>
    
    <?php
    // Connect with public key.
    $session = ssh2_connect('example.com', 22);
    $result = ssh2_auth_pubkey_file($session, 'remote-username', '/home/local-username/.ssh/id_rsa.pub',
                                   '/home/local-username/.ssh/id_rsa', 
                                   'secret');
    // Setup sftp stream wrapper
    $sftp = ssh2_sftp($session);
    // See: https://bugs.php.net/bug.php?id=73597
    $connection_string = 'ssh2.sftp://' . intval($sftp);
    // List files in remote homedir.
    $i = new \RecursiveDirectoryIterator("$connection_string/home/remote-username");
    $r = new \RecursiveIteratorIterator($i);
    foreach ($r as $f) {
      print $f->getPathname() . "\n";
    }
    ?>
    

    上篇:phar://

    下篇:rar://