» Make SSH connections with PHP

ssh phpNot 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

Prerequisites

Packages

First let's install the following packages:

sudo aptitude update
sudo aptitude install php5-dev php5-cli php-pear build-essential \
openssl-dev zlib1g-dev

That should set us up alright.

Update - On recent Ubuntu machines, there's no need to do any compiling anymore:

aptitude install libssh2-1-dev libssh2-php
#pecl install -f ssh2
#echo 'extension=ssh2.so' > /etc/php5/conf.d/ssh2.ini

If the above works for you (you should see: "Build process completed successfully"), you can skip to Great! PHP supports SSH - time to code.

If not, you probably need to compile manually, continue reading here.

libssh2

We need libssh2 from sourcefourge. We have to compile this, but no worries, this is all you need to do:

cd /usr/src
wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
tar -zxvf libssh2-0.14.tar.gz
cd libssh2-0.14/
./configure
make all install

That's it! Easy right?

- 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:

pecl install -f ssh2

The -f makes sure ssh2 is installed even though there's not a stable candidate. You could also use the package name: ssh2-beta to overrule this.

Now you need to make sure our new ssh2.so module is loaded by PHP. Edit your php.ini file (for CLI utitilies: /etc/php5/cli/php.ini, for Apache utilities /etc/php5/apache2/php.ini)

extension=ssh2.so

It should be placed beneath the: "Dynamic Extensions" section somewhere around line 515.

Great! PHP supports SSH - time to code

You've just enabled ssh2 support in PHP. Now how can we make use of this? There are 2 options. SSH supports the: 

  • execute method
    This tells the server's operating system to execute something and pipe the output back to your script. (recommended)
  • shell method
    This opens an actual shell to the operating system, just as you would normally when logging in with your terminal application. Some routers that don't have a full POSIX compliant implementation, but run their own application as soon as you login, require this. (advanced)

Method 1: Execute

Best would be to create functions or even a class for the following code, but this is the basic idea and will definitely get you started:

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("server1.example.com", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "secretpassword")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";

// execute a command
if(!($stream = ssh2_exec($con, "ls -al" )) ){
echo "fail: unable to execute command\n";
} else{
// collect returning data from command
stream_set_blocking( $stream, true );
$data = "";
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
fclose($stream);
}
}
}
?>

Method 2: Shell

Best would be to create functions or even a class for the following code, but this is the basic idea and will definitely get you started:

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("server1.example.com", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "secretpassword")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";

// create a shell
if(!($shell = ssh2_shell($con, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))){
echo "fail: unable to establish shell\n";
} else{
stream_set_blocking( $shell, true );
// send a command
fwrite($shell,"ls -al\n");
sleep(1);

// & collect returning data
$data = "";
while( $buf = fread($shell,4096) ){
$data .= $buf;
}
fclose($shell);
}
}
}
?>

Tips

Sometimes when a server is busy, or a connection is buggy, the buffer may run dry, and the PHP script stops collecting data from a command output (even though the command hasn't completed yet!). There are a couple of things you could do about that:

ssh2_exec($con, 'ls -al; echo "__COMMAND_FINISHED__"' );
?>

Now, in the loop where you keep checking for the buffer, just see if the COMMAND_FINISHED line is coming by. Because then you know you have all the data. To avoid infinite loops, just limit the loop with a timeout of 10 seconds or so:

$time_start = time();
$data = "";
while( true ){
$data .= fread($stream, 4096);
if(strpos($data,"__COMMAND_FINISHED__") !== false){
echo "okay: command finished\n";
break;
}
if( (time()-$time_start) > 10 ){
echo "fail: timeout of 10 seconds has been reached\n";
break;
}
}
?>

In the example above, you'd better set stream_set_blocking to false.

Can't get enough?

PHP can send files over ssh!

ssh2_scp_send($con, "/tmp/source.dat", "/tmp/dest.dat", 0644);
?>

Doesn't work?

Check the following:

 - Did you follow every step of the prerequisites & installation how to in this article?
 - On the serverside, 'PasswordAuthentication yes' must be enabled in the sshd_config. Default is yes on most servers, but in some cases you will have to turn this on yourself by making sure the following lines are in place in the file: /etc/ssh/sshd_config:

# Change to yes to enable tunnelled clear text passwords
PasswordAuthentication yes

If you've made any changes, ssh needs a restart

/etc/init.d/ssh restart 

 

Post a comment here if it's still failing. Don't forget to paste the error that you're getting.

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 (should be slower though)

Stay up to date

You can track my blog rss articles and rss comments. You may also find my rss bookmarks interesting. Or twitter Follow me on Twitter


Like this article?

   Then Dzone it!
Or use another bookmark button below to show your support &
help me spread the word.


tags: php, ssh, ubuntu, PEAR, PECL, libssh2, CLI, apache, router
category: Programming - PHP
read: 149,688 times

Add comment

(required, shown)(required, not shown)for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

 Track replies: rss feed comments feed

Comments

#249. Kevin on 07 January 2010

Gravatar.com: Kevin@ 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

#248. khalid on 07 January 2010

Gravatar.com: khalidhello

ssh2_auth_password doesn't work,

i do this :
... [more] PasswordAuthentication yes
and
/etc/init.d/ssh restart

and it works perfect.

thanks

#247. David on 31 December 2009

Gravatar.com: DavidUnfortunately this process no longer works on Ubuntu. "pecl install -f ssh2" fails to make. Sounds like a recurring problem over the course of years.

#246. Derak on 24 December 2009

Gravatar.com: DerakI'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?

#245. Alfredo Rivera on 14 December 2009

Gravatar.com: Alfredo RiveraAny Idea On How To Solve It?

#244. Kevin on 13 December 2009

Gravatar.com: Kevin@ 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 & exec methods.

#243. Alfredo Rivera on 12 December 2009

Gravatar.com: Alfredo RiveraHello Kevin, I have Been Writing a CodeIgniter Library For The SSH2 Extension and I'm Encountering some troubles.

<?php
 
class Ssh2
{
 
 
function __construct()
{
$this->ci =& get_instance();
}

function conect($server,$port,$muser,$mpass)
{
if(!($con = ssh2_connect($server,$port))){
echo "<font color='red'><b>Failure: Unable To Connect</b></font>";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con,$muser,$mpass)) {
echo "<font color='red'><b>Failure: Unable To Authenticate</b></font>";
} else {
// allright, we're in!
echo "<font color='green'><b>Success: Logged In, Connected At ".$server." With User ".$muser." And Password ".$mpass."</b></font>";
}
}
}

function execute($server,$port,$command)
{
$con = ssh2_connect($server,$port);
if(!($stream = ssh2_exec($con,$command)) ){
echo "<font color='red'><b>Failure: Unable To Execute Command</b></font>";
} else{
// collect returning data from command
/*stream_set_blocking($stream);*/
$data = "";
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?

#242. Matt Kukowski on 08 December 2009

Gravatar.com: Matt KukowskiUntil 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!
... [more]
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)

#241. Kevin on 06 December 2009

Gravatar.com: Kevin@ Manoj: You're welcome!

#240. Manoj on 21 November 2009

Gravatar.com: ManojThank you for this wonderful documentation! :)

