ssh2://

ssh2://安全外殼協(xié)議 2

說明

ssh2.shell:// ssh2.exec:// ssh2.tunnel:// ssh2.sftp:// ssh2.scp:// (PECL)

注意: 該封裝器默認沒有激活
為了使用 ssh2.*:// 封裝協(xié)議, 你必須安裝來自 ? PECL? SSH2 擴展。

除了支持傳統(tǒng)的 URI 登錄信息,ssh2 封裝協(xié)議也支持通過 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

可選項

封裝協(xié)議概要
屬性 ssh2.shell ssh2.exec ssh2.tunnel ssh2.sftp ssh2.scp
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() No No No Yes No
支持 unlink() No No No Yes No
支持 rename() No No No Yes No
支持 mkdir() No No No Yes No
支持 rmdir() 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 需要設置的環(huán)境變量的關聯(lián)數(shù)組  
term 在分配一個 pty 時請求的終端類型  
term_width 在分配一個 pty 時請求的終端寬度  
term_height 在分配一個 pty 時請求的終端寬度高度  
term_units term_width 和 term_height 的單位 SSH2_TERM_UNIT_CHARS

范例

示例 #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');
?>

示例 #2 $session 變量必須保持可用!

為了使用 ssh2.*://$session 封裝協(xié)議, 必須保留 $session 資源變量。下面的代碼就不會有預期的效果:

<?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() 會關閉 session,因為 $connection_string 不保存對 $session 變量的引用,只是源自它的字符串轉換。當離開(像函數(shù))作用域隱性調(diào)用 unset() 時,也會發(fā)生這種情況。