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 practicle 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
Update
On recent Ubuntu machines, there’s no need to do any compiling anymore:
1
| |
You can now test if PHP recognizes it’s new ssh2 extension by running:
1
| |
It should return: ‘ssh2’
If the above works for you (you should see also: “Build process completed successfully”), you can skip to: Great! PHP supports SSH - time to code.
Otherwise we need to compile manually, continue reading here.
Prerequisites
Packages
First let’s install the following packages:
1
| |
That should set us up alright.
libssh2
We need libssh2 from sourcefourge. We have to compile this, but no worries, this is all you need to do:
1 2 3 4 5 6 | |
That’s it! Easy right?
- Update: since December 26th 2008, libssh2 has reached version 1.0. Though I have not tested it: it has been reported to work. So you may want to check sf.net and download the latest stable version.
Installation
ssh2.so
Next we need to link libssh & PHP together. There’s a PECL module for this so let’s install using:
1
| |
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 a
php.ini file (I’d recommend a separate one: /etc/php5/conf.d/ssh2.ini).
Make sure it reads:
1
| |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
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:
1 2 3 | |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
In the example above, you’d better set stream_set_blocking to false.
Can’t get enough?
PHP can send files over ssh!
1 2 3 | |
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 line is in place in the file:
/etc/ssh/sshd_config:PasswordAuthentication yes
If you’ve made any changes, ssh needs a restart
1
| |
Post a comment here if it’s still failing. Don’t forget to paste the error that you’re getting.
make: *** [ssh2.lo] Error 1
If you get the error:
1 2 | |
that’s because of PHP 5.3 incompatibility. Try this patch:
1 2 3 4 5 6 7 8 | |
make: *** [ssh2_fopen_wrappers.lo] Error 1
If you get the error:
1 2 3 | |
This is the reported fix (thanks to BuNker).
Alternatives
There have been some additional developments since the writing of this article. Checkout:
- Net_SSH2, PEAR’s SSH wrapper (driver based to support multiple ways of establishing the connection)
- SSH2, another wrapper by Jaimie Sirovich
- phpseclib, a pure PHP implementation - no additional libraries, binaries, bindings required (against all odds, still pretty fast with mcrypt installed)
Imported comments
These were imported from my old blog. Please use disqus below for new comments
Cosmin
on 2012-09-30 00:30:09
How to command action url ?
example 127.0.0.1/test.php?shell=reboot
reply me. thanks you
soapdish
on 2012-09-08 07:50:31
Hi there. All works really well, but if I echo $data back to the screen to list the output of the command I get no carriage return. Do you have any tips for adding the new line and manipulation of the text after it has gathered the output from the command?
JeffQuer
on 2012-07-23 22:04:06
Thanks, Kevin. You saved a 5 day trouble for me.
comment
on 2012-07-22 18:14:35
Libshh has moved its files from sourceforge
http://www.libssh2.org/download/libssh2-1.4.2.tar.gz
zanzo
on 2012-05-29 12:36:14
Thanks Kevin,
my problem was the ‘PasswordAuthentication’ set to No in the sshd_config.
Jimmy
on 2012-05-19 16:04:40
Kevin, great job!.. Very helpful article.
I was getting function ssh2_connect doesn’t exist even though I had gotten &
quot;ssh2&
quot; on
php -m |grep ssh2
It turned out my PHP installation was still missing some packages (I am using Debian 6.0 on SPARC). I solved it by running:
apt-get install php5-dev
This installed and updated a number of libraries in my PHP stack. Now your PHP script goes past the first line.
Great job and thanks for sharing!
Akshay Behera
on 2012-05-15 13:43:12
Hi,
I m able to login using ssh2_connect but while executing a command it fails.
$stream = ssh2_exec($con, &
quot;mml&
quot;) not working.
What should I do ?
How can I throw multiple commands ?
ilan
on 2012-04-14 02:21:23
hi,
i did all the tutorial step by step and i got this error:
function ssh2_connect doesn’t exist
i think i didn’t do this part as you said:
Now you need to make sure our new ssh2.so module is loaded by PHP. Edit a php.ini file (I’d recommend a separate one: /etc/php5/conf.d/ssh2.ini). Make sure it reads:
extension=ssh2.so
i did it like this:
/usr/local/lib/php.ini
i add this line:
…
;extension=php_soap.dll
;extension=php_sockets.dll
;extension=php_sqlite.dll
;extension=php_sybase_ct.dll
;extension=php_tidy.dll
;extension=php_xmlrpc.dll
;extension=php_xsl.dll
;extension=php_zip.dll
;extension=ssh2.so
extension=ssh2.so
….
please help me
aneeq
on 2012-04-10 11:18:24
Hi, I have found an excellent solution which is as below.
&
lt;?php
$server = “ftp.you-server.com”; //address of ftp server (leave out ftp://)
$ftp_user_name = “userName”; // Username
$ftp_user_pass = “passwordHere”; // Password
$source = “/home/folder/public_html/filename.ext”;
$dest = “/public_html/filename.ext”;
$mode=”FTP_BINARY”;
$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
if (!$connection || !$login) { die(‘Connection attempt failed!’); }
$upload = ftp_put($connection, $dest, $source, FTP_BINARY);
if (!$upload) { echo ‘FTP upload failed!’; }
ftp_close($connection);
echo “done”;
?&
gt;
Source:
&
lt;a href=&
quot;http://phphelp.co/2012/04/09/how-to-do-server-to-server-ftp-transfer-in-php/&
quot;&
gt;http://phphelp.co/2012/04/09/how-to-do-server-to-server-ftp-transfer-in-php/&
lt;/a&
gt;
&
lt;a href=&
quot;http://addr.pk/af97&
quot;&
gt;http://addr.pk/af97&
lt;/a&
gt;
finnq
on 2012-03-17 11:00:12
Oh, a very very good article I like it. But it shows me only the connection were broken. There’s no php error or other…
What should I do?
Ankit
on 2012-03-12 23:58:54
Hi Kevin,
A very informative article indeed..great work. I’m trying to use this library to start a ‘netconf’ subsystem. But looks like, can’t start any subsystem other than sftp. Any ideas?
Regards
Ankit
Luis
on 2012-02-23 06:31:56
Hi Kevin,
Thank you for all your help , it has been a key to solve the problem that I am trying to solve. However, I have a question : I have tried to run the scripts you have above because I want to connect to a router terminal from a remote client and activate a spectral scan in order to know what are the busiest channel.
The problem is that I havent could to get the results of the CLI, it seems that it is running, and I tested the connection also. Do you know if it something related to the router terminal? or it should run fine no matter what kind of terminal Im using.
If you have any answers, I will appreciate it.
Thanks,
Luis
ahmed
on 2012-02-14 15:35:20
hello ,
the Method 1: Execute code worked for me like a charm , but i have an important question
it did not excute a command it just said okay: logged in… , Even when i tried it with root .
the Most important thing for me , when it login it did Not display the last login date and time like usual in terminal applications . and i really want to display that output because i included some important data within it .
kurokysan
on 2012-01-02 21:32:10
This is the error i get:
function ssh2_connect doesn’t exist
Moeller
on 2011-12-31 15:25:56
Thx. That FAQ is Great and very helpfull.
greetings from Germany
Rain
on 2011-11-14 02:44:04
Hello, I have used ssh2_scp_recv to receive file. but if the password is incorrect, it seems cost too much time for waiting response. is there any solutions? Thanks.
Patrick
on 2011-10-07 10:45:17
Thanks for this KVZ,
My question is how can ensure that the my output appears like below in my interface
-rw-r–r– 1 root root 813K Sep 6 2010 idle.txt
-rw-r–r– 1 root root 184M Jul 29 18:06 ips.txt
keeping the formatting in my interface rather than having it in one long string.
Amir
on 2011-08-22 20:19:43
Thanks Much !
knuthmorris
on 2011-07-05 07:15:46
phpseclib so totally owns libssh2 it’s not even funny. urdd posted some benchmarks over a year ago that demonstrated this but in the time since urdd’s post phpseclib’s increased the margin even more significantly:
http://core.trac.wordpress.org/ticket/16925#comment:7
http://core.trac.wordpress.org/ticket/16925#comment:8
If you’re not using phpseclib then you’re making a big mistake.
Kevin
on 2011-04-17 16:36:57
@ Echo &
amp; Alberto: Seems like the connection ca’t be established. Check if you can connect manually.
@ Nelson: You have faulty stack. You need pear &
amp; php-dev successfully installed before trying anything with the ssh2 module
Nelson
on 2011-04-05 05:54:49
New error
Starting XAMPP for Linux 1.7.1…
PHP Warning: PHP Startup: ssh2: Unable to initialize module
Module compiled with module API=20090626, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0
Nelson
on 2011-04-05 05:23:06
Problems here…
WARNING: failed to download pecl.php.net/ssh2 within preferred state &
quot;stable&
quot;, will instead download version 0.11.2, stability &
quot;beta&
quot;
downloading ssh2-0.11.2.tgz …
Starting to download ssh2-0.11.2.tgz (22,740 bytes)
……..done: 22,740 bytes
5 source files, building
running: phpize
sh: phpize: not found
ERROR: `phpize’ failed
Beshoy
on 2011-03-22 21:07:03
Thank you!
netadmin
on 2011-03-15 21:44:18
Work perfectly for me thanks!
Alberto
on 2011-03-09 12:05:40
I have the same problem of Echo
Echo
on 2011-03-07 14:37:08
Warning: ssh2_connect() [function.ssh2-connect]: Error starting up SSH connection(-1): Failed sending banner in /var/www/get.php on line 17
Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 192.168.7.7 in /var/www/get.php on line 17
fail: unable to establish connection
i used the first code example in this article.
Thank you!
Kevin
on 2011-03-04 13:06:01
@ tony: Looks like your account is restricted by rssh. Try the same code against a different server / account.
tony
on 2011-01-25 03:56:22
OK. Forget my previous posts. Installed phpseclib and got it going first time.
Tony
tony
on 2011-01-25 02:13:47
With reference to my last post (#314) I have also tried the method using stderr given in the php documentation. The output stream is empty and stderr contains the text &
quot;This account is restricted by rssh….&
quot;, as before. I’ve now spent about 15hrs on this and would really like some inspiration.
Thanks
Tony
tony
on 2011-01-22 10:43:01
I have done everything you suggest and it appears to work. I appear to be logging i to the server ok. Using the shell script above I get a reply from the server telling me my last login date and time, but no commands are executed:
&
quot;Last login: Sat Jan 22 18:54:34 2011 from cpe-139-168-211-92.lns1.way.bigpond.net.au This account is restricted by rssh. Allowed commands: scp sftp rsync If you believe this is in error, please contact your system administrator. &
quot;
I can access the server with a terminal sftp command without problems. Any suggestions please?
Thanks
Tony
Gerardo H.
on 2011-01-05 02:54:18
I installed the xampp web server package on an ubuntu(Hardy) machine and this is what I did to solve the:
‘function ssh2_connect doesn’t exist’ error.
1- Followed each step on this great How-to. When running the command ‘pecl install -f ssh2’ took note of the path where the ssh2.so file was installed.
2-Modified the php.ini file on the path (/opt/lampp/etc) and added the following line: extension=ssh2.so
3- Restarted the apache service using the following commands:
sudo /opt/lampp/lampp stopapache
sudo /opt/lampp/lampp startapache
This restarted apache, and when it was starting again, it shown an error trying to find the ssh2.so file. Took note on the path it was looking for.
3-copied the ssh2.so file from the path where it was installed (in my case /usr/lib/php5/20060613+lfs/) to the path where the apache was trying to find it in the previous step.
4- restarted apache.
And it worked !!!
Regards
Marcin
on 2010-12-27 12:41:40
hey, i have a problem in my site…
log: PHP Warning: Module ‘ssh2’ already loaded in Unknown on line 0
could you help me?
thanks ;)
edubidu
on 2010-11-30 09:57:33
Hi,
Great this howto! Just the wget for libssh2 has changed:
wget http://www.libssh2.org/download/libssh2-1.2.7.tar.gz
deepthi
on 2010-11-15 08:12:58
i installed LIBSSH and SSH from PECL: PHP also copies ssh.so to /usr/local/lib/php and also changed php.ini ….but i still get this error
PHP Warning: PHP Startup: Unable to load dynamic library ‘./ssh2.so’ - ./ssh2.so: cannot open shared object file: No such file or directory in Unknown on line 0
deepthi
on 2010-11-15 08:09:57
hey…could give steps to follow to configure PHP with SSH on fedora, but NOT WITH YUM…coz i dont hv net connection to my fedora
yasg
on 2010-11-10 10:41:46
@Kevin
thanks Kevin , yes it works now ^_^
thank u thank u a lot
Kevin
on 2010-11-09 12:17:36
@ yasg: Judging from your output it really seems to ignore any kind of newline though. Maybe this system needs &
quot;\r\n&
quot; or something..
Also try if it helps to put a sleep between commands. Some crappy routers do a bad job at buffering &
amp; blocking and need to be handled with extreme care.
Otherwise you seem to be doing it right.
yasg
on 2010-11-08 13:56:33
@Kevin
I wrote it using the new line &
quot;&
quot;\n&
quot;
but the backslash here doesn’t appear after the submission
fwrite($shell, &
quot;clp&
quot;\n&
quot;);
fwrite($shell, &
quot;start server3&
quot;\n&
quot;);
… [more] also I’ve tried
fwrite($shell, &
quot;clp&
quot;.’&
quot;\n’);
fwrite($shell, &
quot;start server3&
quot;.’&
quot;\n’);
but still doesn’t exe.
yasg
on 2010-11-08 13:53:42
@Kevin
I wrote it using the new line &
quot;\n&
quot;
but the backslash here doesn’t appear after the submission
fwrite($shell, &
quot;clp\n&
quot;);
fwrite($shell, &
quot;start server3\n&
quot;);
also I’ve tried
fwrite($shell, &
quot;clp&
quot;.’\n’);
fwrite($shell, &
quot;start server3&
quot;.’\n’);
but still doesn’t exe.
Kevin
on 2010-11-08 13:31:13
Antonio Carlos: What if you try the version that the article uses?
yasg: You probably need a &
quot;\n&
quot; to execute the commands
yasg
on 2010-11-08 11:56:07
Thanks Kevin, this only the article that helped me so much, but i’m stuck with the execution process.
I’m trying to execute some commands on a server through the router .
which means i have a router ip address and i’m gonna execute a command on for example &
quot;power on&
quot; server3 through this router .
i’v tried the example with Shell method but its still doesn’t work .
it connect successfully
okay: logged in…
but it doesn’t execute the command
this what i test :
stream_set_blocking($shell, true);
// send a command
fwrite($shell, &
quot;clp\n&
quot;);
fwrite($shell, &
quot;start server3\n&
quot;);
and the buffer output is :
[20l eSH& gt; clpstart server3
could you help me plz
Antonio Carlos
on 2010-11-04 20:07:19
I’m using version libssh2-1.2.7 but am having to run the script error
by apache. PHP works fine.
Error menssage:
——————————————-
[Thu Nov 04 15:03:43 2010] [error] [client 9.18.218.31] PHP Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 9.18.218.20 on port 22 in /var/www/html/teste_ssh01.php on line 11
[Thu Nov 04 15:03:43 2010] [error] [client 9.18.218.31] PHP Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 9.18.218.20 in /var/www/html/teste_ssh01.php on line 11
——————————————-
Script:
&
lt;?php
//Tecmaster
$server = ‘9.18.218.10’;
$user = ‘root’;
$pass = ‘pass’;
if (!function_exists(&
quot;ssh2_connect&
quot;)) die(&
quot;function ssh2_connect doesn’t exist&
quot;);
if(!($con = ssh2_connect($server, 22))){
echo &
quot;fail: unable to establish connection\n&
quot;;
}
else {
if(!ssh2_auth_password($con, $user, $pass)) {
echo &
quot;fail: unable to authenticate\n&
quot;;
} else {
// allright, we’re in!
echo &
quot;okay: logged in…\n&
quot;;
// execute a command
if (!($stream = ssh2_exec($con, &
quot;df -h&
quot; ))) {
echo &
quot;fail: unable to execute command\n&
quot;;
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = &
quot;&
quot;;
while ($buf = fread($stream,4096)) {
$data .= $buf.&
quot;”;
echo $buf.”&
quot;;
}
fclose($stream);
}
}
}
?&
gt;
Mike Stewart
on 2010-10-25 21:53:31
Thanks, these instructions are the only ones I’ve found that will actually set up php/ssh2 without hassle.
John Aristizabal
on 2010-10-22 18:11:59
Excellent! This single article helped me so much, I’m doing an IPTV app that stores the media data on a cloud computing server (Eucalyptus in Ubuntu) and finally I can execute the cloud commands through the video server. I will put you in the credits of my undergraduate project. tyvm.
VirtualMachine
on 2010-10-19 14:56:22
Hi,
i tried your example script for the Shell Method but after the okay: logged in… method nothing is happening anymore
Even with the timeout script i didnt get anything back. And no bugs in the logfiles
any idea ?
Reiko
on 2010-10-12 17:02:44
Would anybody happen to know how to send Control Characters?
For example, say I wanted to run screen from PHP, how would I successfully emulate a Ctrl+A &
gt; D press? I’ve tried quite a few methods, such as sending chr(1) or ^A, each time the foreground process just shrugs at me.
Any suggestions would be awesome.
noname
on 2010-10-11 15:06:45
phpseclib is a good alternative
Kevin
on 2010-10-11 13:47:04
John: Sorry, you need to provide way more details for us to be able to help you.
John
on 2010-09-15 23:34:08
Hmm, what to do when avrything is ok with connection, commands executing one time on 10 times?. one time on refresh command execute and next 5 time refresh dont?.
Kevin
on 2010-09-08 21:42:58
@ Sam: telnet is a bad idea. unless you want your server hacked real bad : )
did you include the so file in your php.ini??
Sam
on 2010-08-16 20:06:29
Hi,
I've tried this code with the patch, but each time when I execute it says \&
quot;function ssh2_connect doesn't exist\&
quot;. I've used the patch provided and the patching went successful. But couldn't connect to ssh. I have also tried both shell and execute methods. Earlier I used to connect to unix server through PHPTelnet, but my school admin removed access using PHPTelnet, so I need to rely on ssh.
Any inputs?
dusinnhht
on 2010-08-16 17:52:27
OK It's working!
Today i got 25 updates installed, and now ssh2's working!
So thanks for the tutorial! :)
dusinnhht
on 2010-08-16 15:11:41
Hi everybody!
I'm pretty new to linux(debian) and php, but somehow i managed to create the ssh2.so file. I copied that file into all php extension dirs, to make sure php finds it(i tried several locations, they didn't work).
If I create an index.php file and put
&
lt;?php
if (!function_exists(\&
quot;ssh2_connect\&
quot;)){
print \&
quot;function ssh2_connect doesn\'t exist\&
quot;;
}else{
print \&
quot;function ssh2_connect exist\&
quot;;
}
?&
gt;
in it and I open it from a browser, then i get
function ssh2_connect exist.
I got the same answers with the ssh2_auth_password and the ssh2_auth_nun functions, but if I try to use the ssh2_auth_password, the script doesn't run.
Now I'm confused, becouse I have 3 php.ini-s. I edited the one located in /etc/php5/apache2 , added \& quot;extension=ssh2.so\& quot; and added to the one that you recommended (that one had already that line), and tried to turn the error messages on by deleting \& quot;;\& quot; in the line Developer Value=on, but it didn't show any errors.
(I restarted apache2 and ssh2 by each trying.)
I edited the /etc/ssh/sshd_config file and uncommented the PasswordAuthentication yes line, but it didn't helped either.
So I decidet to give up, and try luck with phpseclib.
I downloaded the \& quot;package\& quot;, untgz-d it and copied everything to php extensions dir, but no luck with that either; the example script runs, but I got nothing back.
Kevin
on 2010-08-12 12:38:53
@ Abdullah: Can't debug without actual code &
amp; output.
@ andi, chakan, Harleyquine: Thanks : )
@ Debbie D: Don't know about Wordpress. But you can use this method to connect to SSH without FTP enabled.
Harleyquine
on 2010-08-11 12:48:03
I was scouring the web looking for the right way to get SFTP working with my PHP and it just wasn't working.. till I found this. Thanks very much :)
chakan
on 2010-08-07 10:17:33
It's work for me too.
Thank you :)
andi
on 2010-08-04 20:19:55
ist works fine for me!
thank your for your instruction.
best references on the internet! great
best regards from germany
andi
Debbie D
on 2010-07-15 23:44:28
What if the server has FTP OFF by defualt, will this method still work to upgrade WP??
Abdullah
on 2010-07-13 09:03:41
Shell method is not working for me.
when i press the connect button, the page just keeps on looking for localhost.
but when i use execute method the command executes perfectly.
Kevin
on 2010-06-29 13:27:11
@ Josh: Try patching the file by hand, or obtaining a version that's compatible with the patch.
@ Rutvij Clerk: You could use the shell method to interact with a shell, or execute multiple mysql commands like: mysq –defaults-file=/etc/mysql/debian.cfn -e 'use db; select * from xyz;'
Rutvij Clerk
on 2010-06-29 01:27:28
Hi Kevin,
I am using the execute method code to connect to a mysql server after I login using ssh. It works perfectly except for a slight glitch. After I ssh into the server, I want to use the mysql client to issue a couple of commands. Here in this example only one command is executed. How do i execute multiple commands? I am fairly new to PHP.
Josh
on 2010-06-28 19:49:54
When I type
pecl install -f ssh2
I get the error…
/usr/include/php5/Zend/zend_API.h:360: note: expected ‘char *’ but argument is of type ‘const char *’
make: *** [ssh2.lo] Error 1
ERROR: `make\' failed
I saw that there is a patch for this error but it did not seem to work. I did notice that there is a difference between my error and the example error. Mine says …
/usr/include/php5/Zend/zend_API.h:360...
While the example says…
/usr/include/php5/Zend/zend_API.h:361...
Would that make a difference?
Kevin
on 2010-06-10 20:34:08
@ Rick: Try SELinux related forums, or a distro that comes without SELinux.
@ Rob: Nginx probably uses php-cgi, so it could be a different ini file to load the ssh2.so. Otherwise: no difference
Rob
on 2010-06-04 13:08:16
Great article - really is and it's referenced all over the web; however I don't use Apache, I use nginx instead so want to get some form of confirmation that using the above would work exactly the same for me since I'm relatively new to unmanaged VPS solutions.
Cheers!
Rick
on 2010-05-25 10:16:17
i saw this when i tried to run the script above..
Anyone know how to change this ?
======
May 25 15:16:49 localhost setroubleshoot: SELinux is preventing the http daemon from connecting to network port 22 For complete SELinux messages. run sealert -l 6f69fd46-929a-439a-b8de-13129b92496f
rachael
on 2010-05-24 22:36:34
This is a great piece of writing!! totally worked for my wp install. You're a legend!
Rick
on 2010-05-24 05:21:50
@ Rick: Not enough data to debug anything, sorry.
=============
Kevin,
What datas do you need?
if
i use shell :
==================
[root@localhost html]# php ssh2.php
PHP Warning: Module 'ssh2' already loaded in Unknown on line 0
okay: logged in…ntotal 2768
==================
it works….
confuse me…..
Thanks for the reply…
=====
Kevin
on 2010-05-15 19:26:28
@ Rick: Not enough data to debug anything, sorry.
@ Christina: Wow, you're really lost here :) Try a wordpress support forum or something. I have nothing to do with that software.
Christina
on 2010-05-11 14:46:04
@kevin Every time I try to upgrade wordpress it doesn't give me the option to select sftp to update wordpress is just gives FTP FTPS (SSL) connection type. I havent been able to update…
Rick
on 2010-05-07 05:37:26
I try to connect with this…
the 192.168.1.13 is alive and open for ssh
&
lt;?php
$con = ssh2_connect(\&
quot;192.168.1.13\&
quot;,22)
//echo $con;
?&
gt;
but i got this :
[Fri May 07 04:41:42 2010] [error] [client 192.168.1.3] PHP Warning: ssh2_connect(): Unable to connect to 192.168.1.13 in /var/www/html/test.php on line 2
anybody can help me ?
thx a lot
Kevin
on 2010-04-29 20:48:56
@ Christina: You should probably go to a wordpress forum. You don't need php-ssh stuff to just upgrade wordpress.
Kevin
on 2010-04-29 20:43:52
@ BuNker: Ok thanks for sharing!
Christina
on 2010-04-28 14:13:47
I am so new with this stuff but I need some help! I am okay until I get to \&
quot;Great! PHP supports SSH - time to code\&
quot;
Am i sopose to create a new php file? or is this something I write in terminal? Im sorry if that is a dumb question but I really need to figure out a solution for this because I cant update my wordpress…
martin
on 2010-04-26 13:29:29
thanx alot man….your tutorial works great.
but in my case,there's several adjustment.
here's my version:
-i use the latest version of libssh2 to works with command pecl install -f ssh2 (didnt work well with libssh2 0.14)
-dont forget to install openssl-dev
BuNker
on 2010-04-24 13:24:54
hey, fix it! complited make configure.
cvs fix(path) \&
quot;ssh2_fopen_wrappers.c\&
quot; file, \&
quot;ssh2-0.11.0.tgz\&
quot;
http://cvs.php.net/viewvc.cgi/pecl/ssh2/ssh2_fopen_wrappers.c?r1=1.15&
amp;r2=1.16&
amp;diff_format=u
If this is done
phpize &
amp;&
amp; ./configure --with-ssh2 &
amp;&
amp; make
Kevin
on 2010-04-24 10:32:12
@ hi1278 &
amp; BuNker: If you have apt, try to not to compile. Otherwise try a different version of libssh.
If all else fails: move to phpseclib, I personally think it's pretty good.
BuNker
on 2010-04-23 18:54:28
hey, im probleme…
http://pastebin.com/uj0bj0wE
Asim Zeeshan
on 2010-04-23 15:52:28
[b]Thanks a lot mate! you are a life saver[/b]. I will be blogging about this article very soon :)
Thanks once again
jjzd2w
on 2010-04-22 18:18:56
Coleages :
in tried briged to web app using ssh2 lib an ifound a library write in pure php implent a ssh2 conector \&
quot; phpseclib\&
quot; well in tried unsing with the brigdge in ssh2.so but not works the phpseclib works fine
Good luck!
hi1278
on 2010-04-08 18:12:40
Hi Kevin,
Thanks for the detailed steps. I am facing an issue in the installation. \&
quot;pecl install -f ssh2\&
quot; failed with the following make error. ( I am running this in Debian). All the previous steps went fine. I would really appreciate
any help.
/tmp/pear/download/ssh2-0.11.0/ssh2_fopen_wrappers.c: In function 'php_ssh2_channel_stream_read':
/tmp/pear/download/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: 'LIBSSH2_ERROR_EAGAIN' undeclared (first use in this function)
/tmp/pear/download/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: (Each undeclared identifier is reported only once
/tmp/pear/download/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: for each function it appears in.)
make: *** [ssh2_fopen_wrappers.lo] Error 1
ERROR: `make' failed
Kevin
on 2010-04-04 17:03:56
@ kmasiak: If you pastebin the full code we may be able to establish why. Or not. But glad it works anyway now :)
kmasiak
on 2010-03-29 16:18:09
@Kevin
I've tried it before, but with no luck. It looped the page, with simple command with just a little characters of result.
But after your point i gave it a closer look, i've erased the while() {} and did just a simple test like:
$buf = fread($shell,8096); and now it reads me all the output i need. I'm just curious why it makes infinite loop for me.
Thanks!
Kevin
on 2010-03-29 14:38:19
@ kmasiak: You need the shell method for most routers as I explain in the article.
kmasiak
on 2010-03-29 14:30:06
very nice sample codes, works perfectely on linux.
but doesnt do the thing on netscreen - you know the junipier routers.
It connects perfectely, but command exec doesnt work… this difficulity may be provided by netscreen's shell… cant get *any* ssh lib to execute command.
i've just found one way to send command to this os by sharpssh (C# .net library).
however it would be pretty nice if somebody found any solution fetching shell_exec result from netscreen box, pls msg me. for now, it simply loops the page and nothing happens till timeout - no php errors even.
cheers.
Kevin
on 2010-03-27 13:21:12
@ Brian Ellis: Have you tried playing around with the options a bit? e.g.: vt100?
Also, why are you only getting the first 1024 bytes?
Brian Ellis
on 2010-03-26 00:06:52
I'm successfully using this to connect to a remote server via SSH. It runs a ssh_exec ok, but the stream that I get back appears to be encrypted somehow.
$stream = ssh2_exec($connection, '/usr/bin/uptime', 'vt100');
if ($stream == FALSE)
{
$message = \&
quot;ERROR: Cannot execute remote command.\&
quot;;
return(FALSE);
}
$message = stream_get_line($stream, 1024);
I get back something that looks like this:
Message is �( �(�Prr^H��&
lt;MA�k�l��rr^��M&
lt;�n�8��rr^� �Rc�lE��&
lt;RA�k���rr^�x�&
lt;RA�k�l�rr^���R&
lt;�k�P�Z�k�P�~�k�P���k�P���k�PMessage is �( �(�PH�V�]�ݐRLą��ߟ�6���߉G��F������q�7z�u���l�z�v\&
quot;8�&
gt;��E8N-�@ ]�W݄G�Qd�J�m���'����&
lt;;�CQ}��*����(*�1�(��A�F�y��?�`�38�=�p��vy��� 9ːD��&
amp;y|���t9����������]�
Kevin
on 2010-03-24 10:08:51
@ bedong: Could be differences between your php.inis or your server preventing you to open specific ports.
You could try the native php implementation to determine what's wrong.
bedong
on 2010-03-22 20:37:51
It is a great post. I can successfully create a ssh connection within my php code by following your instruction. But when I try to create a web service by using the same code, I got \&
quot;fail: unable to establish connection\&
quot; error. In other words, if I run the command \&
quot;php index.php\&
quot; under shell prompt, it works perfectly; but if I request the same file through my Web browser (http://mydomain/index.php), I got an unable to connect error. Do you have any idea about that. Thanks a lot for your help.
Kevin
on 2010-03-14 17:14:53
@ Kyle: You should debug it by adding echo's &
amp; dumping variables inside the while loop, see what really is the show-stopper.
@ John: You could use the sftp_ functions to download files over ssh. Although it seems that these kind of tasks belong more in CLI scripts than web interfaces. You could schedule them from web interfaces though.
@ urdd: Very interesting indeed. I think I'd have to agree with your conclusion. The fact is I've personally switched to phpseclib a few months ago, and though it feels incredibly dirty, I must say it's been working reliably since. Thanks a lot for sharing this!
urdd
on 2010-03-13 06:47:51
&
lt;?php
include(\'Net/SFTP.php\');
$sftp = new Net_SFTP(\'www.domain.tld\', 22);
$sftp-&
gt;login(\'user\', \'pass\');
$start = microtime(true);
$sftp-&
gt;put(\'test\', str_repeat(\'a\', 256 * 1024));
$elapsed = microtime(true) - $start;
echo \&
quot;took $elapsed seconds\\r\\n\&
quot;;
$connection = ssh2_connect(\'www.domain.tld\', 22);
ssh2_auth_password($connection, \'user\', \'pass\');
$sftp = ssh2_sftp($connection);
$start = microtime(true);
$stream = fopen(\&
quot;ssh2.sftp://$sftp/home/user/test\&
quot;, \'w\');
fputs($stream, str_repeat(\'a\', 256 * 1024));
fclose($stream);
$elapsed = microtime(true) - $start;
echo \&
quot;took $elapsed seconds\\r\\n\&
quot;;
?&
gt;Per that, it looks like phpseclib is faster? Here's what I get when running that:
phpseclib: took 4.4995489120483 seconds
libssh2: took 5.706494808197 seconds
That's with the mcrypt and gmp extensions, but still… it doesn't speak very highly of libssh2 that it's bested by a pure-PHP implementation.
John
on 2010-03-12 10:38:02
Hi Kevin,
\&
quot;Method 1: Execute\&
quot; script seems to be working fine for me. But the \&
quot;Method 2: Shell\&
quot; is not working, meaning when i executed the php script via browser, the browser keeps on loading.
I have a requirement where I wanted to get a file (3.5 GB) from external server. This can be done only via SSH, i.e. using SSH connect the external server via FTP and download the file.
Note: I could not use wget command to download the file, as the file cannot be accessed via URL. So the only options is via FTP.
Please let me know your thoughts.
//John
Kyle
on 2010-02-28 23:52:41
Hey Kevin, great blog, I enjoy reading it. One quick question, I'm trying to output the data of a simple \&
quot;whoami\&
quot; execution, but it's just blank. All I see is: \&
quot;okay: logged in\&
quot; and that's it. Here's my code:
&
lt;?php
error_reporting(E_ALL);
if (!function_exists(\&
quot;ssh2_connect\&
quot;)) die(\&
quot;function ssh2_connect doesn\'t exist\&
quot;);
// log in at server1.example.com on port 22
if(!($con = ssh2_connect(\&
quot;myserver.com\&
quot;, 22))){
echo \&
quot;fail: unable to establish connection\\n\&
quot;;
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, \&
quot;username\&
quot;, \&
quot;password\&
quot;)) {
echo \&
quot;fail: unable to authenticate\\n\&
quot;;
} else {
// allright, we\'re in!
echo \&
quot;okay: logged in...\\n\&
quot;;
// execute a command
if(!($stream = ssh2_exec($con, \&
quot;whoami\&
quot; )) ){
echo \&
quot;fail: unable to execute command\\n\&
quot;;
} else{
// collect returning data from command
stream_set_blocking( $stream, true );
$data = \&
quot;\&
quot;;
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
echo $data;
fclose($stream);
}
}
}
?&
gt;
Kevin
on 2010-02-28 11:52:28
@ Jared: Thanks for sharing!
Jared
on 2010-02-26 21:10:41
Awesome page! Just got this working on an 8.04 server, and thought I'd give you some feedback.
Here's all I had to do to get it working
apt-get install php5-dev php5-cli php-pear build-essential libssh2-1
pecl install -f ssh2
edit php.ini
/etc/init.d/apache2 restart
That's it!
I couldn't find a package named libssl2-php, but that didn't seem to matter.
I had previously installed a package named libssh-2 - I'm not sure if that made any difference.
You need to mention that apache needs to be restarted after modifying the php.ini file.
You might suggest creating a php file with the the first line of your method 1 script to test and see if everything is working.
Kevin
on 2010-01-07 18:55:45
@ Derak: May be that you didn't enable it in the right php.ini. You may have several.
@ David: Try the pure php implementation, you'd be surprised how good it works :)
@ khalid: Thanks but the article already mentions that
khalid
on 2010-01-07 15:31:46
hello
ssh2_auth_password doesn't work,
i do this :
PasswordAuthentication yes
and
/etc/init.d/ssh restart
and it works perfect.
thanks
David
on 2009-12-31 17:39:58
Unfortunately this process no longer works on Ubuntu. \&
quot;pecl install -f ssh2\&
quot; fails to make. Sounds like a recurring problem over the course of years.
Derak
on 2009-12-24 20:37:05
I'm working with Mac OS X. I can install everything fine with no errors. When I type php -i | grep ssh2 I see ssh2 in my Registered PHP Streams. But with I go to my phpinfo() page in my browser ssh2 does not show up in my Registered PHP Streams. Any ideas?
Alfredo Rivera
on 2009-12-14 05:04:00
Any Idea On How To Solve It?
Kevin
on 2009-12-13 17:40:18
@ Matt Kukowski: I didn't like the idea of a pure PHP implementation, but like you god fed up with breakage and am actually quite happy with it : )
@ Alfredo Rivera: Seems like you're mixing stream &
amp; exec methods.
Alfredo Rivera
on 2009-12-12 21:28:23
Hello Kevin, I have Been Writing a CodeIgniter Library For The SSH2 Extension and I'm Encountering some troubles.
&
lt;?php
class Ssh2
{
function __construct()
{
$this-&
gt;ci =&
amp; get_instance();
}
function conect($server,$port,$muser,$mpass)
{
if(!($con = ssh2_connect($server,$port))){
echo \&
quot;&
lt;font color=\'red\'&
gt;&
lt;b&
gt;Failure: Unable To Connect&
lt;/b&
gt;&
lt;/font&
gt;\&
quot;;
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con,$muser,$mpass)) {
echo \&
quot;&
lt;font color=\'red\'&
gt;&
lt;b&
gt;Failure: Unable To Authenticate&
lt;/b&
gt;&
lt;/font&
gt;\&
quot;;
} else {
// allright, we\'re in!
echo \&
quot;&
lt;font color=\'green\'&
gt;&
lt;b&
gt;Success: Logged In, Connected At \&
quot;.$server.\&
quot; With User \&
quot;.$muser.\&
quot; And Password \&
quot;.$mpass.\&
quot;&
lt;/b&
gt;&
lt;/font&
gt;\&
quot;;
}
}
}
function execute($server,$port,$command)
{
$con = ssh2_connect($server,$port);
if(!($stream = ssh2_exec($con,$command)) ){
echo \&
quot;&
lt;font color=\'red\'&
gt;&
lt;b&
gt;Failure: Unable To Execute Command&
lt;/b&
gt;&
lt;/font&
gt;\&
quot;;
} else{
// collect returning data from command
/*stream_set_blocking($stream);*/
$data = \&
quot;\&
quot;;
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
fclose($stream);
}
}
}
The error I Get Is:
A PHP Error was encountered
Severity: Warning
Message: ssh2_exec() [function.ssh2-exec]: Unable to request a channel from remote host
Filename: libraries/Ssh2.php
Line Number: 30
Failure: Unable To Execute Command
This Have Something To Be With the stream_set_blocking function but no matter how I set it I still get the same error. I'm working on Ubuntu PHP 5.2 Any Idea?
Matt Kukowski
on 2009-12-08 20:45:52
Until the buggy 0.11.0 c code is fixed, I recommend using the Pure PHP implementation. I wont bother posting the link as others already have. (hint: it is called phpseclib at sourceforge)
the PECL ssh2 php ext is full of so many problems, I highly recommend not using it… really. It SEEMs to work for one off trivial remote commands, but as soon as you start using it for a lot of server management and a lot of various types of Commands and reacting on the Output, you will find nightmares.
Most try, as I did, using a command terminator like '@end@' or similar. At first this seems to solve the non blocking issues, but again, I saw output where it was returned corrupted when simply altering the LENGTH of the command!
Example:
cat /etc/issue;echo '@end@';
Will send me OK output, but if I adjust the command length as follows
cat /etc/issue; echo '@12321121331_some_length@';
I get garbage along with the contents of the /etc/issue file…
I got fed up, so now am watching the phpseclib (pure php implementation and using it)
Kevin
on 2009-12-06 20:58:03
@ Manoj: You're welcome!
Manoj
on 2009-11-21 11:52:27
Thank you for this wonderful documentation! :)
Kevin
on 2009-11-08 14:45:42
@ Jafar: That's not enough to go on sorry, I also don't run your OS so can't test anything. Maybe you have more luck with the pure PHP implementation found here:
http://phpseclib.sourceforge.net/documentation/net.html
Mike
on 2009-10-28 04:05:08
I found a great SSH2 client at: http://verticalevolution.com/index.php?/archives/4-PHP-Client-SSH.html
It has an easy interface and a good wrapper.
Jafar
on 2009-10-27 22:29:30
Hi, great doc. I have followed the steps. And, I am able to connect but I am not able to send a file. I do not get any error messages either. Any thoughts? I am running, RHent5.4 and Zend Server CE (apache 2.x, php 5.2.6).
Kevin
on 2009-10-09 13:11:03
@ nebj00la: Using shell &
amp; many sleeps helped me when talking to routers.
@ nathan: Before writing a class, checkout:
http://www.seoegghead.com/blog/seo/ssh2-php-howto-guide-ssh-connections-made-easy-in-php-p343.html
(Jaimie Sirovich already did it)
and
http://pear.php.net/pepr/pepr-proposal-show.php?id=586
A PEAR class in the making
nebj00la
on 2009-10-06 19:37:46
Kevin,
Here's the SSH daemon running on the remote device:
SSH-2.0-RomSShell_4.31
Thanks,
John
nebj00la
on 2009-10-06 19:34:42
Kevin,
Great article. The code works great against a linux server, but when I try to run it against a router I get the following repetitive output:
return rc = -1
return rc = -1
return rc = -1
return rc = -1
etc. etc.
Please advise,
Thanks,
John
macrocode
on 2009-10-06 05:31:39
2 words: THANK YOU!
nathan
on 2009-10-04 13:56:10
very nice examples,
haven't tried them yet,
but i'm going to in a sec.
will probably use your tips to create an ssh class.
wonder if it'll work on my wamp as well.
thanks for the help in getting me started.
Nathan.
Llorca
on 2009-09-22 22:55:09
Kevin,
Thanks for the link to the SSH2 Wrapper, that looks like an excellent base for me to start with.
Much appreciated. I've got some minor configuration issues too where PHP is barking about some stuff, but I'm sure I'll get it sorted soon enough.
Alfredo Rivera
on 2009-09-18 16:55:17
I Have tried so many times to install this on a Centos 5 Server, but for some reason it couldnt be installed, I had 2 shell experts working for 4 hours and none of both could successfully install this function, Does anyone hear about this isue?
Kevin
on 2009-09-04 16:09:35
@ Derak: Do a find on php.ini and try to see if Maybe a different php.ini is used?
Derak
on 2009-08-28 01:41:37
I am having trouble with this.
I am running php entropy (php5-5.2.5-6-beta) on Mac OSX Serve 10.5.8. And I successfully installed libssh2-1.1 and did a pecl -f ssh2 successfully.
I go through all the steps without problem, but ssh2 is never activated. ssh2.so gets created and put into \&
quot;/usr/lib/php/extensions/no-debug-non-zts-20060613/\&
quot;
and my php.ini file has:
extension_dir = \&
quot;/usr/lib/php/extensions/no-debug-non-zts-20060613/\&
quot;
extensions=ssh2.so
Here is a php_info() page for everything on my server:
http://studentdev.jour.unr.edu/derak/images/rlb/
ssh2 should be showing up in my \&
quot;Registered PHP Streams\&
quot; section, but it is not.
Any ideas? I feel like I've tried everything here.
Kevin
on 2009-08-22 17:52:11
@ Reinard: Thanks for sharing!
Reinard
on 2009-08-18 23:35:33
If you are having problems sending multiple commands you're likely forgetting to close the stream inbetween sending individual commands.
Try doing an fclose() on the stream resource between the ssh2_exec() calls.
Kevin
on 2009-08-12 12:19:57
@ Eugene van der Merwe &
amp; Brian &
amp; 1oBuZ: If using a different version doesn't work, you could try using a different method: Try switching from blocking &
lt;&
gt; nonblocking. Or try using a shell instead of execute. If that doesn't help start working with sleep and see if the problem is caused by buffers being too lazy, finally if it's a couple of commands you could try to concatenate them with the ';' sign or the '&
amp;&
amp;' signs (in case you want to build a chain: if one fails, all following fail as well). This can actually save you some exectuion time as well.
@ Llorca: Be sure to check out:
http://www.seoegghead.com/software/ssh2-php-wrappers.seo
1oBuZ
on 2009-08-09 18:02:04
I have got the same problem wehn u will find a solution please tell here :)
Brian
on 2009-08-08 11:38:36
Hello,
Im having some issues with outputting the results of the command, sometimes it works, other times it doesnt.
I cannot return the output for something as simple as 'pwd', other times it will return the output of /etc/init.d/mysqld status, other times it wont.
The command seems to complete because i can do something like:
cat /etc/php.ini &
gt;&
gt; test.txt
sure enough the file is there, but if i tried to cat a small file with 1 word in it, no output.
i thought maybe it was the length on fopen, increasing it didnt make a difference,
oddly sometimes I can output the status of httpd or mysql, then other times it just wont do it..
any ideas?
Eugene van der Merwe
on 2009-08-08 07:29:03
#217. Charlie,
I have the same problem as you. What I am trying to achieve is make one connection but send multiple commands. The first command works, but on second try I also get \&
quot;Unable to request a channel from remote host.\&
quot;.
Googling this didn't produce much results, except someone who insinuates this does not happen in libssh 0.13 (see http://www.mail-archive.com/libssh2-devel@lists.sourceforge.net/msg01804.html).
So I suppose my question is, how to I open one connection and run multiple commands without getting any errors? :-)
Eugene van der Merwe
on 2009-08-08 07:10:41
#210. Fatima, if you get error LIBSSH2_ERROR_EAGAIN use the latest version of libssh2 (I used libssh2-1.1.tar.gz). Kevin refers to an old version here.
Llorca
on 2009-07-31 01:00:45
This is fantastic. I'm building this out into a PHP class for use on an automation project. I'll link back to this post for proper recognition :)
Thanks for sharing this, its awesomely useful
Kevin
on 2009-07-28 23:43:19
@ Charlie: No sorry I have never seen that. Do you really need blocking = false?
Also, I think the auth.log on the remote side may show you a clue as to why a channel could not be made. If not, try having ssh log debug messages and try &
amp; review logs again.
Charlie
on 2009-07-27 16:08:58
Should also mention this occurs on the 2nd ssh_exec() call. The first works fine.
Charlie
on 2009-07-27 15:43:16
When executing ssh_exec() with stream_set_blocking false, I get this:
Warning: ssh2_exec(): Unable to request a channel from remote host in /script.php line 111
If set blocking to true, it works… what gives? Does it work fine for you?
Also, I'm using the latest releases… thanks for any response. Great post btw.
Kevin
on 2009-07-15 14:01:29
@ Santosh: Please provide me with more details. This is impossible to debug for me, please understand.
Santosh
on 2009-07-09 08:42:17
php ssh1.php
okay: logged in…
\&
quot;NO OUTPUT\&
quot;
Kevin
on 2009-07-03 14:08:37
@ Stephen: OK Stephen, I'm glad you got it working man. And thanks for sharing.
Stephen
on 2009-07-01 04:59:41
It seems through some trials and a lot of swearing, I managed to figure out a method to get the file to the recipient server, but its not relying on the ssh2_scp_send routine.
In place of the ssh_scp_send line, I replaced with the following:
$sftp=ssh2_sftp($con);
$stream=fopen(\&
quot;ssh2.sftp://{$sftp}{$DestFile}\&
quot;,\&
quot;w\&
quot;);
fwrite($stream,$GeneratedNavb_live);
fclose($stream);
That got the files to their destination.
How I found this was from one resource on two pages:
http://ca2.php.net/manual/en/function.ssh2-sftp.php
http://ca2.php.net/manual/en/function.fwrite.php
Since I had already established a successful link with the recipient server, all I had to do was open an SFTP session, write the file out (Binary safe), and everything worked as expected. No cut offs, although I am hammering the machines just to make sure this works.
Stephen
on 2009-07-01 04:04:36
@Kevin: I KNOW you're not the developer behind the SSH code, but maybe you've experienced this if you use this part of PHP.
What my project entails is taking some information from a database, formatting it very specifically, putting it into a string or file, then shove it over to a series of servers in one particular location. I `sometimes` do not successfully transfer the file in its entirety when using the following code:
// $UploadToServer is an array of server names created by explode
foreach($UploadToServer as $Server){
$DestFile=\&
quot;/tmp/somefile.txt\&
quot;;
echo \&
quot;Transferring to $Server...\&
quot;;
// Force a cache flush
ob_flush();
flush();
$con=ssh2_connect($Server,22);
ssh2_auth_password($con,\&
quot;user\&
quot;,\&
quot;password\&
quot;);
ssh2_scp_send($con,\&
quot;cache/somefile.txt\&
quot;,\&
quot;$DestFile\&
quot;) or die(\&
quot;Could not transfer to $Server - Operation aborted.\&
quot;);
// Force a cache flush again
echo \&
quot;OK.\\n\&
quot;;
ob_flush();
flush();
}
The file that is to be transferred is about 40k in size. However, what gets to the server is about 16k in size. I've found this to be an extremely random event. I'm also seeing that this code is sent successfully, however, the file sizes are a serious mismatch.
The other thing I'm seeing is on a failed transmit, the recipients /var/log/secure log shows this:
Jul 1 01:57:18 hsNavYyzComm2 sshd[29549]: Accepted password for foms from ::ffff:10.12.5.71 port 59937 ssh2
Jul 1 01:57:18 hsNavYyzComm2 sshd[29551]: Disconnecting: Corrupted MAC on input.
Whats interesting is that I get a few KB of data TO the recipient machine before failure. It may be a streaming issue, I don't know.
Now, the thing that I KNOW is working is that if I log into the source machine, I can SCP the file directly to any of those servers and it works every-single-time-without-a-hitch.
Any ideas what I can peek at?
Kevin
on 2009-06-26 10:39:09
@ shaunah: I obviously have no insight in your hosting situation. anyway, looks like you want to try the pure php implementation.
@ Stephen: Glad you to get it to work!
@ Fatima: I think you'd best report this with the authors. You can find their contact data at
http://pecl.php.net/package-info.php?package=ssh2&
amp;version=0.11.0
Fatima
on 2009-06-21 17:35:55
Hi Kevin,
When i execute the command
pecl install -f ssh2,
it produces the following error,
/tmp/pear/cache/ssh2-0.11.0/ssh2.c: In function 'zif_ssh2_fingerprint':
/tmp/pear/cache/ssh2-0.11.0/ssh2.c:558: warning: initialization discards qualifiers from pointer target type
/bin/bash /var/tmp/pear-build-root/ssh2-0.11.0/libtool –mode=compile gcc -I. -I/tmp/pear/cache/ssh2-0.11.0 -DPHP_ATOM_INC -I/var/tmp/pear-build-root/ssh2-0.11.0/include -I/var/tmp/pear-build-root/ssh2-0.11.0/main -I/tmp/pear/cache/ssh2-0.11.0 -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/cache/ssh2-0.11.0/ssh2_fopen_wrappers.c -o ssh2_fopen_wrappers.lo
gcc -I. -I/tmp/pear/cache/ssh2-0.11.0 -DPHP_ATOM_INC -I/var/tmp/pear-build-root/ssh2-0.11.0/include -I/var/tmp/pear-build-root/ssh2-0.11.0/main -I/tmp/pear/cache/ssh2-0.11.0 -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/cache/ssh2-0.11.0/ssh2_fopen_wrappers.c -fPIC -DPIC -o .libs/ssh2_fopen_wrappers.o
/tmp/pear/cache/ssh2-0.11.0/ssh2_fopen_wrappers.c: In function 'php_ssh2_channel_stream_read':
/tmp/pear/cache/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: 'LIBSSH2_ERROR_EAGAIN' undeclared (first use in this function)
/tmp/pear/cache/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: (Each undeclared identifier is reported only once
/tmp/pear/cache/ssh2-0.11.0/ssh2_fopen_wrappers.c:49: error: for each function it appears in.)
make: *** [ssh2_fopen_wrappers.lo] Error 1
ERROR: `make' failed
What should i do with this. Hope you can help me. Thanks
Stephen
on 2009-06-21 10:09:20
I figured out the problem.
It dawned at me (At 4am) that the change to the php.ini as documented here affected just Apache. However, what I'm doing is calling php via a shell, so Apache doesn't come into play. What had to be done was go into the /var/php5/cli/php.ini and add the appropriate extension entry. No restart of services, or anything, and POOF, it worked.
Thanks for this!
Stephen
on 2009-06-21 00:52:53
This is exactly what I'm looking for, as Ubuntu didn't have it installed. Went through the step-by-step instructions and managed to get it to work.
However, ONLY for the first demo.
I changed the credentials, server, and command to execute, and with an additional line of
echo \&
quot;&
lt;pre&
gt;\&
quot;.$data.\&
quot;&
lt;/pre&
gt;\&
quot;;
right at the end of the file. Perfect, I get the results I expected.
However, if I copy/paste the line to check if the ssh2_connect function exists into my own code, first line, nothing else, it bombs saying that the function doesn't exist.
The unique thing about this particular PHP file I'm fighting with is that its actually \& quot;CALLED\& quot; via exec. The actual command line is
php comparefile.php file1 file2It fails with \& quot;function ssh2_connect doesn't exist\& quot;.
I drop to the command prompt, run code posted above, and it fails as well.
So why would it be that I can execute the PHP code directly from a web browser, it works perfectly as expected, but, when I run it at a command prompt, or shell out, I'm failing?
shaunah
on 2009-06-20 03:51:05
i don't install pear like that - i usually take the individual components i need and their dependencies and put them in a directory in the include path.
i've done go-pear, before, as well, but all trying to do it via the command line results in are errors. i'm on a shared host and it seems like it would be a rather big oversight on their part if they let me install arbitrary extensions on their server.
Kevin
on 2009-06-18 15:07:40
@ shaunah: Well I mentioned PEAR cause if pear works chances are you can install pecl using pear just like:
pear install -f ssh2
instead of:
pecl install -f ssh2
@ purelife: Yeah that sure looks like a good option too!
@ Alan Choyna: I think our issue is a bit beyond the scope of this article and if you're still choosing libssh instead of purelife's suggestion it might be best to create a bug report with the authors of the software to help you troubleshoot.
@ Han: Do other commands work with libssh? You might test something like the following:
$str = file_get_contents('/srv/www/htdocs/point/server.tmp')
remotely execute:
\& quot;echo '{$str}' & gt; /home/point/etmain/server.cfg\& quot;
Which could also get your config file across. At least you isolate any permission related issues that way.
purelife
on 2009-06-17 08:06:08
@shaunah and @Alan Choyna:
Try this:
http://phpseclib.sourceforge.net/
Since it's pure-PHP, you don't have to bother with any PECL nonsense.
Alan Choyna
on 2009-06-14 23:45:27
I have a Centos 5.3 64 bit OS that I'm building as a Scalr management station manage our Amazon Ec2 cloud instances, and am caught trying get the \&
quot;pecl install -f ssh2\&
quot; to work.
Scaltr requires pretty much the latest PHP (at least 5.2.9, I've installed 5.3.0).
I've updated the libssh2 as you've documented, but the \&
quot;pecl install -f ssh2\&
quot; command fails with a
\&
quot;: Function eregi() is deprecated in PEAR/Registry.php on line 735. Error make failed\&
quot;.
I'm not sure what to do to debug this issue. Any advice?
Thanks in advance,
Alan
Han
on 2009-06-14 15:05:46
Kevin, i hope you remember me.
I have a new problem… ive been trying to make v2.0 of a project i made some months ago.
And i notice that for some reason the following does not do anything:
if(ssh2_auth_password($connection,$user,$pwd)){
$stream=ssh2_scp_send($connection,\'/srv/www/htdocs/point/server.tmp\',\'/home/point/etmain/server.cfg\',0644);
}else{
echo \&
quot;No ballzzz...\&
quot;;
}
if i put an echo before and after the $stream line, it gets echoed, but for some reason the scp send command doesnt do bullshit …
Any idea ?
shaunah
on 2009-06-12 17:55:08
pear isn't a problem - it's pecl that is.
some pear stuff may be a problem, too, if they use pecl extensions, but i haven't come across such packages, yet.
Kevin
on 2009-06-11 20:50:12
@ shaunah: That kind of depends how they blocked it. Can you run pear?
shaunah
on 2009-06-10 23:16:56
my host doesn't support let me install pecl modules. can i still do ssh2?
Kevin
on 2009-06-10 15:00:30
@ Daisy: Hm, could it be that you don't have enough rights to create the files? Try running the commands prefixed with sudo (or as root) for once.
@ Rachid: The PHP implementation doesn't generate dialogs that block further processing. It's a warning, not an error.
Rachid
on 2009-06-08 15:22:17
I am trying to connect to a system which requires some extra code.
When connecting with a regular SSH client, I get the following message once.
\&
quot;The authenticity of host '192.168.255.9 (192.168.255.9)' can't be established.
RSA key fingerprint is 64:bd:4e:94:a5:da:50:30:a8:67:71:7e:f0:19:1a:b7.
Are you sure you want to continue connecting (yes/no)?\&
quot;
I can bypass this by pressing Yes on the SSH client.
But how can I bypass this with PHP ?
Daisy
on 2009-06-08 11:53:47
The libssh2-1.1is the latest stable version and I have tried the method mentioned in this post and it works successfully. However, when I try to install this in another Debian Server, it gives me the following error. I am very new to Debian, hence, any help is welcome :
I get this when I put the ./configure :
checking host system type… (cached) x86_64-unknown-linux-gnu
checking for style of include used by make… GNU
checking for gcc… gcc
checking for C compiler default output file name…
configure: error: in `/usr/src/libssh2-1.1':
configure: error: C compiler cannot create executables
Kevin
on 2009-05-26 14:24:32
@ estus: Sorry estus, I don't know what else to do, I find it kind of hard debugging this remotely. Could it be the PHP version that ships with redhat? I'm on the PEAR mailing list &
amp; I heard a LOT of complaints &
amp; bugs with that version..
estus
on 2009-05-22 15:29:19
When I use shell method site/script is loading, loading… and loading and nothing happens. I don't have any ideas whats wrong… ;/
estus
on 2009-05-22 14:08:03
I know it's strange. I'm using PHP Version 5.1.6, with default php.ini. I was nothing changing. Maybe check my phpinfo: http://195.85.230.135/~gameone/phpinfo.php
stream_set_blocking($stream, 0);
no change
var_dump($buf);
no change and nothing return, really :/ this while loop is not perform.
Kevin
on 2009-05-22 13:30:49
@ estus: Ok that's strange. What kind of device are you connecting to? Sometimes the shell method works better.
What version of PHP are you using?
Maybe setting blocking to 0 helps:
stream_set_blocking($stream, 0);
Try to echo debug information inside the while loop like:
var_dump($buf);
Cause it must return something, as echo \& quot;1\& quot; is never reached, hence $buf contains something that evaluates to true I suppose.
estus
on 2009-05-22 12:57:04
I'm using source code from your article.
error_reporting(E_ALL);
// ini_set(\'display_errors\', 1);
if (!function_exists(\&
quot;ssh2_connect\&
quot;)) die(\&
quot;Error #0: Nie wykryto ssh2 na serwerze.\&
quot;);
if(!($con = ssh2_connect(\&
quot;xxx\&
quot;, 27))){
echo \&
quot;Error #1: Nie moge sie polaczyc z serwerem.\\n\&
quot;;
} else {
if(!ssh2_auth_password($con, \&
quot;xxx\&
quot;, \&
quot;xxx\&
quot;)) {
echo \&
quot;Error #2: Bledny login lub haslo.\\n\&
quot;;
} else {
echo \&
quot;Wszystko dziala.\\n\&
quot;;
if(!($stream=ssh2_exec($con, \&
quot;whoami\&
quot; ))){
echo \&
quot;Error #3: Komenda nie moze zostac wykonana.\\n\&
quot;;
} else {
// zwraca wynik
stream_set_blocking($stream, true);
$data = \&
quot;\&
quot;;
echo\&
quot;0\&
quot;; // helps
while($buf=fread($stream,4096)){
$data .= $buf;
echo $data;
echo\&
quot;1\&
quot;; // helps
}
fclose($stream);
}
}
}
Commands works good, I know cause I can create directory. Error reporting doesn't shows any errors. :d It shows only number \& quot;0\& quot; but no \& quot;1\& quot;. I was try with fgets or stream_gets_content but it doesn't works too.
Kevin
on 2009-05-22 10:54:40
@ estus: Yes maybe you're doing something wrong. But for me there's no way to tell if you don't show me how you're doing it.
So please provide the source code and set error_reporting to E_ALL and show all output and what goes wrong.
estus
on 2009-05-20 19:27:25
Hi,
thanks for this tutoturial, but I have some problem. Commands work fine but it doesn't return data.
Am I doing something wrong?
Kevin
on 2009-05-14 22:23:23
@ Albert: I haven't seen that before so this is not enough for me to go on. Does it work to other servers?
@ Ed: Sorry Ed, this doesn't ring any bells. Have you tried both the version 0.14 and the latest stable one of libssh?
Ed
on 2009-05-07 02:09:00
I have an installtion questions.
I'm running on CentOS 5 Final, PHP5.
all packages installed, libssh2 installed, but when i go to install ssh2 - pecl install ssh2-beta, i get the following(only the last few lines):
***************************
/usr/include/ctype.h:105: error: declaration for parameter 'isdigit' but no such parameter
/usr/include/ctype.h:104: error: declaration for parameter 'iscntrl' but no such parameter
/usr/include/ctype.h:103: error: declaration for parameter 'isalpha' but no such parameter
/usr/include/ctype.h:102: error: declaration for parameter 'isalnum' but no such parameter
/usr/include/ctype.h:86: error: declaration for parameter '__ctype_toupper_loc' but no such parameter
/usr/include/ctype.h:84: error: declaration for parameter '__ctype_tolower_loc' but no such parameter
/usr/include/ctype.h:82: error: declaration for parameter '__ctype_b_loc' but no such parameter
/usr/include/php/main/php.h:127: error: declaration for parameter 'php_strlcpy' but no such parameter
make: *** [ssh2.lo] Error 1
ERROR: `make' failed
*******************
I've spent a few days on this and i'm at my wits end. any suggestions would be great.
thanks,
Ed
Albert
on 2009-05-06 15:46:49
Warning: ssh2_connect() [function.ssh2-connect]: Error starting up SSH connection(-3): Error sending banner to remote host in /var/www/test/classes/ssh.php on line 27
Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 10.0.0.22 in /var/www/test/classes/ssh.php on line 27
I know that the server is running I use your code.
Janckos
on 2009-04-20 20:27:28
Great!
Kevin
on 2009-04-03 10:58:37
@ Mike: very well!
Mike
on 2009-04-01 17:58:21
scratch that last comment, I figured out by enabling clear text passwords in the sshd_config and restarting ssh, like it said in the instructions! Thanks for the article its been very helpful
Mike
on 2009-04-01 17:24:50
To clarify my web page pulls up:
fail: unable to authenticate
echos straight from the code.
Mike
on 2009-04-01 17:20:52
I am able to get the ssh function working and connect, but having authentication issues. I can use ssh through cygwin and connect fine with the same username and password. When I enter the servername, username and password with the code above it doesnt authenticate. Are there any concerns I should be aware of in regards to php authenticating vs cygwin or bash? Im running this on a SUSE 11 box.
stef
on 2009-03-16 00:38:50
Yeah, great article ! ! Just want to let people now, in the examples , output is returned in $data variables. I was stumped on this one for a few minutes, staring at a blank page before I found the jackpot.
Kevin
on 2009-03-02 14:36:07
@ redux: Sounds like you need shell, as is explained in the article.
@ Kristjan Adojaan: Thanks for sharing!
Kristjan Adojaan
on 2009-02-27 07:46:05
In manual there is refferred to libssh2-0.14. I was not able to use it (pecl install did not compile). With version libssh2-0.18 or libssh2-1.0 it was OK. Get latest version from http://sourceforge.net/project/showfiles.php?group_id=125852
redux
on 2009-02-26 02:43:17
Can you use ssh2_exec and write to STDIN? I have a program that reads input for STDIN, but I am unable to write to the stream. I've tried a bunch of stuff, even adding a CTL-D, but it just hangs as if it is waiting for input.
Thanks,
Kevin
on 2009-02-21 12:44:34
@ purelife: That looks amazing!
purelife
on 2009-02-17 21:50:04
if you can't get php_ssh2.dll working, you might have better luck with a pure-PHP implementation of ssh:
http://phpseclib.sourceforge.net/
Kevin
on 2009-02-11 02:17:28
@ Daisy: What you did all looks okay. Exactly which php.ini did you change it in? And are you trying to run this from commandline or apache?
Daisy
on 2009-02-04 19:14:40
Hello kevin.
Well I've very basic knowledge in this area, so please, you can be very specific in your answer :)
I was having some problem with the command:
pecl install -f ssh2
wich I could fix thanks to the comment #165
Everything worked fine with the installation now,
but it is still not recognized when I run the php script in my webpage:
function ssh2_connect doesn't exist
I've seen a lot in the comments that you say it's probably that the extension is not in the php.ini file.
I don't know if I put it in the right place, so I'm posting where I did put it:
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
;
; If you wish to have an extension loaded automatically, use the following
; syntax:
;
; extension=modulename.extension
;
; For example, on Windows:
;
; extension=msql.dll
;
; … or under UNIX:
;
; extension=msql.so
;
; Note that it should be the name of the module only; no directory information
; needs to go here. Specify the location of the extension with the
; extension_dir directive above.
extension=ssh2.so
Right there below the comments. I guess that is right, how can I test what else can be happening?
Thanx in advance,
Daisy.
Kevin
on 2009-02-01 21:52:00
@ John: Thank you for sharing!
John
on 2009-01-26 22:30:51
Thanks for good tutorial.
Not sure if this is covered in previous comments:
On Ubuntu Gutsy I had to use the ssh2 0.10.0 version as the 0.11.0 would not build - there is already a bug written…
Once built, I had to manually add an ssh2.ini file in /etc/php5/apache2/conf.d-
\&
quot;# configuration for php SSH2 module
extension=ssh2.so\&
quot;
PHP wouldn't use the module until I did, though phpinfo and the error log made it look as though it was loading.
hope this helps somebody
John
Shizuka
on 2009-01-23 22:59:28
Thank you so much for this article, and especially for the additional help at comment #165! I've been banging my head against a wall for about five hours trying to get this working for a WordPress installation.
Kevin
on 2009-01-15 11:25:47
@ ian: Thanks for the heads up! I've updated the article
ian
on 2009-01-14 22:42:10
ssh2 is now upto v1.
everything works nicely, remember to apt-get make for ubuntu.
Kevin
on 2008-12-30 10:25:05
@ Jamie: Thanks for sharing
@ Jaimie Sirovich: You may even concider contributing such a class to PEAR. That will guarantee solid testing, ideas for improvement &
amp; feature requests &
amp; bug reports.
Jaimie Sirovich
on 2008-12-19 08:57:59
The wrappers below may help you a little more … for most common tasks. If anyone has any input on them (or bugfixes!), let me know.
The article above was helpful when I was trying to figure out heads from tails with ssh_*; thanks Kevin!
http://www.seoegghead.com/software/ssh2-php-wrappers.seo
Virg
on 2008-12-19 00:19:00
Just want to say thanks for this article. It was super helpful. =)
Jamie
on 2008-12-18 16:13:26
I have been having problems installing this on Ubuntu 8.10 also. I finally got it working. It appears the latest version of the PECL SSH2 package is the problem, so the solution is to manually install it.
Go to:
http://pecl.php.net/package/ssh2
And download 0.10.0. Then:
tar -zxf ssh2-0.10.tgz
cd ssh2-0.10
phpize
./configure
make
After that add the extension=ssh2.so in your php.ini file and you are up and working.
The problem with the current version (0.11.0) seems to be this:
ssh2_fopen_wrappers.c:49: error: `LIBSSH2_ERROR_EAGAIN' undeclared (first use in this function)
Hope this helps out!
Kevin
on 2008-12-17 13:15:17
@ Nicolas: Need. More. Output :) Check below.
Nicolas
on 2008-12-12 21:20:15
hi, i run :
pecl install -f ssh2-beta
and get this error
make: *** [ssh2_fopen_wrappers.lo] Error 1
ERROR: `make' failed
helpmeeeee!!
tanks
Kevin
on 2008-12-10 17:23:40
@ Allie: Great that you share that with us. Thanks
Allie
on 2008-12-05 16:06:24
I had problems using ssh2 on Mac OS X. Everything compiled fine for both libssh 0.18 and ssh2.0.10. However, when I try to run my php script on the commandline, I keep getting the error \&
quot;PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php5/lib/modules/ssh2.so' - (null) in Unknown on line 0\&
quot;. I tried everything but nothing works until I reconfigure ssh2 again using the following :-
./configure –with-ssh2=/usr/local/lib –with-php-config=/usr/local/php5/bin/php-config &
amp;&
amp; make install
Hope this will hope someone
Kevin
on 2008-12-01 09:18:04
@ Karol: I have enough hobbies as it is ;) but feel free man!
Karol
on 2008-11-28 14:46:57
It would awesome to see this integrated with something like phpterm:
http://phpterm.sourceforge.net/
Søren Jensen
on 2008-11-23 16:36:07
My bad… I see #133 already found that solution.
Reading comments do pay off, heh :-)
Søren Jensen
on 2008-11-23 16:33:40
Thanks for the guide.
It somewhat worked for me - I mean it really pointed me in the right direction and got me started.
I think I had the same error as Caesonia, tho not much information was provided.
I had to modify the ssh2.c file according to the commenst on this link: http://dk2.php.net/manual/en/ssh2.installation.php
Then it seemed to work out fine. It shows up in a phpinfo() now.
Alf Marius
on 2008-11-17 19:31:22
Great guide, thanks! :)
Kevin
on 2008-11-13 23:16:48
@ Trenton &
amp; Caesonia: I can only help you if you paste the full output/log of what's hapening. Maybe pastebin.org is a good place for that, and then paste the link to it here?
@ Caesonia: I can see how that's frustrating, but there have been 153 comments before you, and a lot of people have got it to work.
If my article has become outdated, I will update it, but as for now, the information to get it working on ubuntu; should be here. If not let me know.
Also, you might want to try to CTRL+F through the comments to see if your problem was already solved.
Caesonia
on 2008-11-13 19:49:51
As usual I appreciate the guide and time, but as usual, it doesn't work. I am using Ubuntu, and apparently openssl-dev is not available, though several with it in the package name are. I've tried several different locations. Additionally PECL fails to install. Wow, big surprise there.
So one more big waste of time in the Linux world. It's this sort of thing that really keeps prince of darkness MS in the game. We can;t get our acts together to have stuff that is consistently available and workable. And SSH2 is not exactly some strange out there sort of geek thing so it should be available easily across the makes.
I need to write scripts to monitor my server load on several different systems, and I really get tweaked at wasting my time instead going down dead paths to finding I can;t write those scripts to begin with, because its not possible to compile whatever the platform you need.
What a fricken joke.
Trenton
on 2008-11-11 17:32:47
I am trying to follow this but the pecl build is erroring out.. @ ssh2.lo. Can you possibly provide some assistance?
\&
quot;make: *** [ssh2.lo] Error 1\&
quot; feel free to email me.
Kevin
on 2008-11-03 10:21:35
@ Bob: It also depends what device you are talking to. I found that a couple of network routers have to be treated differently from debian servers. This had to do with the ssh implementation on the other side. You have to play around with the settings to make it solid for your environment. Thanks for sharing though.
@ Sander: that way you go around the problem, but if it works your you and you can live with the security implications, that's good!
@ Sean: Thanks for sharing!
Bob Likes Your Tutorial
on 2008-11-02 12:17:07
Great tutorial but it didn't work for me. In my case, the script just returned \&
quot;logged in\&
quot;.. but never seemed to move on to the next stage. I had to change the part about reading the buffer to:
stream_set_blocking( $stream, true );
$output = stream_get_contents($stream);
echo $output;
fclose($stream);
I'm not sure if I'll discover hidden consequences though. I'm too new at this to know.
Thanks again for this though. I would never have know about this function without this tutorial.
Sander
on 2008-10-27 18:04:17
We figured it out:
The command for the cronjob:
wget -q http://www.xxxxxx.xxx/getfiles.php
Sean
on 2008-10-23 22:11:27
If anyone encounters an memory allocation error when intalling ssh2 via PECL.
Add the following at the top of /usr/share/pear/pearcmd.php:
ini_set('memory_limit','32M');
Kevin
on 2008-10-22 16:40:15
@ Sander: There are two php.ini's. One for CLI &
amp; one for Apache. Maybe you need to enable your so in your CLI's php.ini?
Sander
on 2008-10-22 15:47:06
Hi Kevin,
When I run the script with my browser there is no problem, but in the cronjob the function ssh2_connect() is unknown.
Other \&
quot;normal\&
quot; functions are working fine like ftp_connect(), ftp_login(), microtime()…. The systemmanager is now checking whats wrong because he installed ssh2 functions on my server (i can't do that). I'm searching also for some solutions :)
Thanks
Kevin
on 2008-10-21 10:04:46
@ Sander: And running it from the commandline otherwise goes fine? A common reason for things not to work from crontab, is that some environment variables like PATH are not set. Could this be your problem as well? Are you making any additional system calls?
Sander
on 2008-10-20 20:34:34
Hi, I installed ssh2.so this works fine but when i create a cronjob to receive some files from an sFTP server the function does not excist :( is there an problem to use the function ssh2_connect() in combination with a cronjob?
Thanks
Han
on 2008-10-14 13:07:35
No prob ! :)
Glad that its actually working ;-)
Kevin
on 2008-10-14 13:00:17
@ Han: Awesome man. Good job &
amp; thanks for sharing.
Han
on 2008-10-14 12:54:03
UPDATE: i has fixed the problem, for other 64bitters out there :
in the configure file, locate libcrypto
and change the following :
if test -r $i/lib/libcrypto.a -o -r $i/lib/libcrypto.$SHLIB_SUFFIX_NAME; then
OPENSSL_LIBLINE=\&
quot;-L$i/lib -lcrypto\&
quot;
change that into :
if test -r /usr/lib64/libcrypto.a -o -r /usr/lib64/libcrypto.$SHLIB_SUFFIX_NAME; then
OPENSSL_LIBLINE=\&
quot;-L/usr/lib64 -lcrypto\&
quot;
Kevin
on 2008-10-14 12:52:04
@ Han: Could work I suppose, if the so is binary compatible that is (other machine also 64bits? etc).
Han
on 2008-10-14 12:46:59
PS: if i copy the so files from my other box ( suse 10.2 ) to the same directories, would this work or does the install of libssh do other stuff as well ?
Regards,
Han
on 2008-10-14 12:40:33
Well, i cant remember which version i used.
Looking at my earlier posts, i used version 16 , but version 16 and 14 are both giving me the same problem, and i have no idea anymore how i fixed it :(
Kevin
on 2008-10-14 10:16:05
@ Han: If I remember correctly you fixed some issues on version libssh2-0.14, and that worked better?
Han
on 2008-10-14 01:02:14
Dear Kevin,
You have been a great help in the past, but now im having difficulties all over again.
I recently moved server host and instead of Suse 10.2 , i now have Suse 11 ( x64 as well )
And the error im getting on all versions is :
checking for OpenSSL… configure: error: Cannot find OpenSSL's libcrypto
Artemis:~/libssh2-0.16 # locate libcrypto
/usr/lib64/libcrypto.so.0.9.8
Altough as u can see, its there …
Any suggestions ? :s
Regards,
Kevin
on 2008-10-10 15:00:38
@ slak: Thanks for sharing.
Kevin
on 2008-10-10 14:59:51
@ Marek: I have not. But for your specific case, you might want to consider using:
echo \&
quot;user:pass\&
quot; | chpasswd
Marek
on 2008-10-10 09:32:27
Hello,
First, thanks for tutorial - it's very good :).
Second, does anyone tried to run interactive commands using this library, eg. 'passwd'… My question is: is it possible to enter some value when console prompt for it?
slak
on 2008-10-09 19:25:08
Thanks for the tutorial. When I pecl install -f ssh2 under Debian Etch I get:
…
/root/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
I had to get the patch from: http://www.billpitz.com/howto/php-libssh2.html
cd /tmp/pear/cache/ssh2-0.10
patch &
lt; php-libssh2.diff
phpize &
amp;&
amp; ./configure –with-ssh2 &
amp;&
amp; make
Hope that helps someone.
Kevin
on 2008-10-06 17:40:59
@ Eugene van der Merwe: That's not really specific enough to help. But what I can say is this:
Don't do it from webpages. This can only lead to problems. If you really need a webinterface for this module. Create an interface that inserts jobs into a table.
This table can be read from a PHP program (using PEAR's System_Daemon), and can then from the commandline execute the ssh commands.
This would be the clean way.
Eugene van der Merwe
on 2008-10-06 17:07:49
Thank you for this routine.
Our our system it hangs when we do it on a web page. If I run it from the command line it works 100% every time.
The funny thing is it only sometimes hangs when you do it in a web page. I have not been able to spot the pattern.
Please assist.
Kevin
on 2008-09-29 12:34:03
@ faizal: My pleasure!
faizal
on 2008-09-26 05:19:37
Great article…I've searched this topic for a long time. But thx God I found it here…Thx to Mr. Kevin
Kevin
on 2008-09-21 21:49:48
@ chris: I had the same problem trying to connect to network routers running their own CLI instead of POSIX compliant SSH.
Working with ssh2_shell instead of exec was the solution though.. I figured, if I can get ssh to interface with the device, it should be possible for libssh to do so as well. And eventually it was.
The only problem left were timing issues. As mentioned in the Tips chapter.
chris
on 2008-09-18 20:13:08
I've successfully connected and authenticated to a remote server via SSH. However, I don't know who how to get a stream going because the remote server is not another UNIX box, its a system running a custom CLI.
Using: ssh2_shell() won't work, nor does ssh2_exec(). The latter will give me the following warning:
Warning: ssh2_exec() [function.ssh2-exec]: Unable to request command execution on remote host in …
Can anyone help?
Kevin
on 2008-09-13 15:51:33
@ Neil: Thanks for sharing!
Neil
on 2008-09-11 18:53:24
I was not able to get 'pecl' to automatically build and install the ssh2-beta on my Linux distro. 'pecl' would always exit after running 'configure' on the ssh2-beta package, even though (as far as I can tell) 'configure' exited cleanly for the package.
So, I just manually built ssh2.so after 'pecl' had downloaded the package, using:
cd /tmp/pear/cache/ssh2-0.10
(added fixes to ssh2.c from http://pecl.php.net/bugs/bug.php?id=12348 just in case – doesn't hurt)
./configure
make all
make install
The ssh2.so library is placed into /usr/lib/extensions/no-debug-non-zts-20020429 by default after the 'make install'. I copied ssh2.so from there to /opt/lampp/lib/php/extensions/no-debug-non-zts-20020429 since I use the XAMPP PHP install. From there, I could pick up again with your Installation instructions above to add the extension to my /opt/lampp/etc/php.ini file. I was able to see the library being loaded in phpinfo() after that, so now I'm off to try out the new library in my code….
Hope that helps someone else. :)
Daniele Siddi
on 2008-09-10 10:34:34
OK, Thank you for your guide, it helped me very much.
Kevin
on 2008-09-09 17:30:44
@ Shane: OK thanks a lot!
Shane
on 2008-09-09 16:45:08
It's at the bottom of the post.
Kevin
on 2008-09-08 10:11:22
@ Shane: Yeah I noticed you've copied parts of my tutorial to your blog. I would appreciate some kind of backlink for that.
Shane
on 2008-09-08 01:13:39
@ Kevin: Yeah. I had to use 0.14 also. I found out that the later versions don't work because the ssh2.c file for pecl. They haven't work on a fix fo 0.18 yet.
That's the deal. I posted something about what I was using it for on my blog.
Kevin
on 2008-09-05 21:40:12
@ Shane: What version are you using? I still use v14 and have no issues with it. People have reported similar issues with later versions of libssh.
Shane
on 2008-09-04 22:25:14
Kevin… I am also getting the \&
quot;PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) 'libssh2.so' in Unknown on line 0\&
quot; error when I run
[root@apollo lib]# php -r \&
quot;echo (int)function_exists(\'ssh2_exec\');\&
quot;
Any ideas?
Kevin
on 2008-08-28 14:45:57
@ bvidinli: Thank you for noticing! I've updated the article.
bvidinli
on 2008-08-28 14:24:30
to reader:
build-essential
not buid-essential
fix that in your codes..
Kevin
on 2008-08-27 18:21:54
@ Han: I would only use this method from PHP-CLI utilities, Han. But can you paste a more detailed log over at pastebin.org or something? That would greatly help solving the problem.
Han
on 2008-08-15 17:41:23
I need urgent help with PHP &
amp; SSH2 …
All of a sudden, in my error_log file in /var/log/apache2/error_log i get spammed with the following messages :
sh: load: command not found
What is it ,and how do i fix it. I get spammed whenever i load a php file that has ssh2 stuff in it :/
Kevin
on 2008-07-26 14:25:26
@ David Merel: Seems like connectivity issues to me. From that server can you telnet 192.168.1.116 on port 22 and get a connection?
David Merel
on 2008-07-25 18:46:51
I am getting the following, SSH2 is howing up in PHPinfo and I was able to get libssh2 .18 version going with 2.0.10, however I keep getting these errors below. I have also tried 2.0.11 as well as 2.0.10 with Libssh2 .14 version which they say is more compatible but still same results. It doesnt happen on my other servers, the only difference is that this is on FC4 (Fedora Core 4)
Any ideas?
Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 192.168.1.116 on port 22 in /var/www/html/send.php on line 3
Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 192.168.1.116 in /var/www/html/send.php on line 3
Warning: ssh2_auth_password() expects parameter 1 to be resource, boolean given in /var/www/html/send.php on line 4
Warning: ssh2_scp_send() expects parameter 1 to be resource, boolean given in /var/www/html/send.php on line 6
finished
Kevin
on 2008-07-25 08:49:09
@ ken@ghx: So far I've only tested it by supplying username &
amp; passwords. Think it may be faster to try it yourself than to wait for someone else to reply here.
Could you share your findings though?
ken@ghx
on 2008-07-23 05:22:04
has anyone tested this with ssh keys? Would it be possible to authenticate via LDAP, then ssh to a server and execute a command via an identity file? Or is there a better way to do this? I am trying to write an central application control app.
Han
on 2008-07-22 21:58:07
Sorry for the dutch there, i has fixed it and now i hs a .so file, thanks a lot for pointing me in the right direction !
Kevin
on 2008-07-22 21:42:53
@ Han: English please. I think I'm not the right person to support that package or it's bugs/patches for that matter. Maybe you need to ask on your distro's forum.
Han
on 2008-07-22 20:28:17
Kevin, ik snap niet heel goed wat ik precies moet doen …
Kan jij me ff op weg helpen ?
Thx
Kevin
on 2008-07-21 23:18:20
@ Han: Warnings can usually be ignored. Errors not. The error I saw in your output was: \&
quot;too many arguments to function libssh2_session_methods\&
quot;
I ran it through google and this was the first hit:
http://pecl.php.net/bugs/bug.php?id=11779
Looks like your problem so it might help you
Han
on 2008-07-21 20:41:44
Posted the whole stuff again on pastebin:
http://pastebin.org/55011
Kevin
on 2008-07-21 16:40:53
@ Han: Can't really tell based on the output given. Maybe post the next failures on pastebin.org
Han
on 2008-07-21 16:30:38
Kevin, if ur fine with it, ill post the output when compiling v 14 …
but still, i do not think that my pear / pecl problem is related to the version of libssh i have installed …
Kevin
on 2008-07-21 15:27:40
@ Han: I still use v14 and have no issues with it. People have reported similar issues with later versions. It works on debian &
amp; ubuntu 64bits systems. I'm unable to test on suse 64bits.
Han
on 2008-07-21 14:52:43
kevin, i have all packages needed.
And installed libssh2v16, because i was not able to install v18 (latest ) and v14 because of other issues ( regarding ld -lz or something )
Pls note that im on Suse x64 10.2
Help is much appreciated :(
Kevin
on 2008-07-21 14:32:38
@ Han: OK, the output is cut at (I think) 80 characters or so, so I can't fully read it.
But could it be you use the latest libssh version and not the 0.14? Cause some people have reported similar problems with that.
Also, check under 'Packages' to see what packages are required (also dev packages &
amp; build tools).
Han
on 2008-07-21 14:00:33
Kevin, i have fixed the pecl problem, i was missing some packages, sorry for my latest comment !
Altough now, i have a new problem, please find the output here :
http://rafb.net/p/YiZQBj72.html
Han
on 2008-07-21 13:56:40
kevin, i appreciate ur comment and help, but eh, pecl command not found means pecl does not exist on my system to be honest … Does -f also work with pear ?
Like pear -f install ssh2 ?
Kevin
on 2008-07-21 13:37:08
@ Han: Please follow the tutorial closely. It says '-f' and explains why that is needed.
Han
on 2008-07-21 13:17:11
After installing libssh2 and trying to install ssh2 i get :
Aphrodite:~ # pear install ssh2
No releases available for package \&
quot;pear.php.net/ssh2\&
quot; - package pecl/ssh2 can be installed with \&
quot;pecl install ssh2\&
quot;
Cannot initialize 'channel://pear.php.net/ssh2', invalid or missing package file
Package \&
quot;channel://pear.php.net/ssh2\&
quot; is not valid
install failed
However, when trying pecl install ssh2, i get, command pecl not found … ?
Kevin
on 2008-07-17 22:10:40
@ Peter: I believe I've read somewhere that ssh2_scp_send isn't that reliable. What kind of file are you trying to submit? Because there may be ways to implement a more reliable system using ordinary commands.
@ Ho Wai Keong: Thank you for sharing!
@ Ross: Please request support from a CentOS related forum/community.
@ Tim: Thank you Tim
Tim
on 2008-07-16 20:18:24
Hey, just wanted to tell you, this article has been really helpful. We just moved an account management webapp from one server to another, and couldn't figure why it didn't work, until we found that it required ssh access to get to our ldap on another server, and apparently we forgot to link this module when we moved over. Thanks for this information!
Ross
on 2008-06-26 12:41:20
Hi,
I'm trying to install this on a machine running CentOS. However I am hitting this error when trying to run the \&
quot;make all install\&
quot; command for libssh2
make[1]: Entering directory `/usr/src/libssh2-0.14/src\'
gcc -o channel.o channel.c -c -g -O2 /usr/include -I/usr/include -Wall -I../include/ -fPIC
gcc: /usr/include: linker input file unused because linking not done
channel.c:1253:10: /usr/include: No such file or directory
channel.c:71: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See &
lt;URL:http://bugzilla.redhat.com/bugzilla&
gt; for instructions.
make[1]: *** [channel.o] Error 1
make[1]: Leaving directory `/usr/src/libssh2-0.14/src\'
make: *** [all] Error 1
Ho Wai Keong
on 2008-06-26 08:13:14
This article has helped me solved installation of ssh2 to my php on my server. I guess those who have difficulties to compile or make the ssh2.so, u should download the ssh2 tarball from this link http://svn.mandriva.com/cgi-bin/viewvc.cgi/packages/cooker/php-ssh2/pristine/SOURCES/
this definitely fixes it!
Peter
on 2008-06-25 19:30:44
When i use ssh2_scp_send() to copy files it show me a warning saying that the file wasn't copied and the return value of the function is false but the file is truly copied. I tested if the file was really well
copied by verifying his md5 hash code and the copy was successful, the file was well copied. Can you help me?
Shoaibi
on 2008-06-10 05:03:33
Great article indeed…
Kevin
on 2008-06-06 08:00:21
@ UnrealComps: My guess would be that the module has not been loaded. Did you include the
extension=ssh2.so
UnrealComps
on 2008-06-06 00:38:48
I have followed all the steps above but it still says \&
quot;function ssh2_connect doesn't exist\&
quot;. Any ideas? Thanks
MianoSM
on 2008-06-04 00:04:53
Thank you, I went back and double checked my extensions and had mistakenly commented out my own additions. Thank you for your rapid response. : )
Kevin
on 2008-06-03 23:05:07
@ MianoSM: My guess would be that the module has not been loaded. Did you include the
extension=ssh2.so
MianoSM
on 2008-06-03 22:45:47
I'm following the prerequisites, as well as the steps with copy and paste (with a few sudo's beforehand on some of them), on a 6.06 Ubuntu system.
I get install ok: channel://pear.php.net/ssh2-0.10
I have also restarted sshd after ensuring:
passwordauthentication yes
Now, when I use the block for Method 1, I'm seeing:
\&
quot;function ssh2_connect doesn't exist\&
quot;
Any pointers on this would be greatly appreciated. Thank you.
Kevin
on 2008-05-31 12:37:45
@ nunu: Seem's to me you can't connect. Can you connect to that same IP using regular console SSH?
nunu
on 2008-05-27 09:06:59
hei, i got this error :
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
can you tell me what's wrong?
i'm using :
libssh2 version 0.12
banner SSH-2.0-libssh2_0.12
Thankyou
NJ
on 2008-05-09 16:38:04
Thanks a ton!! I was trying to resolve this problem from a long time. It really helped.
cartmanf
on 2008-05-04 00:29:57
Thank you kindly for the extensive tutorial. I found it extremely useful.
Hope you post some more advanced tutorials soon.
Regards,
Reinhard
Wes
on 2008-04-24 00:34:46
I found what was the problem. a space in the var that I was using. I used trim and that took care of it.
Thanks a lot for all your help.
Excellent tutorial indeed
Kevin
on 2008-04-23 11:11:46
@ Wes: Can't reproduce easily, but 2 tips:
- concatenate the var
- when sudoing it is probably better use a shell instead of exec
Wes
on 2008-04-22 22:00:02
Correction
if(!($stream = ssh2_exec($con, \&
quot;ls /home/user/$v1\&
quot; )) )
Wes
on 2008-04-22 21:59:01
Thanks Kevin, I found the easiest way is to enable sudo user from running commands without being asked for a password
%admin ALL=NOPASSWD: ALLBut after all that i cant pass variables to the shell, for example:
if(!($stream = ssh2_exec($con, \&
quot;/home/user/ls /home/user/$v1\&
quot; )) )
Kevin
on 2008-04-22 13:12:28
@ shinta: No problem, glad it works!
shinta
on 2008-04-22 10:34:20
oh i'm sorry i didn't see #29. Patrick on 23 October 2007 comment
i'ts doesn't work in newes libssh2
i trying libssh2-0.14 and everything ok
thank u kevin for your article
GBU
Kevin
on 2008-04-22 09:36:13
@ shinta: Hi,
make: *** [ssh2.lo] Error 1
ERROR: `make\' failed
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
pear install -f ssh2
? Some people for some weird reason can't use pecl on their system, though ssh2 definitely is a pecl package…
shinta
on 2008-04-22 08:41:30
hi kevin i'have something problem with
command:
——————————————————–
pecl install -f ssh2
——————————————————–
and i got :
——————————————————–
No releases available for package \&
quot;pecl.php.net/ssh2\&
quot;
Cannot initialize 'ssh2', invalid or missing package file
Package \&
quot;ssh2\&
quot; is not valid
install failed
——————————————————–
whether i do connect to internet? to execute \&
quot;pecl install -f ssh2\&
quot;?
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?
few bugs when setting up ssh2
on 2008-04-18 08:37:13
working link - http://programming.has.no.com/2008/04/18/libssh2-and-php/
few bugs when setting up ssh2
on 2008-04-18 08:36:11
I had a few bugs when trying to get ssh2 to work, such as an error in the pelc and libssh2 not working inside a chroot…. fixes can be found here &
lt;a href=\&
quot;http://programming.has.no.com/2008/04/18/libssh2-and-php/\&
quot;&
gt;http://programming.has.no.com/2008/04/18/libssh2-and-php/&
lt;/a&
gt;
Kevin
on 2008-04-17 12:50:06
@ Wes: It might work if you open a shell instead of using the 'exec' method. One way to find out :) Let me know alright?
Sebastian
on 2008-04-17 10:41:25
Hey Wes,
if you have root access on that box, configure sudo and put that in your script (e.g. sudu rcapache2 reload).
rgds,
Wes
on 2008-04-17 01:25:03
Thanks a lot Kevin, I found your tutorial in the time I needed something like it the most. Thanks much.
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?
Sebastian
on 2008-04-09 15:57:01
Kevin,
does \&
quot;I see\&
quot; 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.
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,
Kevin
on 2008-04-09 13:14:55
@ Sebastian: I see. If you change the command like this it will probably work:
ssh2_exec($con, \'ls -al &
gt;&
gt; /tmp/file.txt; echo \&
quot;--command finished--\&
quot;\');
Sebastian
on 2008-04-09 09:19:01
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
ssh2_exec($con, \'ls -al &
gt;&
gt; /tmp/file.txt; echo \&
quot;--command finished--\&
quot; &
gt;&
gt; /tmp/file.txt\');
works as expected. The stout was directed into the file, and can be read by loggin in on console.
hrrghh.
Kevin
on 2008-04-08 23:17:14
@ Sebastian: Yeah I meant the tip of running an echo '__COMMAND_FINISHED__', setting blocking to false, etc. The one from the header 'tips'.
Sebastian
on 2008-04-08 15:00:49
Hey Kevin,
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?
You mean about valitading the results?
How shall I test?
We can switch over to email conversation, not to \&
quot;spam\&
quot; your commments. Drop a mail to s(dot)waitz(at)rtsgroup(dot)net.
Thanks a lot.
Kevin
on 2008-04-08 14:38:33
@ Sebastian: Hm, you stripped out all of the checks. Might that be one reason it did not echo anything? Furthermore, have you tried my second 'tip'?
Sebastian
on 2008-04-08 14:12:12
That works, five seconds sleep in the script, results in 5 sec timedifference in the logfile. I was able to touch a file on the remote system and echo some data in it.
But cat'ing the file to have some data back doesn't work.
Crazy!
Thats the codesnipped I'm working with:
&
lt;?php
error_reporting( E_ALL );
$con = ssh2_connect(\&
quot;cme1\&
quot;, 22);
ssh2_auth_password($con, \&
quot;rts\&
quot;, \&
quot;thesecret\&
quot;);
// execute a command
$stream = ssh2_exec($con, \&
quot;touch /tmp/file.txt; echo \\\&
quot;My Test\\\&
quot; &
gt;&
gt; /tmp/file.txt; sleep 5; cat /tmp/file.txt\&
quot;);
// collect returning data from command
stream_set_blocking($stream, true);
$data = \&
quot;\&
quot;;
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
fclose($stream);
?&
gt;
Kevin
on 2008-04-08 12:46:41
The disconnect is logged the same second the connect is. I guess therein lays your problem?
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?
Sebastian
on 2008-04-08 11:28:11
No changes in behavior. No errors. No hints.
connection establishes successfully, but no response.
I stripped the code for only connecting, executing, streaming, output. But nothing.
The /var/log/messages on the remote system sais:
Apr 8 04:21:13 cme1 sshd[21681]: Accepted password for rts from 10.34.2.9 port 51653 ssh2
Apr 8 04:21:13 cme1 sshd[21683]: Received disconnect from 10.34.2.9: 11: PECL/ssh2 (http://pecl.php.net/packages/ssh2)
Any ideas?
Thanks
Kevin
on 2008-04-08 08:59:47
@ 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?
&
lt;?php
$old_level = error_reporting( E_ALL );
?&
gt;
Does this tell you anything?
Sebastian
on 2008-04-07 15:39:39
Hey Kevin,
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.
On echo'ing $stream, I get \&
quot;Resource id#3\&
quot; back.
Any ideas?
Thanks a bunch
Kevin
on 2008-03-08 13:04:46
@ XSchne2036: You don't have phpize which is part of the php5-dev package. So it seems to me you skipped a step. Try (re)installing the requirements.
XSchne2036
on 2008-03-07 22:27:43
hi,
I followed your instructions
and …
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 \&
quot;stable\&
quot;, will instead download version 0.10, stability \&
quot;beta\&
quot;
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 ?
youme
on 2008-02-26 02:00:52
i have my own php script, no need to install on server, just upload php file on your server, and you have full access you server root, including system commands, exec commands, shell , SSH and lot more
if you want buy price is 150$ USD
email me mscmsc@brain.com.pk
Emanuel
on 2008-02-06 04:24:56
Very obliged for its aid. The example executed! Where I can find more functions of this I modulate? I intend to use it, to connect itself in the Mikrotik it saw ssh and to twirl rules
Kevin
on 2008-02-05 19:40:31
@ Emanuel: My bad, the example code was missing a: ';'
so:
die(\&
quot;function ssh2_connect doesn\'t exist\&
quot;)
needed to be:
die(\&
quot;function ssh2_connect doesn\'t exist\&
quot;);
I've updated the article accordingly. btw, you were using the CODE tags incorrectly but I've editted your comment for better readability.
Emanuel
on 2008-02-05 19:08:49
Hi, finally I obtained to install lib ssh2.so. to put when I go to twirl the test function that exists in its wiki:
if (!function_exists(\&
quot;ssh2_connect\&
quot;)) die(\&
quot;function ssh2_connect doesn\'t exist\&
quot;)
// log in at server1.example.com on port 22
if(!($con = ssh2_connect(\&
quot;server1.example.com\&
quot;, 22))){
echo \&
quot;fail: unable to establish connection\\n\&
quot;;
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, \&
quot;root\&
quot;, \&
quot;secretpassword\&
quot;)) {
echo \&
quot;fail: unable to authenticate\\n\&
quot;;
} else {
// allright, we\'re in!
echo \&
quot;okay: logged in...\\n\&
quot;;
// execute a command
if(!($stream = ssh2_exec($con, \&
quot;ls -al\&
quot; )) ){
echo \&
quot;fail: unable to execute command\\n\&
quot;;
} else{
// collect returning data from command
stream_set_blocking( $stream, true );
$data = \&
quot;\&
quot;;
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
fclose($stream);
}
}
\&
quot;]e in log of the apache returns me the following error:
PHP error Parse: syntax error, unexpected T_IF in /var/www/htdocs/a.php on line 4wanted to know if it would have as to help to decide this problem me
Kevin
on 2008-01-19 21:01:38
@ 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:
while( $buf = fread($shell,4096) ){
$data .= $buf;
}
fclose($shell);
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.
Pimmy
on 2008-01-18 18:50:19
I have the case where I create an SSH2 session to a server and from there I execute a script which will copy some files back to the original server via SCP (This is done like that for security reasons).
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?
Thanks,
Pimmy
Kevin
on 2008-01-17 18:09:55
@ Emanuel: Then I'm sorry Emanuel, I don't know slackware good enough to help you any further. But maybe the slackware forums are a good place to ask for further assistance as this seems to be distro related. Good luck anyway, too bad it doesn't work 'out of the box'.
Emanuel
on 2008-01-17 03:26:43
yes, the place /usr/include vi exists inside that another folder exists…
/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.
Kevin
on 2008-01-16 21:12:08
The binding between libssh2 and php is ssh2.so, and last time I checked it wasn't availble for versions higher than 0.14.
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?
Emanuel
on 2008-01-15 14:40:05
1º - I followed its tutorial one using the version libssh2-0.14.
./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”
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 &
lt;URL: http://gcc.gnu.org/bugs.html &
gt; 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!
Kevin
on 2008-01-15 08:32:02
@ Emanuel: Haven't used it since version 7, but isn't there something like slapt-get, or swaret that can (kind of) replace the package management tool aptitude?
If that doesn't work, let me know where you got stuck. what errors do you get. Thanks!
Emanuel
on 2008-01-15 04:59:15
hi, congratulations for the article! It wanted to know if you it would have some tip of as I can make this installation of slackware 11.0? I made you vary attempts and in all I did not get success. It wanted to know if it can give some tip to me.
Kevin
on 2007-12-16 13:18:04
@ Pimmy: Thank you for sharing!
Pimmy
on 2007-12-14 13:01:17
I knew it - I've posted the previous msg too soon.. But anyway, the problem was that for some reason, (from somewhere - clean tesing server) the compiler was referring to an old version of PHP. Still cant figure out where is the old libraries.
So I fixed the problem by pointing to my real PHP:
root@tangra:ssh2-0.10&
gt;phpize
root@tangra:ssh2-0.10&
gt;./configure \
&
gt; –with-php-config=/usr/local/php/bin/php-config \
&
gt; –with-ssh2=/usr/local
root@tangra:ssh2-0.10&
gt;make clean
root@tangra:ssh2-0.10&
gt;make
Then everything went smooth.
Cheers.
Pimmy
on 2007-12-14 12:14:56
Now I am stuck with another problem. In my error_log I have the following line:
——————————————————–
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
——————————————————–
I've recompiled both libssh2-0.14 and ssh2-0.10 several times in different ways but no joy.
Have you got any suggestions?
Hiren Thakkar
on 2007-12-13 14:15:19
Thanks .. This may be helpful to me.
Pimmy
on 2007-12-13 13:47:06
I got stuck from the beginning compiling libssh2..
—————————————————-
root@tangra:libssh2-0.14&
gt;make all install
make[1]: Entering directory `/space/installs/libssh2-0.14/src'
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&
gt;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
Kevin
on 2007-12-09 19:46:49
@ Nigel: I've never used PHP on Windows, but this is what I Can tell you: The PECL module acts as an interface between PHP &
amp; libssh2, and it seems to me you've totally skipped the libssh2 part. I wonder if this is even available on windows?
Nigel
on 2007-12-09 19:33:29
Hi Kevin, looks like you've done some really good work here, problem is I am stuck at first base with windows.
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
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
Kevin
on 2007-11-07 17:53:58
Thanks for sharing that information Bryan! I hadn't tried 0.17 yet, seems more people are running into problems with it.
Bryan
on 2007-11-06 21:46:13
Thank you! I'm so glad I found this article, or I may have not been able to get this working.
I initially tried the 0.17 version of libssh, and I got the \&
quot;make 1\&
quot; error from PECL. When I installed 0.14, everything became peachy.
Kevin
on 2007-10-31 19:21:39
@ vezzo: If your really want an 'interactive' session with the remote server, you cannot use the exec function. You will have to use the shell function instead. Hope it helps. K
vezzo
on 2007-10-31 15:06:20
hey, kevin, why i can execute a lot of command but, I can't change directory with \&
quot;cd Desktop\&
quot; for example????
Kevin
on 2007-10-25 19:28:20
@ Patrick. I'm not certain if the pecl module supports the newer version (0.17). So maybe we can figure out what went wrong with 0.14? That way we can be certain that it can work.
@ JavieL: I'm sorry what do you mean exactly?
JavieL
on 2007-10-23 18:05:37
Some example for view?¿
Patrick
on 2007-10-23 14:25:39
Ok, first of all, this is some awesome tutorial!. I hope i can get this working!
Below my full report on what went wrong.
Today: 23-10-2007 i followed the instructions on:
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.
Kevin
on 2007-10-12 11:30:20
@ prasanna: I'm not familiar with the libphp5.so configuration and so I do not have a way to reproduce your errors. I always just include the ssh2.so file as an extension in the php.ini (either cli, or apache, depending on the intended use). So unfortunately I can't help you with this.
prasanna
on 2007-10-11 14:53:11
i created the ssh2.so using
phpize &
amp;&
amp; ./configure –with-ssh2 &
amp;&
amp; make.
I run the above command under PECL/SSH directory.
But i have a doubt and i explained it below.
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.
&
lt;LoadModule php5_module /home/nprasanna/test/modules/libphp5.so&
gt;
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
Kevin
on 2007-10-10 22:20:06
@ prasanna:
I believe you've typed:
pecl install ssh
instead of:
pecl install ssh2
Kevin
on 2007-10-10 22:12:33
@ Edi: Oh I see, I haven't used it like that. Only from CLI actually. I don't think you can store the connection resource in the $_SESSION variable so maybe you should write a CLI daemon in PHP that can accept commands as it keeps a connection open. Or just connect for every command you send.
You should really ask yourself if this technology belongs in a web app b.t.w. And if it does.. Please sanitize your input :)
prasanna
on 2007-10-10 12:33:37
I got the error
I had installed pecl, openssl.
I found no useful results while googling with the below error.
No releases available for package \&
quot;pecl.php.net/ssh\&
quot;
Cannot initialize 'pecl/ssh', invalid or missing package file
Package \&
quot;Array\&
quot; is not valid
install failed
How to resolve it, please help me.
prasanna
Edi
on 2007-10-10 00:13:36
Ok, thanks, I've got it,
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.
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.
Kevin
on 2007-10-09 07:49:17
@ Edi: Sure, after you've used ssh2_connect &
amp; ssh2_auth_password you can use as many ssh2_execs as you want.
Edi
on 2007-10-09 00:56:00
Very nice tutorial, thanx,
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?
Stefan Ideler
on 2007-10-05 17:26:05
For people getting the make 1 error, I\'ve found the following 2 diff files to solve the problem for me.
(Compiling it on suse 10.2, 64 bit).
http://svn.mandriva.com/cgi-bin/viewvc.cgi/packages/cooker/php-ssh2/pristine/SOURCES/
Download the tar, patch the 2 files with the diff files, then install it using phpize &
amp;&
amp; ./configure –with-ssh2 &
amp;&
amp; make &
amp;&
amp; 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
Kevin
on 2007-10-04 16:58:33
@ Joo: You shouldn't have to create the php.ini yourself. It should already be installed if php-cli is succesfully installed. That's probably why it isn't working.
try:
updatedb &
amp;&
amp; locate php.ini |egrep '^/etc/.*\.ini$'
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.
Joo
on 2007-10-04 15:16:50
Thank you for your advice, Kevin.
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.
Kevin
on 2007-10-04 12:03:30
@Joo: The module ssh2.so isn't loaded in PHP. This could mean you have to double check the Installation Step, and see if extension=ssh2.so is added to the correct php.ini
I see you're using commandline so probably something like /etc/php5/cli/php.ini
Joo
on 2007-10-04 09:38:52
I just copied your php script and then run it on my server. (Before doing that, I followed all your instruction and installed everything) However, I met a following message.
- [root@localhost htdocs]# php -f ssh2.php
function ssh2_connect doesn't exist[
What happened in my php?
Thanks in advance.
Oriol
on 2007-09-23 18:46:32
I'm using Ubuntu dapper 6.06 LTS in my servers. When I try to install openssl-dev I don't found it. But, the correct package for me is libssl-dev.
Tancks for your guide is very useful for me.
ServerChief
on 2007-09-13 21:37:51
Hi,
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,
ServerChief
http://www.serverchief.com
Jason
on 2007-09-11 09:10:36
Hi Kevin,
Do you have any idea about SSH Tunneling? I tried the following code but don't work:
$connection = ssh2_connect($SSH_HOST, $SSH_PORT);
ssh2_auth_password($connection, $SSH_LOGIN, $SSH_PASSWORD);
$tunnel = ssh2_tunnel($connection, $REMOTE_HOST.\&
quot;:\&
quot;.$REMOTE_PORT, $LOCAL_PORT);
Could you please help me?
Kevin
on 2007-08-30 17:36:01
@ trume: sorry dude I don't have email ;)
Anyhow, you might want to concatenate all of your commands with semicolons like this:
ls -al; echo \&
quot;!BREAK!\&
quot;; cat /proc/cpuinfo; echo \&
quot;!BREAK!\&
quot;; cat /proc/loadavg; echo \&
quot;!BREAK!\&
quot;; df -h
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 \&
quot;!BREAK!\&
quot; (see 'explode' function in PHP manual)
trume
on 2007-08-30 17:00:57
hi,kevin,i wander if i can run the ssh connection once and get all infomation in that way.
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.
thanks.
Kevin
on 2007-08-28 00:18:41
@ Jan: When I install I get similar warnings like:
/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
Can it be that you ran into this bug?
http://pecl.php.net/bugs/bug.php?id=11779
jan
on 2007-08-27 16:35:15
i get this …
/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'
/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 :(
Peter
on 2007-08-14 21:18:31
Never mind, my mistake. I forgot to run 'make all install'. Duh!
Peter
on 2007-08-14 21:12:39
Great tutorial; now to make it work. ;-)
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/
Any suggestions?
tr
on 2007-08-13 04:32:34
Instead of 'pear install -f ssh2' try with 'pecl install -f ssh2'.
Kevin
on 2007-08-10 10:47:10
Did you type '-f' ?
Will
on 2007-08-09 22:04:09
First of all, great article. Second I'm having an error I was wondering if you had any thoughts on. I've been following the instructions pretty closely and all seems to have gone well up until the ssh2.so portion. Here is the error that I get when trying to run pear….
No releases available for package \&
quot;pear.php.net/ssh2\&
quot; - package pecl/ssh2 can be installed with \&
quot;pecl install ssh2\&
quot;
Cannot initialize 'ssh2', invalid or missing package file
Package \&
quot;ssh2\&
quot; is not valid
install failed
Any help would be great. Thanks…
NiKo
on 2007-07-30 17:27:16
Great article, thanks.
Kevin
on 2007-07-28 12:35:22
Hi Sara, nice to know that improvements are on it's way. And I'd just like to say you did a great job on integrating ssh into php. It really made my life easier (it definitely beats making system calls and parsing output ;)
And as for the PHP manual, maybe I will contribute directly to it. Not a bad idea. Thanks for your comment!
Sara Golemon
on 2007-07-27 21:13:25
Glad you like PECL/ssh2 and libssh2! :)
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.
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.