#239. Kevin on 08 November 2009

Gravatar.com: Kevin@ 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

#238. Mike on 28 October 2009

Gravatar.com: MikeI 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.

#237. Jafar on 27 October 2009

Gravatar.com: JafarHi, 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).

#236. Kevin on 09 October 2009

Gravatar.com: Kevin@ nebj00la: Using shell & 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)
... [more]
and
http://pear.php.net/pepr/pepr-proposal-show.php?id=586
A PEAR class in the making

#235. nebj00la on 06 October 2009

Gravatar.com: nebj00laKevin,

Here's the SSH daemon running on the remote device:

SSH-2.0-RomSShell_4.31
... [more]
Thanks,
John

#234. nebj00la on 06 October 2009

Gravatar.com: nebj00laKevin,

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
... [more] return rc = -1
return rc = -1
return rc = -1
etc. etc.

Please advise,

Thanks,
John

#233. macrocode on 06 October 2009

Gravatar.com: macrocode2 words: THANK YOU!

#232. nathan on 04 October 2009

Gravatar.com: nathanvery 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.
... [more]
wonder if it'll work on my wamp as well.

thanks for the help in getting me started.

Nathan.

#231. Llorca on 22 September 2009

Gravatar.com: LlorcaKevin,

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.

#230. Alfredo Rivera on 18 September 2009

Gravatar.com: Alfredo RiveraI 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?

#229. Kevin on 04 September 2009

Gravatar.com: Kevin@ Derak: Do a find on php.ini and try to see if Maybe a different php.ini is used?

#228. Derak on 28 August 2009

Gravatar.com: DerakI 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 "/usr/lib/php/extensions/no-debug-non-zts-20060613/"

... [more] and my php.ini file has:
extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20060613/"
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 "Registered PHP Streams" section, but it is not.

Any ideas? I feel like I've tried everything here.

#227. Kevin on 22 August 2009

Gravatar.com: Kevin@ Reinard: Thanks for sharing!

#226. Reinard on 18 August 2009

Gravatar.com: ReinardIf 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.

#225. Kevin on 12 August 2009

Gravatar.com: Kevin@ Eugene van der Merwe & Brian & 1oBuZ: If using a different version doesn't work, you could try using a different method: Try switching from blocking <> 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 '&&' 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

#224. 1oBuZ on 09 August 2009

Gravatar.com: 1oBuZI have got the same problem wehn u will find a solution please tell here :)

#223. Brian on 08 August 2009

Gravatar.com: BrianHello,

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.
... [more]
The command seems to complete because i can do something like:

cat /etc/php.ini >> 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?

#222. Eugene van der Merwe on 08 August 2009

Gravatar.com: Eugene van der Merwe#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 "Unable to request a channel from remote host.".

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).
... [more]
So I suppose my question is, how to I open one connection and run multiple commands without getting any errors? :-)

#221. Eugene van der Merwe on 08 August 2009

Gravatar.com: Eugene van der Merwe#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.

#220. Llorca on 31 July 2009

Gravatar.com: LlorcaThis 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

#219. Kevin on 28 July 2009

Gravatar.com: Kevin@ 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 & review logs again.

#218. Charlie on 27 July 2009

Gravatar.com: CharlieShould also mention this occurs on the 2nd ssh_exec() call. The first works fine.

#217. Charlie on 27 July 2009

Gravatar.com: CharlieWhen 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?

... [more] Also, I'm using the latest releases... thanks for any response. Great post btw.

#216. Kevin on 15 July 2009

Gravatar.com: Kevin@ Santosh: Please provide me with more details. This is impossible to debug for me, please understand.

#215. Santosh on 09 July 2009

Gravatar.com: Santoshphp ssh1.php
okay: logged in...

"NO OUTPUT"

#214. Kevin on 03 July 2009

Gravatar.com: Kevin@ Stephen: OK Stephen, I'm glad you got it working man. And thanks for sharing.

#213. Stephen on 01 July 2009

Gravatar.com: StephenIt 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("ssh2.sftp://{$sftp}{$DestFile}","w");
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.

#212. Stephen on 01 July 2009

Gravatar.com: Stephen@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="/tmp/somefile.txt";
echo "Transferring to $Server...";
// Force a cache flush
ob_flush();
flush();

$con=ssh2_connect($Server,22);
ssh2_auth_password($con,"user","password");
ssh2_scp_send($con,"cache/somefile.txt","$DestFile") or die("Could not transfer to $Server - Operation aborted.");
 
// Force a cache flush again
echo "OK.\n";
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?

#211. Kevin on 26 June 2009

Gravatar.com: Kevin@ 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
... [more]
http://pecl.php.net/package-info.php?package=ssh2&version=0.11.0

#210. Fatima on 21 June 2009

Gravatar.com: FatimaHi Kevin,

When i execute the command

pecl install -f ssh2,
... [more]
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

#209. Stephen on 21 June 2009

Gravatar.com: StephenI 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!

#208. Stephen on 21 June 2009

Gravatar.com: StephenThis 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 "<pre>".$data."</pre>";

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 "CALLED" via exec. The actual command line is

[code=text]php comparefile.php file1 file2[/code]

It fails with "function ssh2_connect doesn't exist".

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?

#207. shaunah on 20 June 2009

Gravatar.com: shaunahi 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.

#206. Kevin on 18 June 2009

Gravatar.com: Kevin@ 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:
"echo '{$str}' > /home/point/etmain/server.cfg"

Which could also get your config file across. At least you isolate any permission related issues that way.

#205. purelife on 17 June 2009

Gravatar.com: purelife@shaunah and @Alan Choyna:

Try this:

http://phpseclib.sourceforge.net/
... [more]
Since it's pure-PHP, you don't have to bother with any PECL nonsense.

#204. Alan Choyna on 14 June 2009

Gravatar.com: Alan ChoynaI 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 "pecl install -f ssh2" 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 "pecl install -f ssh2" command fails with a
... [more] ": Function eregi() is deprecated in PEAR/Registry.php on line 735. Error make failed".

I'm not sure what to do to debug this issue. Any advice?

Thanks in advance,

Alan

#203. Han on 14 June 2009

Gravatar.com: HanKevin, 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 "No ballzzz...";
}

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 ?

#202. shaunah on 12 June 2009

Gravatar.com: shaunahpear 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.

#201. Kevin on 11 June 2009

Gravatar.com: Kevin@ shaunah: That kind of depends how they blocked it. Can you run pear?

#200. shaunah on 10 June 2009

Gravatar.com: shaunahmy host doesn't support let me install pecl modules. can i still do ssh2?

#199. Kevin on 10 June 2009

Gravatar.com: Kevin@ 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.

#198. Rachid on 08 June 2009

Gravatar.com: RachidI 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.

"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.
... [more] Are you sure you want to continue connecting (yes/no)?"

