When installing SSH2 on Debian Etch using gcc 4.1.2, compilation of ssh2.c failed with a whole load of errors (too many to show here).
A lot of people seem to have had this problem with a variety of flavours of Linux and gcc. The solution that worked for me is as follows:
Presuming you've downloaded, and extracted the pecl lib you can patch it manually by opening up ssh2.c and reading down the script untill you get to the line that reads:
#if LIBSSH2_APINO < 200412301450
Change it to read
#if LIBSSH2_VERSION_NUM < 0x001000
Save the file and run
phpize && ./configure --with-ssh2 && make
Then carry on from there.
Incidentally, if you run "make install" rather than just "make" as the root user then the make process *should* automatically copy ssh2.so into your php.ini extension_dir, provided your environment is correct.
Secure Shell2 関数
導入
安全な暗号化通信を使用したリモートマシン上にあるリソースへのアクセス手段 (シェル、リモート実行、トンネリング、ファイル転送) を提供する » libssh2 のバインディングです。
インストール手順
Windows 用バイナリは » http://snaps.php.net/ にあります。 インストールするには php_ssh2.dll を php.ini ファイルの extension_dir ディレクティブで指定されたフォルダに ダウンロードしてください。そして php.ini に extension=php_ssh2.dll を追加し、 Web サーバを再起動して有効にしてください。
extension_dir=c:/php5/exts/ extension=php_ssh2.dll
Linux, BSD, あるいは他の *nix 系 では以下の手順でコンパイルすることができます:
- » OpenSSL をダウンロードしインストールします。 もしディストリビューションのパッケージングシステム経由で OpenSSL をインストールする場合、開発用ライブラリもインストールしてください。 これは典型的には openssl-dev や openssl_devel、 もしくはいくつかのバリエーションで名前付けされたパッケージです。
- » libssh2 をダウンロードしインストールします。典型的には libssh2 のソースツリーから次のコマンドを実行することを意味します。 ./configure && make all install
- PECL/ssh2 のために PEAR インストーラを実行します: pear install ssh2
- ビルドプロセスが示したディレクトリから php.ini ファイルの extension_dir で指定された場所に ssh2.so をコピーします。
- php.ini に extension=ssh2.so を追加します。
- php.ini の設定を再読込させるため、Web サーバを再起動します。
注意: 開発バージョン 現時点では PECL/ssh2 の 安定 バージョンはありません。 PECL/ssh2 の ベータ バージョンをインストールするには次を実行してください: pear install ssh2-beta
PEAR コマンドを使用しないで PECL/ssh2 をコンパイルする
自動的に PECL/ssh2 をダウンロード、インストールする pear install ssh2 を使用するよりもむしろ » PECL から tarball をダウンロードしても良いです。 展開した tarball のルートから ssh2.so を生成するには次を実行します: phpize && ./configure --with-ssh2 && make 生成した後、上記のステップ 4 からインストールを続けてください。
この PECL 拡張モジュールをインストールする方法は、 マニュアルの PECL 拡張モジュールのインストール という章にあります。 新規リリース・ダウンロード・ソースファイル・管理者情報・CHANGELOG といった関連する情報については、次の場所にあります。 » http://pecl.php.net/package/ssh2.
注意: libssh2 ライブラリのバージョン 0.4 もしくはそれ以降が必要です (ひょっとするともっと新しいかも知れませんので、 リリースノートを参照してください) 。
定義済み定数
以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。
- SSH2_FINGERPRINT_MD5 (integer)
- ホスト鍵のフィンガープリントを MD5 ハッシュとして要求する ssh2_fingerprint() 用フラグ
- SSH2_FINGERPRINT_SHA1 (integer)
- ホスト鍵のフィンガープリントを SHA1 ハッシュとして要求する ssh2_fingerprint() 用フラグ
- SSH2_FINGERPRINT_HEX (integer)
- ホスト鍵のフィンガープリントを 16 進法の文字列として要求する ssh2_fingerprint() 用フラグ
- SSH2_FINGERPRINT_RAW (integer)
- ホスト鍵のフィンガープリントを 8 ビットキャラクタの文字列として要求する ssh2_fingerprint() 用フラグ
- SSH2_TERM_UNIT_CHARS (integer)
- 文字単位で 幅 と 高さ を指定するための ssh2_shell() 用フラグ
- SSH2_TERM_UNIT_PIXELS (integer)
- ピクセル単位で 幅 と 高さ を指定するための ssh2_shell() 用フラグ
- SSH2_DEFAULT_TERM_WIDTH (integer)
- ssh2_shell() で要求されるデフォルトのターミナル幅
- SSH2_DEFAULT_TERM_HEIGHT (integer)
- ssh2_shell() で要求されるデフォルトのターミナル高さ
- SSH2_DEFAULT_TERM_UNIT (integer)
- ssh2_shell() で要求されるデフォルトのターミナル単位
- SSH2_STREAM_STDIO (integer)
- STDIO サブチャネルを要求する ssh2_fetch_stream() 用フラグ
- SSH2_STREAM_STDERR (integer)
- STDERR サブチャネルを要求する ssh2_fetch_stream() 用フラグ
- SSH2_DEFAULT_TERMINAL (string)
- ssh2_shell() によって要求される デフォルトのターミナル型 (例えば vt102, ansi, xterm, vanilla)
目次
- ssh2_auth_hostbased_file — ホスト公開鍵を使用して認証を行う
- ssh2_auth_none — "none" として認証する
- ssh2_auth_password — SSH 上でプレーンなパスワードを使用した認証を行う
- ssh2_auth_pubkey_file — 公開鍵を使用した認証を行う
- ssh2_connect — SSH サーバに接続する
- ssh2_exec — リモートサーバ上でコマンドを実行する
- ssh2_fetch_stream — 拡張データストリームを取得する
- ssh2_fingerprint — リモートサーバのフィンガープリントを処理する
- ssh2_methods_negotiated — ネゴシエートされたメソッドのリストを返す
- ssh2_publickey_add — 認証済み公開鍵を追加する
- ssh2_publickey_init — 公開鍵サブシステムを初期化する
- ssh2_publickey_list — 現在認証済みの公開鍵の一覧を表示する
- ssh2_publickey_remove — 認証済み公開鍵を取り除く
- ssh2_scp_recv — SCP 経由でファイルを要求する
- ssh2_scp_send — SCP 経由でファイルを送信する
- ssh2_sftp_lstat — シンボリックリンクの情報を取得する
- ssh2_sftp_mkdir — ディレクトリを作成する
- ssh2_sftp_readlink — シンボリックリンクのターゲットを返す
- ssh2_sftp_realpath — 指定されたパス文字列の実パスを解決する
- ssh2_sftp_rename — リモートファイルを改名する
- ssh2_sftp_rmdir — ディレクトリを削除する
- ssh2_sftp_stat — リモートファイルシステム上のファイルの情報を取得する
- ssh2_sftp_symlink — シンボリックリンクを作成する
- ssh2_sftp_unlink — ファイルを削除する
- ssh2_sftp — SFTP サブシステムを初期化する
- ssh2_shell — 対話式のシェルを要求する
- ssh2_tunnel — リモートサーバを経由するトンネルをオープンする
ssh2
25-Jan-2008 08:01
11-Dec-2007 10:41
Setup PHP SSH2 Connection from php running locally win32
to a remote box (i used pair networks)
1. Download module
http://www.php.net/downloads.php
Download Win32 binaries and extract
php_ssh2.dll to the extensions directory
Edit the php.ini file that is being used to include this extension
extension=php_ssh2.dll
Generate a pub / private key pair on the server you wish to connect to
ssh-keygen -t rsa -C "username@boxname.pair.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/usr/home/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /usr/home/username/.ssh/id_rsa.
Your public key has been saved in /usr/home/username/.ssh/id_rsa.pub.
Copy both the public and private key files to your local box.
Cut and paste the id_rsa.pub contents into authorized_keys2
file in /usr/home/username/.ssh - creating it if it does not exist.
Create a php program containing the following
<?php
/* Notify the user if the server terminates the connection */
function my_ssh_disconnect($reason, $message, $language) {
printf("Server disconnected with reason code [%d] and message: %s\n",
$reason, $message);
}
$methods = array(
'kex' => 'diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,'
.'diffie-hellman-group-exchange-sha1',
'client_to_server' => array(
'crypt' => '3des-cbc',
'comp' => 'none'),
'hostkey'=>'ssh-rsa',
'server_to_client' => array(
'crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc',
'comp' => 'none'));
$callbacks = array('disconnect' => 'my_ssh_disconnect');
$connection = ssh2_connect('vepar.pair.com', 22, $methods, $callbacks);
if (!$connection) die('Connection failed');
/* Uncomment to secure even further,
you want to get the known host key by getting $fingerprint
the first time and then hard coding
$known_host = '';
$fingerprint = ssh2_fingerprint($connection,
SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
if ($fingerprint != $known_host) {
die("HOSTKEY MISMATCH!\n" .
"Possible Man-In-The-Middle Attack? $fingerprint");
}
*/
if (ssh2_auth_pubkey_file($connection, 'username',
'S:/sshphp/id_rsa.pub',
'S:/sshphp/id_rsa', '')) {
echo "Public Key Authentication Successful\n";
} else {
die('Public Key Authentication Failed');
}
?>
Alan Reddan Silverarm
24-Nov-2007 08:21
if you want to be able to compile the ssh2 extension with the newest version of libss2, you should modify the source code of ssh2.c and replace
#if LIBSSH2_APINO < 200412301450
with
#if false
and it is working
08-Nov-2007 01:52
Just a note, if you're download the libssh2 source tar, you'll need to download libssh2-0.12.tar.gz. The newer versions won't compile properly. I've tried either the pecl or manual way, and kept getting a compile time error when using the pecl package ssh2-0.10.
Michael James
mjames@voipsupply.com
26-Oct-2007 03:06
you will have an issue transferring any file using ssh2_scp_send unless you send a dummy file after your real file, or preferably issue an
ssh2_exec ($connection, 'exit');
I was transferring hundreds of thousands of files in my prototype across multiple servers, and noticed that each file had a mismatched md5 hash and bytelength.
after a bit of research i learnt that this was a bug, and added the code above as suggested at pecl.php.net in the bugs section.
hopefully this helps someone who is getting partial file transfer that could ruin your whole app.
24-Oct-2007 03:52
when connecting with ssh2_auth_password (connection, username, password), if you don't use a constant for the password you will get an authentication failure. i tried casting as a string, putting in single and double quotes, etc.
i tried passing the password straight from mysql and couldn't. you'll spend hours trying to figure this simple thing out, so you're stuck with using hard coded password strings for now.
18-Aug-2007 04:00
Some installations of PHP 5 may have a problem starting the SSH2 extension as a Windows' service. It just started happening to me today, spontaneously. I have PHP 5.2.2 (and have tried 5.2.3), on Apache 2.0.59 .
It turns out that it's an Apache problem. Even though I was given an error message when starting it, it still worked perfectly from the CLI. So, I commented it out in the
INI file again, and am just loading it using extension_loaded('ssh2') and dl('php_ssh2.dll') from within the script.
26-Jul-2007 11:25
A detailed installation how to, and some source code examples:
http://kevin.vanzonneveld.net/?mp=techblog&sp=article&id=37
11-Jun-2007 10:12
The windows binary if php_ssh2.pdb on http://snaps.php.net/ is located in the debug pack.
06-Jan-2007 03:11
For those trying to use this under an x86_64 version of an rpm-based distribution (e.g. CentOS, RHEL or Fedora Core) who don't have an rpm for libssh2, here's a work around.
Locate the source rpm for an i386 version (for instance from dag or dries) and rebuild it:
rpmbuild --rebuild --target x86_64 libssh2-*.src.rpm
If the rebuild complains about not being able to find something and references a path beginning /var/tmp or /tmp, it may very well have built the files but failed due to a minor error in the spec file. I was too lazy to fix the spec file, so I just manually copied the files into place. In my case, the path in the error included /var/tmp/libssh2-0.12-root/usr/lib/... Note that libssh2 part? That's the area where all the files are dumped during the rpmbuild process. if you move everything in there to the comparable place on your drive, you're golden. In this case libssh2.so was in /var/tmp/libssh2-0.12-root/usr/lib/ Strip off the /var/tmp/libssh2* part and you find out the libssh2.so file goes in /usr/lib/
Enjoy.
21-Dec-2006 12:19
Update for the Mac OS X install when using the Entropy.ch PHP5 package: You can use the PECL installer for ssh2 now instead of manually downloading it.
1) Download and install libssh2 per the instructions below(Steps 1-5)
2) run "sudo /usr/local/php5/bin/pecl install ssh2-beta"
3) add extension=ssh2.so to /usr/local/php5/lib/php.ini
4) Restart Apache (sudo apachectl restart)
19-Oct-2006 07:30
I also struggled with the
configure: error: libssh2 version >= 0.4 not found
error using verions 0.10 of ssh2 and 0.14 of libssh.
When looking in the config.log file I found that it was unable to find my SSL libraries, and as I installed my openssl in /usr/local/ssl it could not find it. It was only looking in /usr/local/lib.
To get round this problem, I set
LDFLAGS="-L /usr/local/ssl/lib"
and this works a treat.
01-Sep-2006 02:17
Installing on HPUX 11.11 cookbook
Just follow the install instructions on the man page and solved the problems as they appear
- when making libssh2
Problem related with /lib/pa20_64 and 64 bits library in 32 bits link
Edited the configure file and changed
SHLIB_LDFLAGS="-b +vnocompatwarnings -L/lib/pa20_64"
to
SHLIB_LDFLAGS="-b +vnocompatwarnings -L/lib"
-when executing phpize
Problems with aclocal, autom4ke and m4
Installed automake, autoconf and m4
-when ./configuring the ssh2
Configure: error: libssh2 version >= 0.4 not found
Edited the configure.m4 and removed the -ldl switch from line (as posted before)
39 -L$SSH2_DIR/lib -lm -ldl
runned phpize again and ./configure --with-ssh2
11-Aug-2006 10:11
I was building out the ssh2 extension (0.10) under OpenBSD 3.9 on sparc64 and I got this error when running configure:
checking for libssh2_banner_set in -lssh2... no
configure: error: libssh2 version >= 0.4 not found
I did some poking around in the configure file and traced the problem down. The reason for the failure is that configure is trying to compile a test program with -ldl. libdl is not installed on OpenBSD 3.9. The OpenBSD folks purged libdl years ago and I could not find a package that contains it.
(The error message above is misleading because the problem is not with libssh2.)
I looked at the ssh2 extension code and could not see a reason for using libdl, so I removed the -ldl from the config.m4 file. Re-ran phpize and configure. No errors. Did make and installed ssh2.so. Works fine.
25-Jul-2006 01:02
Just a quick note about the manual installation instructions above. If your PHP is in a non-standard location, or you're running multiple installs of different PHP version, you have to complie the module with an explicit path to phpize and php-config:
/path/to/correct/php/bin/phpize && ./configure --with-ssh2 --with-php-config=/path/to/correct/php/bin/php-config && make
Took me an hour to figure out why I was getting a "Unable to initialize module Module compiled with module API" error when I loaded the module. Turns out, it was compiling against my php4 libs, and not the php5 ones.
06-Jun-2006 01:57
I couldn't get `pear install ssh2-beta` to work, so I tried the manual installation. I followed the instructions:
phpize && ./configure --with-ssh2 && make
Copy ssh2.so from the directory indicated by the build process to the location specified in your php.ini file under extension_dir.
When I restarted Apache, I got:
PHP Warning: Unknown(): Invalid library (maybe not a PHP library) 'ssh2.so' in Unknown on line 0
To fix the problem, I ran `make install`, instead of copying ssh2.so into the extension directory.
27-Mar-2006 02:08
To slightly correct the directory listing code below....
<?php
$connection = ssh2_connect("hostname", 22);
// use any of the ssh2_auth_* methods
ssh2_auth_password($connection,"username", "password");
$sftp = ssh2_sftp($connection);
$dir = "ssh2.sftp://$sftp/path/to/dir/";
$dh = opendir($dir);
while (($file = readdir($dh)) !== false) {
echo "$file is in hostname:/path/to/dir\ n";
}
closedir($dh);
?>
(thanks to Sara for assisting)
05-Oct-2005 07:15
Trying to compile this ssh PECL extension for Mac OS X (10.4.2)
with entropy.ch's php5 distribution? Read on: PART 2:
11) Check your shell's path variable by SHELL$ "echo $PATH".
If your php5 binary directory is not listed before the standard OS X binary directory,
update your $PATH variable to list it first. Mine reads:
/usr/local/mysql/bin/: /usr/local/php5/bin/: /bin:/sbin: /usr/bin:/usr/sbin
11a) if you do not do this step, the PECL extension will link against the old
php4 version that ships with Mac OS X. You will know this by the fact that the
next step lists a server API that is different than the output of phpinfo():
My version was 20041030, Apples version was 20020429.
12) Run: phpize && ./configure --with-ssh2 && make
This will produce a lot of output and probably some warnings. If you get a note
at the end that says "Build Complete", then the process has completed
(probably successfully). The command has produced the output file ssh2.so within
the ssh2-0.9 (or whatever version) directory.
13) Check where the extension directory is located at by running the following command:
SHELL$/usr/local/php5/bin/php-config --extension-dir
If it lists some directory that actually exists, copy the ssh2.so file to this directory.
If it doesn't list a location that exists, you can either create the location, or
choose a different location and update the line in your php.ini file:
extension_dir = "./"
to point to the directory you would like to use. I chose:
extension_dir = "/usr/local/php5/extensions/"?>
14) Add the line
extension=ssh2.so
to the Dynamic Extensions area of your php.ini file.
15) Restart your web server for the extensioin to be loaded. If there are any problems, they will be written to the webserver log.
16) Check if your new ssh based streams are available by running:
SHELL$ /usr/local/php5/bin/php -r "print_r(stream_get_wrappers());"
You should get something similar to this output:
Array
(
[0] => php
[1] => file
[2] => http
[3] => ftp
[4] => compress.bzip2
[5] => compress.zlib
[6] => https
[7] => ftps
[8] => ssh2.shell
[9] => ssh2.exec
[10] => ssh2.tunnel
[11] => ssh2.scp
[12] => ssh2.sftp
)
You should now be able to do all the cool things you need to do with ssh
(like sftp scp ssh execution)! Take a look at some of the comment here for some
example scripts to test things out:
http://us2.php.net/manual/en/ref.ssh2.php
Good luck... hope that saves you my headache! :)
05-Oct-2005 07:12
Trying to compile this ssh PECL extension for Mac OS X (10.4.2)
with entropy.ch's php5 distribution? Read on: PART 1:
1) Download libssh (I used version 0.11)
http://sourceforge.net/project/showfiles.php?group_id=125852
2) Unzip the directory and cd to the upacked directory in the terminal.
3) run: ./configure; make all install;
libssh should compile and place the output file in src/*.o
4) copy all headers from the include folder within the libssh2 directory to
/usr/local/include/
5) copy all compiled files (*.o) from the src folder within the libssh2
directory to /usr/local/lib/
6) Download ssh2
(I used version 0.9 with additional changes from cvs - explained below)
http://pecl.php.net/package/ssh2
7) unzip the downloaded archive.
8) version 0.9 has a conflict with an existing symbol used in some of the ZEND headers...
a change is available in cvs which renames the symbol in ssh2 to avoid the conflict
http://cvs.php.net/diff.php/pecl/ssh2/ssh2.c?r1=1.9&r2=1.10&ty=h
down load at least the 1.10 version of ssh2.c
if you decide to download a newer version, download any of the corresponding related files,
such as the php_ssh2.h header file.
9) replace the old version of ssh2.c with the new version of ssh2.c
(and any other files you downloaded).
10) From the terminal:
cd to the source directory (for me ssh2-0.9/ssh2-0.9/).
(Continued in a PART 2 comment)
27-May-2005 11:29
OK, the SSH class I wrote is now available for free on the Net.
The class uses an underlying SSH implementation, and hence does not require any library to be compiled against.
It is available at savannah.cer.ch as a byproduct of project GridGramm (look in the downloads section). There are two releases available, the first one establishes a connection for each command, having a serious authentication overhead. The second release shares a master connection, highly reducing auth payload, but requires openssh >= 3.8.
They are also available from the development site at
http://savannah.cern.ch/projects/gridgramm
and come with extensive documentation and examples.
These are work in progress, and I hope to add more enhancements in the close future. But you are welcome to use them now.