ssh2://

ssh2://Secure Shell 2

説明

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

注意: このラッパーはデフォルトでは有効になっていません
ssh2.*:// ラッパーを使用するには、 » PECL から » SSH2 拡張モジュールを インストールする必要があります。

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 で制約される 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

コンテキストオプション
名前 使用法 デフォルト
session 再利用する接続済みの ssh2 リソース  
sftp 再利用する割り当て済みの sftp リソース  
methods Key exchange, hostkey, cipher, compression, および MAC methods  
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');
?>

この例では、unset() のところでセッションを閉じてしまいます。 なぜなら、$connection_string が持つのが $session への参照ではなく、単にそれを文字列にキャストしたものだからです。 これは、unset() が暗黙のうちに呼ばれた場合にも起こりえます (関数などの) スコープを抜けるときに発生する可能性があります。

add a note add a note

User Contributed Notes 4 notes

up
5
exptom
10 years ago
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).
up
6
bluej100 at gmail dot com
10 years ago
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.
up
3
guilhem at no dot spam dot answeb dot net
6 years ago
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');
?>
up
-2
thomas at gielfeldt dot dk
6 years ago
<?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";
}
?>
To Top