I can bypass this by pressing Yes on the SSH client.
But how can I bypass this with PHP ?

#197. Daisy on 08 June 2009

Gravatar.com: DaisyThe 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
... [more] 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

#196. Kevin on 26 May 2009

Gravatar.com: Kevin@ 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 & I heard a LOT of complaints & bugs with that version..

#195. estus on 22 May 2009

Gravatar.com: estusWhen I use shell method site/script is loading, loading... and loading and nothing happens. I don't have any ideas whats wrong... ;/

#194. estus on 22 May 2009

Gravatar.com: estusI 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.

#193. Kevin on 22 May 2009

Gravatar.com: Kevin@ 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 "1" is never reached, hence $buf contains something that evaluates to true I suppose.

#192. estus on 22 May 2009

Gravatar.com: estusI'm using source code from your article.

error_reporting(E_ALL); 
// ini_set('display_errors', 1);
 
if (!function_exists("ssh2_connect")) die("Error #0: Nie wykryto ssh2 na serwerze.");
if(!($con = ssh2_connect("xxx", 27))){
echo "Error #1: Nie moge sie polaczyc z serwerem.\n";
} else {
if(!ssh2_auth_password($con, "xxx", "xxx")) {
echo "Error #2: Bledny login lub haslo.\n";
} else {
 
echo "Wszystko dziala.\n";

if(!($stream=ssh2_exec($con, "whoami" ))){
echo "Error #3: Komenda nie moze zostac wykonana.\n";
} else {
// zwraca wynik
stream_set_blocking($stream, true);
$data = "";
echo"0"; // helps
while($buf=fread($stream,4096)){
$data .= $buf;
echo $data;
echo"1"; // 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 "0" but no "1". I was try with fgets or stream_gets_content but it doesn't works too.

#191. Kevin on 22 May 2009

Gravatar.com: Kevin@ 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.

#190. estus on 20 May 2009

Gravatar.com: estusHi,
thanks for this tutoturial, but I have some problem. Commands work fine but it doesn't return data.

Am I doing something wrong?

#189. Kevin on 14 May 2009

Gravatar.com: Kevin@ 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?

#188. Ed on 07 May 2009

Gravatar.com: EdI 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
... [more] /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

#187. Albert on 06 May 2009

Gravatar.com: AlbertWarning: 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.

#186. Janckos on 20 April 2009

Gravatar.com: JanckosGreat!

#185. Kevin on 03 April 2009

Gravatar.com: Kevin@ Mike: very well!

#184. Mike on 01 April 2009

Gravatar.com: Mikescratch 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

#183. Mike on 01 April 2009

Gravatar.com: MikeTo clarify my web page pulls up:

fail: unable to authenticate

echos straight from the code.

#182. Mike on 01 April 2009

Gravatar.com: MikeI 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.

#181. stef on 16 March 2009

Gravatar.com: stefYeah, 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.

#180. Kevin on 02 March 2009

Gravatar.com: Kevin@ redux: Sounds like you need shell, as is explained in the article.

@ Kristjan Adojaan: Thanks for sharing!

#179. Kristjan Adojaan on 27 February 2009

Gravatar.com: Kristjan AdojaanIn 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

#178. redux on 26 February 2009

Gravatar.com: reduxCan 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,

#177. Kevin on 21 February 2009

Gravatar.com: Kevin@ purelife: That looks amazing!

#176. purelife on 17 February 2009

Gravatar.com: purelifeif you can't get php_ssh2.dll working, you might have better luck with a pure-PHP implementation of ssh:

http://phpseclib.sourceforge.net/

#175. Kevin on 11 February 2009

Gravatar.com: Kevin@ 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?

#174. Daisy on 04 February 2009

Gravatar.com: DaisyHello 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:
... [more]
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.

#173. Kevin on 01 February 2009

Gravatar.com: Kevin@ John: Thank you for sharing!

#172. John on 26 January 2009

Gravatar.com: JohnThanks 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...
... [more]
Once built, I had to manually add an ssh2.ini file in /etc/php5/apache2/conf.d-
"# configuration for php SSH2 module
extension=ssh2.so"
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

#171. Shizuka on 23 January 2009

Gravatar.com: ShizukaThank 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.

#170. Kevin on 15 January 2009

Gravatar.com: Kevin@ ian: Thanks for the heads up! I've updated the article

#169. ian on 14 January 2009

Gravatar.com: ianssh2 is now upto v1.

everything works nicely, remember to apt-get make for ubuntu.

#168. Kevin on 30 December 2008

Gravatar.com: Kevin@ Jamie: Thanks for sharing

@ Jaimie Sirovich: You may even concider contributing such a class to PEAR. That will guarantee solid testing, ideas for improvement & feature requests & bug reports.

#167. Jaimie Sirovich on 19 December 2008

Gravatar.com: Jaimie SirovichThe 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

#166. Virg on 19 December 2008

Gravatar.com: VirgJust want to say thanks for this article. It was super helpful. =)

#165. Jamie on 18 December 2008

Gravatar.com: JamieI 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:
... [more]
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!

#164. Kevin on 17 December 2008

Gravatar.com: Kevin@ Nicolas: Need. More. Output :) Check below.

#163. Nicolas on 12 December 2008

Gravatar.com: Nicolashi, i run :
pecl install -f ssh2-beta
and get this error
make: *** [ssh2_fopen_wrappers.lo] Error 1
ERROR: `make' failed
... [more]
helpmeeeee!!
tanks

#162. Kevin on 10 December 2008

Gravatar.com: Kevin@ Allie: Great that you share that with us. Thanks

#161. Allie on 05 December 2008

Gravatar.com: AllieI 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 "PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php5/lib/modules/ssh2.so' - (null) in Unknown on line 0". 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 && make install
Hope this will hope someone

#160. Kevin on 01 December 2008

Gravatar.com: Kevin@ Karol: I have enough hobbies as it is ;) but feel free man!

#159. Karol on 28 November 2008

Gravatar.com: KarolIt would awesome to see this integrated with something like phpterm:
http://phpterm.sourceforge.net/

#158. Søren Jensen on 23 November 2008

Gravatar.com: Søren JensenMy bad... I see #133 already found that solution.
Reading comments do pay off, heh :-)

#157. Søren Jensen on 23 November 2008

Gravatar.com: Søren JensenThanks 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.

#156. Alf Marius on 17 November 2008

Gravatar.com: Alf MariusGreat guide, thanks! :)

#155. Kevin on 13 November 2008

Gravatar.com: Kevin@ Trenton & 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.
... [more]
Also, you might want to try to CTRL+F through the comments to see if your problem was already solved.

#154. Caesonia on 13 November 2008

Gravatar.com: CaesoniaAs 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.
... [more]
What a fricken joke.

#153. Trenton on 11 November 2008

Gravatar.com: TrentonI am trying to follow this but the pecl build is erroring out.. @ ssh2.lo. Can you possibly provide some assistance?

"make: *** [ssh2.lo] Error 1" feel free to email me.

#152. Kevin on 03 November 2008

Gravatar.com: Kevin@ 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!

#151. Bob Likes Your Tutorial on 02 November 2008

Gravatar.com: Bob Likes Your TutorialGreat tutorial but it didn't work for me. In my case, the script just returned "logged in".. 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.

#150. Sander on 27 October 2008

Gravatar.com: SanderWe figured it out:
The command for the cronjob:
wget -q http://www.xxxxxx.xxx/getfiles.php

#149. Sean on 23 October 2008

Gravatar.com: SeanIf 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');

#148. Kevin on 22 October 2008

Gravatar.com: Kevin@ Sander: There are two php.ini's. One for CLI & one for Apache. Maybe you need to enable your so in your CLI's php.ini?

#147. Sander on 22 October 2008

Gravatar.com: SanderHi Kevin,
When I run the script with my browser there is no problem, but in the cronjob the function ssh2_connect() is unknown.
Other "normal" 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

#146. Kevin on 21 October 2008

Gravatar.com: Kevin@ 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?

#145. Sander on 20 October 2008

Gravatar.com: SanderHi, 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

#144. Han on 14 October 2008

Gravatar.com: HanNo prob ! :)

Glad that its actually working ;-)

#143. Kevin on 14 October 2008

Gravatar.com: Kevin@ Han: Awesome man. Good job & thanks for sharing.

#142. Han on 14 October 2008

Gravatar.com: HanUPDATE: 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="-L$i/lib -lcrypto"


change that into :
if test -r /usr/lib64/libcrypto.a -o -r /usr/lib64/libcrypto.$SHLIB_SUFFIX_NAME; then
OPENSSL_LIBLINE="-L/usr/lib64 -lcrypto"

#141. Kevin on 14 October 2008

Gravatar.com: Kevin@ Han: Could work I suppose, if the so is binary compatible that is (other machine also 64bits? etc).

#140. Han on 14 October 2008

Gravatar.com: HanPS: 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,

#139. Han on 14 October 2008

Gravatar.com: HanWell, 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 :(

#138. Kevin on 14 October 2008

Gravatar.com: Kevin@ Han: If I remember correctly you fixed some issues on version libssh2-0.14, and that worked better?

#137. Han on 14 October 2008

Gravatar.com: HanDear 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 )
... [more]
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,

#136. Kevin on 10 October 2008

Gravatar.com: Kevin@ slak: Thanks for sharing.

#135. Kevin on 10 October 2008

Gravatar.com: Kevin@ Marek: I have not. But for your specific case, you might want to consider using:

echo "user:pass" | chpasswd

#134. Marek on 10 October 2008

Gravatar.com: MarekHello,
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?

#133. slak on 09 October 2008

Gravatar.com: slakThanks 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
... [more]
I had to get the patch from: http://www.billpitz.com/howto/php-libssh2.html

cd /tmp/pear/cache/ssh2-0.10
patch < php-libssh2.diff
phpize && ./configure --with-ssh2 && make

Hope that helps someone.

#132. Kevin on 06 October 2008

Gravatar.com: Kevin@ 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.

#131. Eugene van der Merwe on 06 October 2008

Gravatar.com: Eugene van der MerweThank 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.
... [more]
Please assist.

#130. Kevin on 29 September 2008

Gravatar.com: Kevin@ faizal: My pleasure!

#129. faizal on 26 September 2008

Gravatar.com: faizalGreat article...I've searched this topic for a long time. But thx God I found it here...Thx to Mr. Kevin

#128. Kevin on 21 September 2008

Gravatar.com: Kevin@ 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.

#127. chris on 18 September 2008

Gravatar.com: chrisI'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 ...
... [more]
Can anyone help?

#126. Kevin on 13 September 2008

Gravatar.com: Kevin@ Neil: Thanks for sharing!

#125. Neil on 11 September 2008

Gravatar.com: NeilI 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. :)

#124. Daniele Siddi on 10 September 2008

Gravatar.com: Daniele SiddiOK, Thank you for your guide, it helped me very much.

#123. Kevin on 09 September 2008

Gravatar.com: Kevin@ Shane: OK thanks a lot!

#122. Shane on 09 September 2008

Gravatar.com: ShaneIt's at the bottom of the post.

#121. Kevin on 08 September 2008

Gravatar.com: Kevin@ Shane: Yeah I noticed you've copied parts of my tutorial to your blog. I would appreciate some kind of backlink for that.

#120. Shane on 08 September 2008

Gravatar.com: Shane@ 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.

#119. Kevin on 05 September 2008

Gravatar.com: Kevin@ 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.

#118. Shane on 04 September 2008

Gravatar.com: ShaneKevin... I am also getting the "PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) 'libssh2.so' in Unknown on line 0" error when I run
[code]
[root@apollo lib]# php -r "echo (int)function_exists('ssh2_exec');"
[/code]

Any ideas?

#117. Kevin on 28 August 2008

Gravatar.com: Kevin@ bvidinli: Thank you for noticing! I've updated the article.

#116. bvidinli on 28 August 2008

Gravatar.com: bvidinlito reader:
build-essential
not buid-essential
fix that in your codes..

#115. Kevin on 27 August 2008

Default avatar:Kevin@ 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.

#114. Han on 15 August 2008

Default avatar:HanI need urgent help with PHP & 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 :/

#113. Kevin on 26 July 2008

Default avatar:Kevin@ 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?

#112. David Merel on 25 July 2008

Default avatar:David MerelI 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?


... [more] 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

#111. Kevin on 25 July 2008

Default avatar:Kevin@ ken@ghx: So far I've only tested it by supplying username & 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?

#110. ken@ghx on 23 July 2008

Default avatar:ken@ghxhas 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.

#109. Han on 22 July 2008

Default avatar:HanSorry 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 !

#108. Kevin on 22 July 2008

Default avatar:Kevin@ 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.

#107. Han on 22 July 2008

Default avatar:HanKevin, ik snap niet heel goed wat ik precies moet doen ...

Kan jij me ff op weg helpen ?

Thx

#106. Kevin on 21 July 2008

Default avatar:Kevin@ Han: Warnings can usually be ignored. Errors not. The error I saw in your output was: "too many arguments to function libssh2_session_methods"

I ran it through google and this was the first hit:
http://pecl.php.net/bugs/bug.php?id=11779

... [more] Looks like your problem so it might help you

#105. Han on 21 July 2008

Default avatar:HanPosted the whole stuff again on pastebin:

http://pastebin.org/55011

#104. Kevin on 21 July 2008

Default avatar:Kevin@ Han: Can't really tell based on the output given. Maybe post the next failures on pastebin.org

#103. Han on 21 July 2008

Default avatar:HanKevin, 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 ...

#102. Kevin on 21 July 2008

Default avatar:Kevin@ Han: I still use v14 and have no issues with it. People have reported similar issues with later versions. It works on debian & ubuntu 64bits systems. I'm unable to test on suse 64bits.

#101. Han on 21 July 2008

Default avatar:Hankevin, 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

... [more] Help is much appreciated :(

#100. Kevin on 21 July 2008

Default avatar:Kevin@ 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 & build tools).

#99. Han on 21 July 2008

Default avatar:HanKevin, 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

#98. Han on 21 July 2008

Default avatar:Hankevin, 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 ?

#97. Kevin on 21 July 2008

Default avatar:Kevin@ Han: Please follow the tutorial closely. It says '-f' and explains why that is needed.

#96. Han on 21 July 2008

Default avatar:HanAfter installing libssh2 and trying to install ssh2 i get :

Aphrodite:~ # pear install ssh2
No releases available for package "pear.php.net/ssh2" - package pecl/ssh2 can be installed with "pecl install ssh2"
Cannot initialize 'channel://pear.php.net/ssh2', invalid or missing package file
... [more] Package "channel://pear.php.net/ssh2" is not valid
install failed

However, when trying pecl install ssh2, i get, command pecl not found ... ?

#95. Kevin on 17 July 2008

Default avatar:Kevin@ 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.
... [more]
@ Tim: Thank you Tim

#94. Tim on 16 July 2008

Default avatar:TimHey, 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!

#93. Ross on 26 June 2008

Default avatar:RossHi,

I'm trying to install this on a machine running CentOS. However I am hitting this error when trying to run the "make all install" command for libssh2

[code=text]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 <URL:http://bugzilla.redhat.com/bugzilla> for instructions.
make[1]: *** [channel.o] Error 1
make[1]: Leaving directory `/usr/src/libssh2-0.14/src'
make: *** [all] Error 1[/code]

#92. Ho Wai Keong on 26 June 2008

Default avatar:Ho Wai KeongThis 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!

#91. Peter on 25 June 2008

Default avatar:PeterWhen 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?

#90. Shoaibi on 10 June 2008

Default avatar:ShoaibiGreat article indeed...

#89. Kevin on 06 June 2008

Default avatar:Kevin@ UnrealComps: My guess would be that the module has not been loaded. Did you include the
extension=ssh2.so

#88. UnrealComps on 06 June 2008

Default avatar:UnrealCompsI have followed all the steps above but it still says "function ssh2_connect doesn't exist". Any ideas? Thanks

#87. MianoSM on 04 June 2008

Default avatar:MianoSMThank you, I went back and double checked my extensions and had mistakenly commented out my own additions. Thank you for your rapid response. : )

#86. Kevin on 03 June 2008

Default avatar:Kevin@ MianoSM: My guess would be that the module has not been loaded. Did you include the
extension=ssh2.so

#85. MianoSM on 03 June 2008

Default avatar:MianoSMI'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:
... [more] passwordauthentication yes

Now, when I use the block for Method 1, I'm seeing:

"function ssh2_connect doesn't exist"

Any pointers on this would be greatly appreciated. Thank you.

#84. Kevin on 31 May 2008

Default avatar:Kevin@ nunu: Seem's to me you can't connect. Can you connect to that same IP using regular console SSH?

#83. nunu on 27 May 2008

Default avatar:nunuhei, 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
... [more]
can you tell me what's wrong?

i'm using :

libssh2 version 0.12
banner SSH-2.0-libssh2_0.12

Thankyou

#82. NJ on 09 May 2008

Default avatar:NJThanks a ton!! I was trying to resolve this problem from a long time. It really helped.

#81. cartmanf on 04 May 2008

Default avatar:cartmanfThank you kindly for the extensive tutorial. I found it extremely useful.

Hope you post some more advanced tutorials soon.

Regards,
... [more] Reinhard

#80. Wes on 24 April 2008

Default avatar:WesI 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

#79. Kevin on 23 April 2008

Default avatar:Kevin@ Wes: Can't reproduce easily, but 2 tips:
- concatenate the var
- when sudoing it is probably better use a shell instead of exec

#78. Wes on 22 April 2008

Default avatar:WesCorrection

if(!($stream = ssh2_exec($con, "ls /home/user/$v1" )) )

#77. Wes on 22 April 2008

Default avatar:WesThanks Kevin, I found the easiest way is to enable sudo user from running commands without being asked for a password

%admin ALL=NOPASSWD: ALL


But after all that i cant pass variables to the shell, for example:

if(!($stream = ssh2_exec($con, "/home/user/ls /home/user/$v1" )) )

#76. Kevin on 22 April 2008

Default avatar:Kevin@ shinta: No problem, glad it works!

#75. shinta on 22 April 2008

Default avatar:shintaoh 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

... [more] thank u kevin for your article
GBU

#74. Kevin on 22 April 2008

Default avatar:Kevin@ 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...

#73. shinta on 22 April 2008

Default avatar:shintahi kevin i'have something problem with
command:

--------------------------------------------------------
pecl install -f ssh2
... [more] --------------------------------------------------------

and i got :

--------------------------------------------------------
No releases available for package "pecl.php.net/ssh2"
Cannot initialize 'ssh2', invalid or missing package file
Package "ssh2" is not valid
install failed
--------------------------------------------------------


whether i do connect to internet? to execute "pecl install -f ssh2"?


and i download some pakcage from http://pecl.php.net/package/ssh2

and i try to execute

--------------------------------------------------------
pecl install -f /home/user/ssh2-0.10.tgz
--------------------------------------------------------

and it's running some script / something compile,but i got some error

--------------------------------------------------------
make: *** [ssh2.lo] Error 1
ERROR: `make' failed
--------------------------------------------------------

what's wrong in my command?
i use ubuntu feisty?

#72. few bugs when setting up ssh2 on 18 April 2008

Default avatar:few bugs when setting up ssh2working link - http://programming.has.no.com/2008/04/18/libssh2-and-php/

#71. few bugs when setting up ssh2 on 18 April 2008

Default avatar:few bugs when setting up ssh2I 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 <a href="http://programming.has.no.com/2008/04/18/libssh2-and-php/">http://programming.has.no.com/2008/04/18/libssh2-and-php/</a>

#70. Kevin on 17 April 2008

Default avatar:Kevin@ Wes: It might work if you open a shell instead of using the 'exec' method. One way to find out :) Let me know alright?

#69. Sebastian on 17 April 2008

Default avatar:SebastianHey Wes,
if you have root access on that box, configure sudo and put that in your script (e.g. sudu rcapache2 reload).
rgds,

#68. Wes on 17 April 2008

Default avatar:WesThanks 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?

#67. Sebastian on 09 April 2008

Default avatar:SebastianKevin,
does "I see" mean you were able to reproduce the behavior?

Unfortunatly, your idea doesn't work on my system.
The echo command will not be displayed on the webpage. But the first part can be seen in the local file.
... [more]
What type of connection is opend to transfer the data back?
Can I presume, when a regular ssh connection from console works, that the ssh2_exec() should work too?
Any ideas? The server to reach is in the same network, so there shouldn't be any routing/firewalling problem.
rgds,

#66. Kevin on 09 April 2008

Default avatar:Kevin@ Sebastian: I see. If you change the command like this it will probably work:

ssh2_exec($con, 'ls -al >> /tmp/file.txt; echo "--command finished--"');

#65. Sebastian on 09 April 2008

Default avatar:SebastianAhhh,
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 >> /tmp/file.txt; echo "--command finished--" >> /tmp/file.txt');

works as expected. The stout was directed into the file, and can be read by loggin in on console.
hrrghh.

#64. Kevin on 08 April 2008

Default avatar:Kevin@ Sebastian: Yeah I meant the tip of running an echo '__COMMAND_FINISHED__', setting blocking to false, etc. The one from the header 'tips'.

#63. Sebastian on 08 April 2008

Default avatar:SebastianHey 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?
... [more] You mean about valitading the results?
How shall I test?
We can switch over to email conversation, not to "spam" your commments. Drop a mail to s(dot)waitz(at)rtsgroup(dot)net.
Thanks a lot.

#62. Kevin on 08 April 2008

Default avatar:Kevin@ 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'?

#61. Sebastian on 08 April 2008

Default avatar:SebastianThat 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:

<?php
error_reporting( E_ALL );
$con = ssh2_connect("cme1", 22);
ssh2_auth_password($con, "rts", "thesecret");
// execute a command
$stream = ssh2_exec($con, "touch /tmp/file.txt; echo \"My Test\" >> /tmp/file.txt; sleep 5; cat /tmp/file.txt");
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
fclose($stream);
?>

#60. Kevin on 08 April 2008

Default avatar:KevinThe 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?

#59. Sebastian on 08 April 2008

Default avatar:SebastianNo 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:
[code]
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)
[/code]
Any ideas?
Thanks

#58. Kevin on 08 April 2008

Default avatar:Kevin@ 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?

<?php
$old_level = error_reporting( E_ALL );
?>

Does this tell you anything?

#57. Sebastian on 07 April 2008

Default avatar:SebastianHey 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.
... [more] On echo'ing $stream, I get "Resource id#3" back.
Any ideas?
Thanks a bunch

#56. Kevin on 08 March 2008

Default avatar:Kevin@ 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.

#55. XSchne2036 on 07 March 2008

Default avatar:XSchne2036hi,

I followed your instructions
and ...

... [more]
zirkonia:/var/www/apache2-default/libssh2-0.18/src/.libs# pecl install -f ssh2 WARNING: failed to download pecl.php.net/ssh2 within preferred state "stable", will instead download version 0.10, stability "beta"
downloading ssh2-0.10.tgz ...
Starting to download ssh2-0.10.tgz (22,187 bytes)
........done: 22,187 bytes
5 source files, building
running: phpize
sh: phpize: command not found
ERROR: `phpize' failed


apt-cache search phpize has no results too...

please... what am I doing wrong ?

#54. youme on 26 February 2008

Default avatar:youmei 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

#53. Emanuel on 06 February 2008

Default avatar:EmanuelVery 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

#52. Kevin on 05 February 2008

Default avatar:Kevin@ Emanuel: My bad, the example code was missing a: ';'

so:

die("function ssh2_connect doesn't exist")


needed to be:
die("function ssh2_connect doesn't exist");


I've updated the article accordingly. btw, you were using the CODE tags incorrectly but I've editted your comment for better readability.

#51. Emanuel on 05 February 2008

Default avatar:EmanuelHi, 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("ssh2_connect")) die("function ssh2_connect doesn't exist")
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("server1.example.com", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "secretpassword")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";

