「password」上下文選項也可以用於提供由「privkey_file」和「pubkey_file」提供的金鑰檔案的通行片語。
請注意此錯誤:https://bugs.php.net/bug.php?id=58573
除非您針對 OpenSSL 建置 libssh2,否則加密金鑰可能無法運作。(只有在我重新編譯 Debian Wheezy 上的函式庫後,它才對我有效)。
ssh2:// — Secure Shell 2
ssh2.shell:// ssh2.exec:// ssh2.tunnel:// ssh2.sftp:// ssh2.scp:// (PECL)
注意:此包裝器預設並未啟用
為了使用 ssh2.*:// 包裝器,必須安裝從 » PECL 取得的 » SSH2 擴充套件。
除了接受傳統的 URI 登入詳細資訊外,ssh2 包裝器也會透過在 URL 的主機部分傳遞連線資源來重複使用已開啟的連線。
範例 #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()` 時,也會發生這種情況。
「password」上下文選項也可以用於提供由「privkey_file」和「pubkey_file」提供的金鑰檔案的通行片語。
請注意此錯誤:https://bugs.php.net/bug.php?id=58573
除非您針對 OpenSSL 建置 libssh2,否則加密金鑰可能無法運作。(只有在我重新編譯 Debian Wheezy 上的函式庫後,它才對我有效)。
請注意,`opendir` 目前在 SFTP 根目錄上無法正常運作,但您可以透過附加一個點來解決此問題。請參閱 https://bugs.php.net/bug.php?id=64169 和 http://stackoverflow.com/a/16238476/69173.
請注意 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');
?>
<?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";
}
?>