» Make SSH connections with PHP
Not everyone knows about PHP's capabilities of making SSH connections and executing remote commands, but it can be very useful. I've been using it a lot in PHP CLI applications that I run from cronjobs, but initially it was a pain to get it to work. The PHP manual on Secure Shell2 Functions is not very practice or thorough for that matter, so I would like to share my knowledge in this how to, to make it a little less time consuming setting this up.
In this article I'm going to assume that:
- You're running Debian / Ubuntu
If not, you will have to substitute the package manager aptitude with whatever your distribution provides - You're running PHP 5
If not, just replace php5 with php4 everywhere - You have basic knowledge of PHP & server administration
- You already have PHP installed
Prerequisites
Packages
First let's install the following packages:
sudo aptitude update
sudo aptitude install php5-dev php5-cli php-pear buid-essential \
openssl-dev zlib1g-dev
That should set us up alright.
libssh2
Now we need libssh2 from sourcefourge. We have to compile this, but no worries, this is all you need to do:
cd /usr/src
wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
tar -zxvf libssh2-0.14.tar.gz
cd libssh2-0.14/
./configure
make all install
That's it! Easy right? If you want you can check if there's a newer version at sf.net but 0.14 will do just fine.
Installation
ssh2.so
Next we need to link libssh & PHP together. There's a PECL module for this so let's install using:
pecl install -f ssh2
The -f makes sure ssh2 is installed even though there's not a stable candidate. You could also use the package name: ssh2-beta to overrule this.
Now you need to make sure our new ssh2.so module is loaded by PHP. Edit your php.ini file (for CLI utitilies: /etc/php5/cli/php.ini, for Apache utilities /etc/php5/apache2/php.ini)
extension=ssh2.so
It should be placed beneath the: "Dynamic Extensions" section somewhere around line 515.
Great! PHP supports SSH - time to code
You've just enabled ssh2 support in PHP. Now how can we make use of this? There are 2 options. SSH supports the:
- execute method
This tells the server's operating system to execute something and pipe the output back to your script. (recommended) - shell method
This opens an actual shell to the operating system, just as you would normally when logging in with your terminal application. Some routers that don't have a full POSIX compliant implementation, but run their own application as soon as you login, require this. (advanced)
Method 1: Execute
Best would be to create functions or even a class for the following code, but this is the basic idea and will definitely get you started:
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); // log in at server1.example.com on port 22 if(!($con = ssh2_connect("server1.example.com", 22))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, "root", "secretpassword")) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; // execute a command if(!($stream = ssh2_exec($con, "ls -al" )) ){ echo "fail: unable to execute command\n"; } else{ // collect returning data from command stream_set_blocking( $stream, true ); $data = ""; while( $buf = fread($stream,4096) ){ $data .= $buf; } fclose($stream); } } }
Method 2: Shell
Best would be to create functions or even a class for the following code, but this is the basic idea and will definitely get you started:
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); // log in at server1.example.com on port 22 if(!($con = ssh2_connect("server1.example.com", 22))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, "root", "secretpassword")) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; // create a shell if(!($shell = ssh2_shell($con, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))){ echo "fail: unable to establish shell\n"; } else{ stream_set_blocking( $shell, true ); // send a command fwrite($shell,"ls -al\n"); sleep(1); // & collect returning data $data = ""; while( $buf = fread($shell,4096) ){ $data .= $buf; } fclose($shell); } } }
Tips
Sometimes when a server is busy, or a connection is buggy, the buffer may run dry, and the PHP script stops collecting data from a command output (even though the command hasn't completed yet!). There are a couple of things you could do about that:
ssh2_exec($con, 'ls -al; echo "__COMMAND_FINISHED__"' );
Now, in the loop where you keep checking for the buffer, just see if the COMMAND_FINISHED line is coming by. Because then you know you have all the data. To avoid infinite loops, just limit the loop with a timeout of 10 seconds or so:
$time_start = time(); $data = ""; while( true ){ $data .= fread($stream, 4096); if(strpos($data,"__COMMAND_FINISHED__") !== false){ echo "okay: command finished\n"; break; } if( (time()-$time_start) > 10 ){ echo "fail: timeout of 10 seconds has been reached\n"; break; } }
In the example above, you'd better set stream_set_blocking to false.
Can't get enough?
PHP can send files over ssh!
ssh2_scp_send($con, "/tmp/source.dat", "/tmp/dest.dat", 0644);
Doesn't work?
Check the following:
- Did you follow every step of the prerequisites & installation how to in this article?
- On the serverside, 'PasswordAuthentication yes' must be enabled in the sshd_config. Default is yes on most servers, but in some cases you will have to turn this on yourself by making sure the following lines are in place in the file: /etc/ssh/sshd_config:
# Change to yes to enable tunnelled clear text passwords
PasswordAuthentication yes
If you've made any changes, ssh needs a restart
/etc/init.d/ssh restart
Post a comment here if it's still failing. Don't forget to paste the error that you're getting.
Like this article?
|
Then Dzone it! Or use another bookmark button below to show your support & help me spread the word. |
Hot StuffFlaming articles» Survive heavy traffic with you... | RelatedArticles like this one» Survive heavy traffic with you... |
tags: php, ssh, ubuntu, PEAR, PECL, libssh2, CLI, apache, router
category: Programming - PHP
read: 33,912 times






tagcloud
#93. Ross on 26 June 2008
#92. Ho Wai Keong on 26 June 2008
this definitely fixes it!
#91. Peter on 25 June 2008
copied by verifying his md5 hash code and the copy was successful, the file was well copied. Can you help me?
#90. Shoaibi on 10 June 2008
#89. Kevin on 06 June 2008
extension=ssh2.so
#88. UnrealComps on 06 June 2008
#87. MianoSM on 04 June 2008
#86. Kevin on 03 June 2008
extension=ssh2.so
#85. MianoSM on 03 June 2008
I get install ok: channel://pear.php.net/ssh2-0.10
I have also restarted sshd after ensuring:
... [more] passwordauthentication yes
Now, when I use the block for Method 1, I'm seeing:
"function ssh2_connect doesn't exist"
Any pointers on this would be greatly appreciated. Thank you.
#84. Kevin on 31 May 2008
#83. nunu on 27 May 2008
Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 10.128.16.19 on port 22 in /var/www/html/oned/ssh.php on line 2
Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 10.128.16.19 in /var/www/html/oned/ssh.php on line 2
... [more]
can you tell me what's wrong?
i'm using :
libssh2 version 0.12
banner SSH-2.0-libssh2_0.12
Thankyou
#82. NJ on 09 May 2008
#81. cartmanf on 04 May 2008
Hope you post some more advanced tutorials soon.
Regards,
... [more] Reinhard
#80. Wes on 24 April 2008
Thanks a lot for all your help.
Excellent tutorial indeed
#79. Kevin on 23 April 2008
- concatenate the var
- when sudoing it is probably better use a shell instead of exec
#78. Wes on 22 April 2008
#77. Wes on 22 April 2008
Thanks Kevin, I found the easiest way is to enable sudo user from running commands without being asked for a password
But after all that i cant pass variables to the shell, for example:#76. Kevin on 22 April 2008
#75. shinta on 22 April 2008
i'ts doesn't work in newes libssh2
i trying libssh2-0.14 and everything ok
... [more] thank u kevin for your article
GBU
#74. Kevin on 22 April 2008
@ shinta: Hi,
Is just the conclusion. Best is to find the first thing (not the last) that goes wrong in the text you get. And what about ? Some people for some weird reason can't use pecl on their system, though ssh2 definitely is a pecl package...#73. shinta on 22 April 2008
command:
--------------------------------------------------------
pecl install -f ssh2
... [more] --------------------------------------------------------
and i got :
--------------------------------------------------------
No releases available for package "pecl.php.net/ssh2"
Cannot initialize 'ssh2', invalid or missing package file
Package "ssh2" is not valid
install failed
--------------------------------------------------------
whether i do connect to internet? to execute "pecl install -f ssh2"?
and i download some pakcage from http://pecl.php.net/package/ssh2
and i try to execute
--------------------------------------------------------
pecl install -f /home/user/ssh2-0.10.tgz
--------------------------------------------------------
and it's running some script / something compile,but i got some error
--------------------------------------------------------
make: *** [ssh2.lo] Error 1
ERROR: `make' failed
--------------------------------------------------------
what's wrong in my command?
i use ubuntu feisty?
#72. few bugs when setting up ssh2 on 18 April 2008
#71. few bugs when setting up ssh2 on 18 April 2008
#70. Kevin on 17 April 2008
#69. Sebastian on 17 April 2008
if you have root access on that box, configure sudo and put that in your script (e.g. sudu rcapache2 reload).
rgds,
#68. Wes on 17 April 2008
I want to run some commands as root, but I can't login as root, is there away that I can su into root after logging into server as normal user?
#67. Sebastian on 09 April 2008
does "I see" mean you were able to reproduce the behavior?
Unfortunatly, your idea doesn't work on my system.
The echo command will not be displayed on the webpage. But the first part can be seen in the local file.
... [more]
What type of connection is opend to transfer the data back?
Can I presume, when a regular ssh connection from console works, that the ssh2_exec() should work too?
Any ideas? The server to reach is in the same network, so there shouldn't be any routing/firewalling problem.
rgds,
#66. Kevin on 09 April 2008
#65. Sebastian on 09 April 2008
Ahhh,
ok, I tried the first variant of __command finished__.
I suppose this should be displayed in the browser when the script is executed.
But that was not the case.
On forwarding this echo to a textfile
works as expected. The stout was directed into the file, and can be read by loggin in on console. hrrghh.#64. Kevin on 08 April 2008
#63. Sebastian on 08 April 2008
1st: The initial script works, too. It echos: okay: logged in...
2nd: the connection establishes fine. otherwise I could not have touched a file.
I stripped the code to exclude other errors (typos, etc)
Which second tip? Have I missed anything?
... [more] You mean about valitading the results?
How shall I test?
We can switch over to email conversation, not to "spam" your commments. Drop a mail to s(dot)waitz(at)rtsgroup(dot)net.
Thanks a lot.
#62. Kevin on 08 April 2008
#61. Sebastian on 08 April 2008
#60. Kevin on 08 April 2008
What about building a 5 second sleep. Executing a command (maybe a command that touches a file on the remote system?), validate the results and then disconnect, for testing purposes?
#59. Sebastian on 08 April 2008
#58. Kevin on 08 April 2008
@ Sebastian: Strange. Not a lot to go on either. If a step fails you should get an error. How about turning on all error reports?
Does this tell you anything?#57. Sebastian on 07 April 2008
thanks for the tutorial. Installation and configuration works nicly on SLES 10.
But I have the problem, that Method 1 oder 2 doesn't get any result back.
No errors in apache2 log.
... [more] On echo'ing $stream, I get "Resource id#3" back.
Any ideas?
Thanks a bunch
#56. Kevin on 08 March 2008
#55. XSchne2036 on 07 March 2008
I followed your instructions
and ...
... [more]
zirkonia:/var/www/apache2-default/libssh2-0.18/src/.libs# pecl install -f ssh2 WARNING: failed to download pecl.php.net/ssh2 within preferred state "stable", will instead download version 0.10, stability "beta"
downloading ssh2-0.10.tgz ...
Starting to download ssh2-0.10.tgz (22,187 bytes)
........done: 22,187 bytes
5 source files, building
running: phpize
sh: phpize: command not found
ERROR: `phpize' failed
apt-cache search phpize has no results too...
please... what am I doing wrong ?
#54. youme on 26 February 2008
if you want buy price is 150$ USD
email me mscmsc@brain.com.pk
#53. Emanuel on 06 February 2008
#52. Kevin on 05 February 2008
@ Emanuel: My bad, the example code was missing a: ';'
so:
needed to be: I've updated the article accordingly. btw, you were using the CODE tags incorrectly but I've editted your comment for better readability.#51. Emanuel on 05 February 2008
Hi, finally I obtained to install lib ssh2.so. to put when I go to twirl the test function that exists in its wiki:
e in log of the apache returns me the following error: [CODE="[ error ] PHP error Parse: syntax error, unexpected T_IF in /var/www/htdocs/a.php on line 4[/CODE] wanted to know if it would have as to help to decide this problem me#50. Kevin on 19 January 2008
@ Pimmy: I can't say I have. I did experience similar hanging sessions when connecting to (Force10) routers though. Implementing a lot of timeouts, double checks, and retries added some stability.
I finally came to the conclusion that the router had a different buffer or timing mechanism. Cause when reading the buffer:
It was suddenly empty. Thus breaking the while and then closing the shell. But the router was just busy filling a new buffer and so the ssh session hang. But I doubt your problem is the same.#49. Pimmy on 18 January 2008
The problem I see is that after everything completes there is a hanging ssh session left running on the server.
Has anyone experienced something similar or anyone with any ideas?
... [more]
Thanks,
Pimmy
#48. Kevin on 17 January 2008
#47. Emanuel on 17 January 2008
/usr/include/libssh
and inside of it exists these archives:
config.h crypto.h libssh.h server.h sftp.h ssh1.h ssh2.h
to put ssh2.so not you.
#46. Kevin on 16 January 2008
But if I would have to guess, I would say that it's got something todo with not having a /usr/include available. Could that be?
#45. Emanuel on 15 January 2008
./configure = OK
make all install =
root@servidor: /temp/libssh2-0.14# make all install
make [1]: Entering in the directory “/temp/libssh2-0.14/src”
... [more] GCC - channel.o channel.c - c - g - O2 /usr/include - I/usr/include - Wall - I. /include/ - fPIC
GCC: /usr/include: to linker input file unused because linking not done
channel.c: 1253: 10: /usr/include: Archive or directory not found
channel.c: 71: internal to compiler error: Imperfection of Please
segmentation submit full bug report,
with preprocessed source if appropriate.
See <URL: http://gcc.gnu.org/bugs.html > will be instructions.
make [1]: ** [channel.o] Error 1
make [1]: Leaving the directory “/temp/libssh2-0.14/src”
make: ** [all] Error 1
2º - As it did not give certain in 1a, I tried using the version libssh2-0.18 that it compiled normally, to put I was to look lib ssh2.so and I did not find, what I found was libssh2.so, from there I configured php.ini with the following parameter:
extension=libssh2.so
I restarted the apache and in log of error vi the following one:
PHP Warning: Unknown (): Invalid library (maybe not the PHP library) “libssh2.so” in Unknown on line 0.
Debtor for helps me!
#44. Kevin on 15 January 2008
If that doesn't work, let me know where you got stuck. what errors do you get. Thanks!
#43. Emanuel on 15 January 2008
#42. Kevin on 16 December 2007
#41. Pimmy on 14 December 2007
So I fixed the problem by pointing to my real PHP:
root@tangra:ssh2-0.10>phpize
... [more] root@tangra:ssh2-0.10>./configure \
> --with-php-config=/usr/local/php/bin/php-config \
> --with-ssh2=/usr/local
root@tangra:ssh2-0.10>make clean
root@tangra:ssh2-0.10>make
Then everything went smooth.
Cheers.
#40. Pimmy on 14 December 2007
--------------------------------------------------------
PHP Warning: PHP Startup: U\x89\xe5VS\x8bu\x0c\xe8: Unable to initialize module\nModule compiled with module API=20020429, debug=0, thread-safety=
0\nPHP compiled with module API=20060613, debug=0, thread-safety=0\nThese options need to match\n in Unknown on line 0
... [more] --------------------------------------------------------
I've recompiled both libssh2-0.14 and ssh2-0.10 several times in different ways but no joy.
Have you got any suggestions?
#39. Hiren Thakkar on 13 December 2007
#38. Pimmy on 13 December 2007
----------------------------------------------------
root@tangra:libssh2-0.14>make all install
make[1]: Entering directory `/space/installs/libssh2-0.14/src'
... [more] gcc -o channel.o channel.c -c -g -O2 /usr/include -I/usr/local/include -Wall -I../include/ -fPIC
gcc: cannot specify -o with -c or -S and multiple compilations
make[1]: *** [channel.o] Error 1
make[1]: Leaving directory `/space/installs/libssh2-0.14/src'
make: *** [all] Error 1
root@tangra:libssh2-0.14>gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-59)
----------------------------------------------------
This seems to be a 'minor' bug in version 0.14 which can be fixed by removing the '-o' option from the Makefile.in file (line 23).
It might be fixed in later versions of libssh2, but as you notice there are other bugs in the newer versions.
I would really like to know if anyone managed to successfully use the patest versions of libssh2.
Thanks for the tutorial
#37. Kevin on 09 December 2007
#36. Nigel on 09 December 2007
I want to start doing some ssh2 from my php scripts. Followed the instructions from php.net, i.e. got the PECL zip's, unzipped to c:\php5\exts
then added to php.ini: -
extension_dir=c:/php5/exts/
extension=php_ssh2.dll
... [more]
then restarted apache (and the server to be sure on the next attempts!)
...Code 500! aaaarrrggghhhh
Log file below, any ideas please??
have installed openssl.exe and made sure in path. php.ini file must be ok as it calls in the errors below but then errors. Anyone got any advise please!!! Stupidly told company that I can get this done next week :(
[Mon Dec 03 20:07:47 2007] [warn] pid file C:/Program Files/Apache Software Foundation/Apache2.2/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
PHP Warning: PHP Startup: Unable to load dynamic library 'c:/php5/exts/php_ssh2.dll' - The operating system cannot run %1.\r\n in Unknown on line 0
[Mon Dec 03 20:07:49 2007] [notice] Apache/2.2.4 (Win32) PHP/5.2.4 configured -- resuming normal operations
[Mon Dec 03 20:07:49 2007] [notice] Server built: Jan 9 2007 23:17:20
[Mon Dec 03 20:07:49 2007] [notice] Parent: Created child process 1072
PHP Warning: PHP Startup: Unable to load dynamic library 'c:/php5/exts/php_ssh2.dll' - The operating system cannot run %1.\r\n in Unknown on line 0
[Mon Dec 03 20:07:49 2007] [notice] Child 1072: Child process is running
[Mon Dec 03 20:07:49 2007] [notice] Child 1072: Acquired the start mutex.
[Mon Dec 03 20:07:49 2007] [notice] Child 1072: Starting 250 worker threads.
[Mon Dec 03 20:07:49 2007] [notice] Child 1072: Starting thread to listen on port 80.
[Mon Dec 03 20:11:09 2007] [error] [client 172.30.1.78] PHP Fatal error: Call to undefined function ssh2_connect() in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\ssh.php on line 3
[Mon Dec 03 20:19:12 2007] [error] [client 172.30.1.78] PHP Fatal error: Call to undefined function ssh2_connect() in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\ssh.php on line 3
#35. Kevin on 07 November 2007
#34. Bryan on 06 November 2007
I initially tried the 0.17 version of libssh, and I got the "make 1" error from PECL. When I installed 0.14, everything became peachy.
#33. Kevin on 31 October 2007
#32. vezzo on 31 October 2007
#31. Kevin on 25 October 2007
@ JavieL: I'm sorry what do you mean exactly?
#30. JavieL on 23 October 2007
#29. Patrick on 23 October 2007
Below my full report on what went wrong.
Today: 23-10-2007 i followed the instructions on:
... [more] http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/
However, i failed to sucessfully get libssh2-0.14 working.
It gave an error on the command './configure' at about 90%.
Also the command 'make all install' failed at about 40%.
Next i tried to get libssh2-0.17 working.
I sucessfully ran './configure' and 'make all install'.
Proceeding i executed: pecl install -f ssh2
This seemed to work, but failed at...
running: make
/bin/sh /tmp/pear-build-root/ssh2-0.10/libtool --mode=compile gcc -I. -I/tmp/pear/download/ssh2-0.10 -DPHP_ATOM_INC -I/tmp/pear-build-root/ssh2-0.10/include -I/tmp/pear-build-root/ssh2-0.10/main -I/tmp/pear/download/ssh2-0.10 -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/download/ssh2-0.10/ssh2.c -o ssh2.lo
gcc -I. -I/tmp/pear/download/ssh2-0.10 -DPHP_ATOM_INC -I/tmp/pear-build-root/ssh2-0.10/include -I/tmp/pear-build-root/ssh2-0.10/main -I/tmp/pear/download/ssh2-0.10 -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/download/ssh2-0.10/ssh2.c -fPIC -DPIC -o ssh2.lo
/tmp/pear/download/ssh2-0.10/ssh2.c: In function `zif_ssh2_methods_negotiated':
/tmp/pear/download/ssh2-0.10/ssh2.c:481: warning: passing arg 2 of `libssh2_session_methods' makes integer from pointer without a cast
/tmp/pear/download/ssh2-0.10/ssh2.c:481: error: too many arguments to function `libssh2_session_methods'
/tmp/pear/download/ssh2-0.10/ssh2.c: In function `zif_ssh2_fingerprint':
/tmp/pear/download/ssh2-0.10/ssh2.c:536: warning: assignment discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c: In function `zif_ssh2_publickey_add':
/tmp/pear/download/ssh2-0.10/ssh2.c:1038: warning: passing arg 1 of `_efree' discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c: In function `zif_ssh2_publickey_list':
/tmp/pear/download/ssh2-0.10/ssh2.c:1097: warning: passing arg 4 of `add_assoc_stringl_ex' discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c:1098: warning: passing arg 4 of `add_assoc_stringl_ex' discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c:1106: warning: initialization discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c:1107: warning: passing arg 2 of `zend_hash_add_or_update' discards qualifiers from pointer target type
make: *** [ssh2.lo] Error 1
ERROR: `make' failed
Here i have ceased to continue installing SSH2 for PHP.
I am unsure if the system has been badly effected by this partial installation.
#28. Kevin on 12 October 2007
#27. prasanna on 11 October 2007
phpize && ./configure --with-ssh2 && make.
I run the above command under PECL/SSH directory.
But i have a doubt and i explained it below.
... [more]
Hi,
I am trying to add SSH2 support in my product. I am using Apache 2.0.4.7 with PHP 5.2.4.
For SSH2 support, i had compiled PECL/SSH2 package and created ssh2.so.
I used to run PHP using libphp5.so through httpd.conf. I struck at the creation of libphp5.so file.
<LoadModule php5_module /home/nprasanna/test/modules/libphp5.so>
I downloaded the latest PHP 5.2.4 and install it and got the libphp5.so, but i dont know how to include the ssh2.so while creating libphp5.so.
Please let me know, whether it is possible to create libphp5.so with ssh2.so support and make it work using Apache.
Also i tried by compiling PHP with a custom_php.ini file, (--with-config-file-path). In that custom_php.ini, i specified the ssh2.so as extension=ssh2.so, with the correct path of extension_dir value, failed that case too.
Please guide me how to use shh2.so in libphp5.so. whether we need to specify this at the creation of libphp5.so or we can make this possible by custom php.ini file.
Please suggest me a solution for this.
My email id: prasankn@gmail.com
Thanks,
prasanna
#26. Kevin on 10 October 2007
I believe you've typed:
pecl install ssh
... [more] instead of:
pecl install ssh2
#25. Kevin on 10 October 2007
You should really ask yourself if this technology belongs in a web app b.t.w. And if it does.. Please sanitize your input :)
#24. prasanna on 10 October 2007
I had installed pecl, openssl.
I found no useful results while googling with the below error.
... [more]
No releases available for package "pecl.php.net/ssh"
Cannot initialize 'pecl/ssh', invalid or missing package file
Package "Array" is not valid
install failed
How to resolve it, please help me.
prasanna
#23. Edi on 10 October 2007
but the situation is more complicated:
user can selects which commands to execute from the select list, then the
ssh conection created and the commands are executed, output is presented to the user, and after that, depends on the output, user should select other commands to be executed by the same ssh connection, (I don't know which commands).
I'm using post method and submit the commands that are chosen each time (reloading the same page), so ssh_connect() and auth....should be executed each time and the commands are executed, and it's quite slow.
... [more] maybe this is wrong method , but if there is some way to hold the ssh2 connection (session) each time the page is reloaded, without connecting to remote host again, then it should work well.
thanks in advance for any suggestions.
#22. Kevin on 09 October 2007
#21. Edi on 09 October 2007
Question: Is there a way holding ssh session, in order to execute number of commands, chosen by the user, and not to create ssh connection (reload the page) again each time, to execute another command?
#20. Stefan Ideler on 05 October 2007
(Compiling it on suse 10.2, 64 bit).
http://svn.mandriva.com/cgi-bin/viewvc.cgi/packages/cooker/php-ssh2/pristine/SOURCES/
... [more]
Download the tar, patch the 2 files with the diff files, then install it using phpize && ./configure --with-ssh2 && make && make install
On another note, if you have PEAR installed but not the pecl package, try an pear upgrade-all .
Appearently a number of older versions of PEAR do not include the pecl package.
Regards,
stefan at i3d dot nl
#19. Kevin on 04 October 2007
try:
updatedb && locate php.ini |egrep '^/etc/.*\.ini$'
... [more] To find all the valid php.ini's on your system. If there isn't a cli version (either 4 or 5) maybe you will have to reinstall:
aptitude reinstall php5-cli
or
aptitude reinstall php4-cli
depending on the version you prefer.
#18. Joo on 04 October 2007
However, I can use ssh2* functions in php web page but I can't use them in CLI. I created /etc/php5/cli/php.ini already.
#17. Kevin on 04 October 2007
I see you're using commandline so probably something like /etc/php5/cli/php.ini
#16. Joo on 04 October 2007
- [root@localhost htdocs]# php -f ssh2.php
function ssh2_connect doesn't exist[
... [more] What happened in my php?
Thanks in advance.
#15. Oriol on 23 September 2007
Tancks for your guide is very useful for me.
#14. ServerChief on 13 September 2007
Just wanted to say thank you for this tutorial. I'm writting an application for VmWare ESX Updater Services, this tutorial, without a doubt will help me do it more efficiently.
Thanks,
... [more]
ServerChief
http://www.serverchief.com
#13. Jason on 11 September 2007
Do you have any idea about SSH Tunneling? I tried the following code but don't work:
$connection = ssh2_connect($SSH_HOST, $SSH_PORT);
... [more] ssh2_auth_password($connection, $SSH_LOGIN, $SSH_PASSWORD);
$tunnel = ssh2_tunnel($connection, $REMOTE_HOST.":".$REMOTE_PORT, $LOCAL_PORT);
Could you please help me?
#12. Kevin on 30 August 2007
Anyhow, you might want to concatenate all of your commands with semicolons like this:
ls -al; echo "!BREAK!"; cat /proc/cpuinfo; echo "!BREAK!"; cat /proc/loadavg; echo "!BREAK!"; df -h
... [more] The shell on the other side will just run every command and give one big output.
Then just parse the output in PHP by exploding on "!BREAK!" (see 'explode' function in PHP manual)
#11. trume on 30 August 2007
i need to get the infomation realtime remote in ssh2,and now i need some advise if i can do it with php.
i will very glad if you can send a email, ^_^ my email is aquajamy@gmail.com.
... [more]
thanks.
#10. Kevin on 28 August 2007
/tmp/pear/cache/ssh2-0.10/ssh2.c: In function 'zif_ssh2_methods_negotiated':
/tmp/pear/cache/ssh2-0.10/ssh2.c:483: warning: assignment discards qualifiers from pointer target type
However the building just continues and so they can be ignored. But in your case the 'make' exits with an error before it can run: /bin/bash /var/tmp/pear-build-root/ssh2-0.10/libtool
... [more]
Can it be that you ran into this bug?
http://pecl.php.net/bugs/bug.php?id=11779
#9. jan on 27 August 2007
/tmp/pear/cache/ssh2-0.10/ssh2.c: In function 'zif_ssh2_methods_negotiated':
/tmp/pear/cache/ssh2-0.10/ssh2.c:481: warning: passing argument 2 of 'libssh2_session_methods' makes integer from pointer without a cast
/tmp/pear/cache/ssh2-0.10/ssh2.c:481: error: too many arguments to function 'libssh2_session_methods'
... [more] /tmp/pear/cache/ssh2-0.10/ssh2.c: In function 'zif_ssh2_fingerprint':
/tmp/pear/cache/ssh2-0.10/ssh2.c:536: warning: assignment discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c: In function 'zif_ssh2_publickey_add':
/tmp/pear/cache/ssh2-0.10/ssh2.c:1038: warning: passing argument 1 of '_efree' discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c: In function 'zif_ssh2_publickey_list':
/tmp/pear/cache/ssh2-0.10/ssh2.c:1097: warning: passing argument 4 of 'add_assoc_stringl_ex' discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c:1098: warning: passing argument 4 of 'add_assoc_stringl_ex' discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c:1106: warning: initialization discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c:1107: warning: passing argument 2 of '_zend_hash_add_or_update' discards qualifiers from pointer target type
make: *** [ssh2.lo] Error 1
makes me sad :(
#8. Peter on 14 August 2007
#7. Peter on 14 August 2007
When I run 'pecl install -f ssh2-beta' I get this error:
configure: error: The required libssh2 library was not found. You can obtain that package from http://sourceforge.net/projects/libssh2/
... [more]
Any suggestions?
#6. tr on 13 August 2007
#5. Kevin on 10 August 2007
#4. Will on 09 August 2007
No releases available for package "pear.php.net/ssh2" - package pecl/ssh2 can be installed with "pecl install ssh2"
Cannot initialize 'ssh2', invalid or missing package file
Package "ssh2" is not valid
... [more] install failed
Any help would be great. Thanks...
#3. NiKo on 30 July 2007
#2. Kevin on 28 July 2007
And as for the PHP manual, maybe I will contribute directly to it. Not a bad idea. Thanks for your comment!
#1. Sara Golemon on 27 July 2007
A few notes:
Yes, 0.15 is out, with several refactoring points and I need to get around to updating the PHP extension to take advantage of new stuff in the library.
... [more]
Part of the reason for the refactorings is that some of the cURL developers have gotten involved in its development and wanted to make it better before building support for it into cURL (which...by the way, cURL now supports SFTP via libssh2).
SCP doesn't work entirely right. I recommend SFTP to anyone who has it available. It's a much more robust protocol and libssh2's implementation of it works much more solidly than it's implementation of SCP. (If ANYONE has a spec document describing SCP or RCP, I'd love to get a link...)
As to docs... Yeah... they could definately stand to be better... You're welcome to contribute directly to the PHP manual. CVS repository can be browsed at http://cvs.php.net/phpdoc/en/reference/ssh2 , anonymous CVS access instructions are at http://php.net/anoncvs.php, and instructions for getting your own cvs account can be found at http://php.net/cvs-php.php . You can also, of course, just send diffs (unified) to the phpdoc mailing list and ask someone politely to commit it for you.