// execute a command
if(!($stream = ssh2_exec($con, "ls -al" )) ){
echo "fail: unable to execute command\n";
} else{
// collect returning data from command
stream_set_blocking( $stream, true );
$data = "";
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
fclose($stream);
}
}
"]

e in log of the apache returns me the following error:
[CODE="[ error ] PHP error Parse: syntax error, unexpected T_IF in /var/www/htdocs/a.php on line 4[/CODE]
wanted to know if it would have as to help to decide this problem me

#50. Kevin on 19 January 2008

Default avatar:Kevin@ 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.

#49. Pimmy on 18 January 2008

Default avatar:PimmyI 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?
... [more]
Thanks,

Pimmy

#48. Kevin on 17 January 2008

Default avatar:Kevin@ 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'.

#47. Emanuel on 17 January 2008

Default avatar:Emanuelyes, 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.

#46. Kevin on 16 January 2008

Default avatar:KevinThe 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?

#45. Emanuel on 15 January 2008

Default avatar:Emanuel1º - 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”
... [more] GCC - channel.o channel.c - c - g - O2 /usr/include - I/usr/include - Wall - I. /include/ - fPIC
GCC: /usr/include: to linker input file unused because linking not done
channel.c: 1253: 10: /usr/include: Archive or directory not found
channel.c: 71: internal to compiler error: Imperfection of Please
segmentation submit full bug report,
with preprocessed source if appropriate.
See <URL: http://gcc.gnu.org/bugs.html > will be instructions.
make [1]: ** [channel.o] Error 1
make [1]: Leaving the directory “/temp/libssh2-0.14/src”
make: ** [all] Error 1

2º - As it did not give certain in 1a, I tried using the version libssh2-0.18 that it compiled normally, to put I was to look lib ssh2.so and I did not find, what I found was libssh2.so, from there I configured php.ini with the following parameter:
extension=libssh2.so
I restarted the apache and in log of error vi the following one:
PHP Warning: Unknown (): Invalid library (maybe not the PHP library) “libssh2.so” in Unknown on line 0.

Debtor for helps me!

#44. Kevin on 15 January 2008

Default avatar:Kevin@ 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!

#43. Emanuel on 15 January 2008

Default avatar:Emanuelhi, 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.

#42. Kevin on 16 December 2007

Default avatar:Kevin@ Pimmy: Thank you for sharing!

#41. Pimmy on 14 December 2007

Default avatar:PimmyI 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>phpize
... [more] root@tangra:ssh2-0.10>./configure \
> --with-php-config=/usr/local/php/bin/php-config \
> --with-ssh2=/usr/local
root@tangra:ssh2-0.10>make clean
root@tangra:ssh2-0.10>make

Then everything went smooth.

Cheers.

#40. Pimmy on 14 December 2007

Default avatar:PimmyNow 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
... [more] --------------------------------------------------------

I've recompiled both libssh2-0.14 and ssh2-0.10 several times in different ways but no joy.

Have you got any suggestions?

#39. Hiren Thakkar on 13 December 2007

Default avatar:Hiren ThakkarThanks .. This may be helpful to me.

#38. Pimmy on 13 December 2007

Default avatar:PimmyI got stuck from the beginning compiling libssh2..

----------------------------------------------------
root@tangra:libssh2-0.14>make all install
make[1]: Entering directory `/space/installs/libssh2-0.14/src'
... [more] gcc -o channel.o channel.c -c -g -O2 /usr/include -I/usr/local/include -Wall -I../include/ -fPIC
gcc: cannot specify -o with -c or -S and multiple compilations
make[1]: *** [channel.o] Error 1
make[1]: Leaving directory `/space/installs/libssh2-0.14/src'
make: *** [all] Error 1
root@tangra:libssh2-0.14>gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-59)
----------------------------------------------------

