Contact Us
 
 
 
 
  Browse by category :
 

Bob and the Knowledge Factory / Linux / Secure SHell / How to Install SSH2 for PHP shell connections

How to Install SSH2 for PHP shell connections

Add comment
Views: 2172
Votes: 0
Comments: 0
Posted: 09 Sep, 2009
by: Vinisha G.
Updated: 02 Oct, 2009
by: Vinisha G.

We need a few modules installed on the server if we want to open SSH connections from within a PHP script.

The main 3 packages needed for the installation are:

a ) OpenSSL
b) Libssh2
c) ssh2 modules

and of course we need root access to the server.

OpenSSL will be installed in many of the servers. To confirm if openssl is installed or not, please execute the following command in shell.

[root@server ~]#rpm -qa |grep openss
If the package is installed, we will get an output like this :
openssl096b-0.9.6b-22.46
openssl-0.9.7a-43.17.el4_6.1
openssl-devel-0.9.7a-43.17.el4_6.1

The versions may vary depending on the OS in the server.

If you did not get any output, you need to install OpenSSL. We can easily install openssl using yum or apt-get(redhat or debian based servers):

yum install openssl (redhat based OS)

apt-get install openssl (debian based OS)

The second package which is needed in the server is libssh2. The installation steps are given below:

wget http://voxel.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.18.tar.gz
tar -zxvf libssh2-0.18.tar.gz
cd libssh2-0.18
./configure
make
make install

ext we need to install ssh2 php module. The installation steps are given below:

 wget http://pecl.php.net/get/ssh2-0.10.tgz
tar -xzf ssh2-0.10.tgz
cd ssh2-0.10
phpize && ./configure –with-ssh2 && make

Now you need to copy ssh2.so module to the php extensions directory which you can find out where it is by checking the php.ini file. To find the location of the ssh2.so module. You can do it by the command:

locate ssh2.so

If in php.ini you got the php extensions directory as "/usr/local/lib/php/extensions/no-debug-non-zts-20060613", then do the following steps.

cp -p ssh2.so /usr/local/lib/php/extensions/no-debug-non-zts-20060613/.

At the end just restart apache and you are done, you can try the working of ssh2 by a script. A test script is given below for your reference:
<?php
$connection = ssh2_connect('abc.com', 22);
ssh2_auth_password($connection, 'abc', 'abc123');

if(ssh2_sftp($connection))
{
echo "connect";
}else
{
echo "not connected";
}
?>

If after running phpize && ./configure --with-ssh2 && make you end up with an error like make: *** [ssh2.lo] Error 1 you will have to do a small hack.

[root@server ssh2-0.10]# vi ssh2.c
Search for the line:
#if LIBSSH2_APINO < 200412301450
and edit it to look like:
#if LIBSSH2_VERSION_NUM < 0x001000

Save the file and run phpize && ./configure --with-ssh2 && make again.

If everything finishes fine you are done.

Others in this Category
document SSHfs: mounting remote filesystem over ssh?
document RSA key pair generation
document Using iptables to rate limit incoming connections
document Disable direct root login via ssh
document Upgrading Openssh on CentOS And Chrooting a User When Connecting via SFTP



RSS
Content is available under Creative Commons Attribution-Share Alike 3.0 License.