2

I want to make a SSH connection to a remote server using php.

Im using php 5.3 on Linux/CEntOS.

What I have done so far :

$connection = ssh2_connect('192.168.1.22', 22);
ssh2_auth_password($connection, '_username', '_password');
$stream = ssh2_exec($connection, 'ls -l');

But I'm getting this error :

Fatal error: Call to undefined function ssh2_connect()

So, my questions are :

  1. Are ssh2_* functions not installed by default in php?

  2. Do I need an extension or library for using these functions?

  3. How can I solve this problem ?

Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
  • possible duplicate of [PHP function ssh2\_connect is not working](http://stackoverflow.com/questions/14050231/php-function-ssh2-connect-is-not-working) – John C Sep 14 '13 at 08:25

3 Answers3

8

You can configure your server (Ubuntu):

sudo apt-get install libssh2-php
sudo service apache2 restart
RNK
  • 5,582
  • 11
  • 65
  • 133
6

phpseclib

  • Download it from here or here ( direct link ) ,

  • Include it to the project ,

And then :

   include('Net/SSH2.php');
   $ssh = new Net_SSH2('www.example.com');
   $ssh->login('username', 'password') or die("Login failed");
   echo $ssh->exec('command');
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
  • suppose that you want to remotely execute an 'ls' command and bring the results to PHP....how do yo do that? – FedeKrum Apr 07 '19 at 06:13
3

ssh_* function are not per default installed in php core. You have to add them using pecl, see php manual for a good description: http://de1.php.net/manual/en/ssh2.installation.php

Tobias
  • 1,692
  • 12
  • 21