This seems to be a 'minor' bug in version 0.14 which can be fixed by removing the '-o' option from the Makefile.in file (line 23).

It might be fixed in later versions of libssh2, but as you notice there are other bugs in the newer versions.

I would really like to know if anyone managed to successfully use the patest versions of libssh2.

Thanks for the tutorial

#37. Kevin on 09 December 2007

Default avatar:Kevin@ 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 & libssh2, and it seems to me you've totally skipped the libssh2 part. I wonder if this is even available on windows?

#36. Nigel on 09 December 2007

Default avatar:NigelHi 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
... [more]
then restarted apache (and the server to be sure on the next attempts!)
...Code 500! aaaarrrggghhhh

Log file below, any ideas please??
have installed openssl.exe and made sure in path. php.ini file must be ok as it calls in the errors below but then errors. Anyone got any advise please!!! Stupidly told company that I can get this done next week :(

[Mon Dec 03 20:07:47 2007] [warn] pid file C:/Program Files/Apache Software Foundation/Apache2.2/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
PHP Warning: PHP Startup: Unable to load dynamic library 'c:/php5/exts/php_ssh2.dll' - The operating system cannot run %1.\r\n in Unknown on line 0
[Mon Dec 03 20:07:49 2007] [notice] Apache/2.2.4 (Win32) PHP/5.2.4 configured -- resuming normal operations
[Mon Dec 03 20:07:49 2007] [notice] Server built: Jan 9 2007 23:17:20
[Mon Dec 03 20:07:49 2007] [notice] Parent: Created child process 1072
PHP Warning: PHP Startup: Unable to load dynamic library 'c:/php5/exts/php_ssh2.dll' - The operating system cannot run %1.\r\n in Unknown on line 0
[Mon Dec 03 20:07:49 2007] [notice] Child 1072: Child process is running
[Mon Dec 03 20:07:49 2007] [notice] Child 1072: Acquired the start mutex.
[Mon Dec 03 20:07:49 2007] [notice] Child 1072: Starting 250 worker threads.
[Mon Dec 03 20:07:49 2007] [notice] Child 1072: Starting thread to listen on port 80.
[Mon Dec 03 20:11:09 2007] [error] [client 172.30.1.78] PHP Fatal error: Call to undefined function ssh2_connect() in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\ssh.php on line 3
[Mon Dec 03 20:19:12 2007] [error] [client 172.30.1.78] PHP Fatal error: Call to undefined function ssh2_connect() in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\ssh.php on line 3

#35. Kevin on 07 November 2007

Default avatar:KevinThanks for sharing that information Bryan! I hadn't tried 0.17 yet, seems more people are running into problems with it.

#34. Bryan on 06 November 2007

Default avatar:BryanThank 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 "make 1" error from PECL. When I installed 0.14, everything became peachy.

#33. Kevin on 31 October 2007

Default avatar:Kevin@ 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

#32. vezzo on 31 October 2007

Default avatar:vezzohey, kevin, why i can execute a lot of command but, I can't change directory with "cd Desktop" for example????

#31. Kevin on 25 October 2007

Default avatar:Kevin@ 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?

#30. JavieL on 23 October 2007

Default avatar:JavieLSome example for view?¿

#29. Patrick on 23 October 2007

Default avatar:PatrickOk, 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:
... [more] http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/

However, i failed to sucessfully get libssh2-0.14 working.
It gave an error on the command './configure' at about 90%.
Also the command 'make all install' failed at about 40%.

Next i tried to get libssh2-0.17 working.
I sucessfully ran './configure' and 'make all install'.

Proceeding i executed: pecl install -f ssh2
This seemed to work, but failed at...

running: make
/bin/sh /tmp/pear-build-root/ssh2-0.10/libtool --mode=compile gcc -I. -I/tmp/pear/download/ssh2-0.10 -DPHP_ATOM_INC -I/tmp/pear-build-root/ssh2-0.10/include -I/tmp/pear-build-root/ssh2-0.10/main -I/tmp/pear/download/ssh2-0.10 -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/download/ssh2-0.10/ssh2.c -o ssh2.lo
gcc -I. -I/tmp/pear/download/ssh2-0.10 -DPHP_ATOM_INC -I/tmp/pear-build-root/ssh2-0.10/include -I/tmp/pear-build-root/ssh2-0.10/main -I/tmp/pear/download/ssh2-0.10 -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/download/ssh2-0.10/ssh2.c -fPIC -DPIC -o ssh2.lo
/tmp/pear/download/ssh2-0.10/ssh2.c: In function `zif_ssh2_methods_negotiated':
/tmp/pear/download/ssh2-0.10/ssh2.c:481: warning: passing arg 2 of `libssh2_session_methods' makes integer from pointer without a cast
/tmp/pear/download/ssh2-0.10/ssh2.c:481: error: too many arguments to function `libssh2_session_methods'
/tmp/pear/download/ssh2-0.10/ssh2.c: In function `zif_ssh2_fingerprint':
/tmp/pear/download/ssh2-0.10/ssh2.c:536: warning: assignment discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c: In function `zif_ssh2_publickey_add':
/tmp/pear/download/ssh2-0.10/ssh2.c:1038: warning: passing arg 1 of `_efree' discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c: In function `zif_ssh2_publickey_list':
/tmp/pear/download/ssh2-0.10/ssh2.c:1097: warning: passing arg 4 of `add_assoc_stringl_ex' discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c:1098: warning: passing arg 4 of `add_assoc_stringl_ex' discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c:1106: warning: initialization discards qualifiers from pointer target type
/tmp/pear/download/ssh2-0.10/ssh2.c:1107: warning: passing arg 2 of `zend_hash_add_or_update' discards qualifiers from pointer target type
make: *** [ssh2.lo] Error 1
ERROR: `make' failed

Here i have ceased to continue installing SSH2 for PHP.
I am unsure if the system has been badly effected by this partial installation.

#28. Kevin on 12 October 2007

Default avatar:Kevin@ 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.

#27. prasanna on 11 October 2007

Default avatar:prasannai created the ssh2.so using
phpize && ./configure --with-ssh2 && make.
I run the above command under PECL/SSH directory.

But i have a doubt and i explained it below.
... [more]
Hi,

I am trying to add SSH2 support in my product. I am using Apache 2.0.4.7 with PHP 5.2.4.

For SSH2 support, i had compiled PECL/SSH2 package and created ssh2.so.

I used to run PHP using libphp5.so through httpd.conf. I struck at the creation of libphp5.so file.

<LoadModule php5_module /home/nprasanna/test/modules/libphp5.so>

I downloaded the latest PHP 5.2.4 and install it and got the libphp5.so, but i dont know how to include the ssh2.so while creating libphp5.so.

Please let me know, whether it is possible to create libphp5.so with ssh2.so support and make it work using Apache.

Also i tried by compiling PHP with a custom_php.ini file, (--with-config-file-path). In that custom_php.ini, i specified the ssh2.so as extension=ssh2.so, with the correct path of extension_dir value, failed that case too.

Please guide me how to use shh2.so in libphp5.so. whether we need to specify this at the creation of libphp5.so or we can make this possible by custom php.ini file.

Please suggest me a solution for this.
My email id: prasankn@gmail.com

Thanks,
prasanna

#26. Kevin on 10 October 2007

Default avatar:Kevin@ prasanna:

I believe you've typed:
pecl install ssh

... [more] instead of:
pecl install ssh2

#25. Kevin on 10 October 2007

Default avatar:Kevin@ 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 :)

