2024 PHP Conference Japan

ssh2://

ssh2://Secure Shell 2

說明

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

注意此包裝器預設並未啟用
為了使用 ssh2.*:// 包裝器,必須安裝從 » PECL 取得的 » SSH2 擴充套件。

除了接受傳統的 URI 登入詳細資訊外,ssh2 包裝器也會透過在 URL 的主機部分傳遞連線資源來重複使用已開啟的連線。

用法

  • 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 限制
允許讀取
允許寫入
允許附加 是(當伺服器支援時)
允許同時讀寫
支援 stat()
支援 unlink()
支援 rename()
支援 mkdir()
支援 rmdir()

上下文選項
名稱 用法 預設值
session 要重複使用的已連線 ssh2 資源  
sftp 要重複使用的已預先配置的 sftp 資源  
methods 要使用的金鑰交換、主機金鑰、加密、壓縮和 MAC 方法  
callbacks    
username 連線的使用者名稱  
password 使用密碼驗證的密碼  
pubkey_file 用於驗證的公鑰檔案名稱  
privkey_file 用於驗證的私鑰檔案名稱  
env 要設定的環境變數關聯陣列  
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 包裝器,$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');
?>

由於 `$connection_string` 並未持有 `$session` 變數的參考,僅儲存了從它轉換而來的字串,因此 `unset()` 會關閉連線。當因為離開作用域(例如在函式中)而隱式呼叫 `unset()` 時,也會發生這種情況。

新增註解

使用者貢獻的註解 4 則註解

8
exptom
11 年前
「password」上下文選項也可以用於提供由「privkey_file」和「pubkey_file」提供的金鑰檔案的通行片語。

請注意此錯誤:https://bugs.php.net/bug.php?id=58573
除非您針對 OpenSSL 建置 libssh2,否則加密金鑰可能無法運作。(只有在我重新編譯 Debian Wheezy 上的函式庫後,它才對我有效)。
7
bluej100 at gmail dot com
11 年前
請注意,`opendir` 目前在 SFTP 根目錄上無法正常運作,但您可以透過附加一個點來解決此問題。請參閱 https://bugs.php.net/bug.php?id=64169http://stackoverflow.com/a/16238476/69173.
5
guilhem at no dot spam dot answeb dot net
7 年前
請注意 thomas at gielfeldt dot dk 指出的 PHP 錯誤,您必須在將連線變數放入連線字串之前使用 `intval()` 進行轉換。

<?php
$connection
= ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$sftp = ssh2_sftp($connection);
// 參考:https://bugs.php.net/bug.php?id=73597
$stream = fopen("ssh2.sftp://" . intval($sftp) . "/path/to/file", 'r');
?>
0
thomas at gielfeldt dot dk
7 年前
<?php
// 使用公開金鑰連線。
$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');
// 設定 sftp 資料流包裝器
$sftp = ssh2_sftp($session);
// 參考:https://bugs.php.net/bug.php?id=73597
$connection_string = 'ssh2.sftp://' . intval($sftp);

// 列出遠端家目錄中的檔案。
$i = new \RecursiveDirectoryIterator("$connection_string/home/remote-username");
$r = new \RecursiveIteratorIterator($i);
foreach (
$r as $f) {
print
$f->getPathname() . "\n";
}
?>
To Top