Alireza Fallah
Alireza Fallah

Reputation: 4607

SSH Connection to a remote server using PHP

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 ?

Upvotes: 2

Views: 28053

Answers (3)

RNK
RNK

Reputation: 5792

You can configure your server (Ubuntu):

sudo apt-get install libssh2-php
sudo service apache2 restart

Upvotes: 8

Alireza Fallah
Alireza Fallah

Reputation: 4607

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');

Upvotes: 6

Tobias
Tobias

Reputation: 1682

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

Upvotes: 3

Related Questions