#24. prasanna on 10 October 2007

Default avatar:prasannaI got the error

I had installed pecl, openssl.

I found no useful results while googling with the below error.
... [more]
No releases available for package "pecl.php.net/ssh"
Cannot initialize 'pecl/ssh', invalid or missing package file
Package "Array" is not valid
install failed

How to resolve it, please help me.
prasanna

#23. Edi on 10 October 2007

Default avatar:EdiOk, 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.
... [more] maybe this is wrong method , but if there is some way to hold the ssh2 connection (session) each time the page is reloaded, without connecting to remote host again, then it should work well.

thanks in advance for any suggestions.

#22. Kevin on 09 October 2007

Default avatar:Kevin@ Edi: Sure, after you've used ssh2_connect & ssh2_auth_password you can use as many ssh2_execs as you want.

#21. Edi on 09 October 2007

Default avatar:EdiVery 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?

#20. Stefan Ideler on 05 October 2007

Default avatar:Stefan IdelerFor 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/
... [more]
Download the tar, patch the 2 files with the diff files, then install it using phpize && ./configure --with-ssh2 && make && make install

On another note, if you have PEAR installed but not the pecl package, try an pear upgrade-all .
Appearently a number of older versions of PEAR do not include the pecl package.

Regards,

stefan at i3d dot nl

