企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# [ssh2://](https://www.php.net/manual/zh/wrappers.ssh2.php)—Secure Shell 2 安全壳2 **Note**:**该封装器默认没有激活** 为了使用ssh2.\*://封装协议, 你必须安装来自[» PECL](https://pecl.php.net/)的[» SSH2](https://pecl.php.net/package/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.shell | ssh2.exec | ssh2.tunnel | ssh2.sftp | ssh2.scp | | --- | --- | --- | --- | --- | --- | | 受[allow\_url\_fopen](https://www.php.net/manual/zh/filesystem.configuration.php#ini.allow-url-fopen)影响 | Yes | Yes | Yes | Yes | Yes | | 允许读取 | Yes | Yes | Yes | Yes | Yes | | 允许写入 | Yes | Yes | Yes | Yes | No | | 允许追加 | No | No | No | Yes(当服务器支持的时候) | No | | 允许同时读和写 | Yes | Yes | Yes | Yes | No | | 支持[stat()](https://www.php.net/manual/zh/function.stat.php) | No | No | No | Yes | No | | 支持[unlink()](https://www.php.net/manual/zh/function.unlink.php) | No | No | No | Yes | No | | 支持[rename()](https://www.php.net/manual/zh/function.rename.php) | No | No | No | Yes | No | | 支持[mkdir()](https://www.php.net/manual/zh/function.mkdir.php) | No | No | No | Yes | No | | 支持[rmdir()](https://www.php.net/manual/zh/function.rmdir.php) | No | No | No | Yes | No | **上下文选项(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\_units* | term\_width 和 term\_height 的单位 | **`SSH2_TERM_UNIT_CHARS`** | ``` $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'); ```