#19. Kevin on 04 October 2007

Default avatar:Kevin@ 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 && locate php.ini |egrep '^/etc/.*\.ini$'

... [more] To find all the valid php.ini's on your system. If there isn't a cli version (either 4 or 5) maybe you will have to reinstall:
aptitude reinstall php5-cli
or
aptitude reinstall php4-cli

depending on the version you prefer.

#18. Joo on 04 October 2007

Default avatar:JooThank 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.

#17. Kevin on 04 October 2007

Default avatar:Kevin@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

#16. Joo on 04 October 2007

Default avatar:JooI 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[

... [more] What happened in my php?

Thanks in advance.

#15. Oriol on 23 September 2007

Default avatar:OriolI'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.

#14. ServerChief on 13 September 2007

Default avatar:ServerChiefHi,

Just wanted to say thank you for this tutorial. I'm writting an application for VmWare ESX Updater Services, this tutorial, without a doubt will help me do it more efficiently.

Thanks,
... [more]
ServerChief

http://www.serverchief.com

#13. Jason on 11 September 2007

Default avatar:JasonHi 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);
... [more] ssh2_auth_password($connection, $SSH_LOGIN, $SSH_PASSWORD);
$tunnel = ssh2_tunnel($connection, $REMOTE_HOST.":".$REMOTE_PORT, $LOCAL_PORT);

Could you please help me?

#12. Kevin on 30 August 2007

Default avatar:Kevin@ 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 "!BREAK!"; cat /proc/cpuinfo; echo "!BREAK!"; cat /proc/loadavg; echo "!BREAK!"; df -h

... [more] The shell on the other side will just run every command and give one big output.

Then just parse the output in PHP by exploding on "!BREAK!" (see 'explode' function in PHP manual)

#11. trume on 30 August 2007

Default avatar:trumehi,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.
... [more]
thanks.

#10. Kevin on 28 August 2007

Default avatar:Kevin@ 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
... [more]
Can it be that you ran into this bug?
http://pecl.php.net/bugs/bug.php?id=11779

#9. jan on 27 August 2007

Default avatar:jani 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'
... [more] /tmp/pear/cache/ssh2-0.10/ssh2.c: In function 'zif_ssh2_fingerprint':
/tmp/pear/cache/ssh2-0.10/ssh2.c:536: warning: assignment discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c: In function 'zif_ssh2_publickey_add':
/tmp/pear/cache/ssh2-0.10/ssh2.c:1038: warning: passing argument 1 of '_efree' discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c: In function 'zif_ssh2_publickey_list':
/tmp/pear/cache/ssh2-0.10/ssh2.c:1097: warning: passing argument 4 of 'add_assoc_stringl_ex' discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c:1098: warning: passing argument 4 of 'add_assoc_stringl_ex' discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c:1106: warning: initialization discards qualifiers from pointer target type
/tmp/pear/cache/ssh2-0.10/ssh2.c:1107: warning: passing argument 2 of '_zend_hash_add_or_update' discards qualifiers from pointer target type
make: *** [ssh2.lo] Error 1


makes me sad :(

#8. Peter on 14 August 2007

Default avatar:PeterNever mind, my mistake. I forgot to run 'make all install'. Duh!

#7. Peter on 14 August 2007

Default avatar:PeterGreat 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/
... [more]
Any suggestions?

#6. tr on 13 August 2007

Default avatar:trInstead of 'pear install -f ssh2' try with 'pecl install -f ssh2'.

#5. Kevin on 10 August 2007

Default avatar:KevinDid you type '-f' ?

#4. Will on 09 August 2007

Default avatar:WillFirst 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 "pear.php.net/ssh2" - package pecl/ssh2 can be installed with "pecl install ssh2"
Cannot initialize 'ssh2', invalid or missing package file
Package "ssh2" is not valid
... [more] install failed


Any help would be great. Thanks...

#3. NiKo on 30 July 2007

Default avatar:NiKoGreat article, thanks.

#2. Kevin on 28 July 2007

Default avatar:KevinHi 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!

#1. Sara Golemon on 27 July 2007

Default avatar:Sara GolemonGlad 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.
... [more]
Part of the reason for the refactorings is that some of the cURL developers have gotten involved in its development and wanted to make it better before building support for it into cURL (which...by the way, cURL now supports SFTP via libssh2).

SCP doesn't work entirely right. I recommend SFTP to anyone who has it available. It's a much more robust protocol and libssh2's implementation of it works much more solidly than it's implementation of SCP. (If ANYONE has a spec document describing SCP or RCP, I'd love to get a link...)

As to docs... Yeah... they could definately stand to be better... You're welcome to contribute directly to the PHP manual. CVS repository can be browsed at http://cvs.php.net/phpdoc/en/reference/ssh2 , anonymous CVS access instructions are at http://php.net/anoncvs.php, and instructions for getting your own cvs account can be found at http://php.net/cvs-php.php . You can also, of course, just send diffs (unified) to the phpdoc mailing list and ask someone politely to commit it for you.