2

I try to run SSH connection in my application and I get this error:

Call to undefined function App\Http\Controllers\Admin\ssh2_connect()

Controller

public function save(Request $request){
        $ip = $request->input('server_ip');
        $sshUser= $request->input('server_ssh_user');
        $sshPw = $request->input('server_ssh_password');

        $sshConnection = ssh2_connect($ip, 22);
        //rest of the code which doesn't execute as result of line above returns error

Any idea?

mafortis
  • 6,750
  • 23
  • 130
  • 288
  • 1
    The `ssh2` functions are an optional extension. Have you installed it? https://www.php.net/manual/en/ssh2.installation.php – ceejayoz Apr 02 '19 at 03:10
  • (Also, consider something like [phpseclib](https://github.com/phpseclib/phpseclib), for which there's a [Laravel package](https://github.com/LaravelCollective/docs/blob/5.6/ssh.md#installation), or [Laravel Envoy](https://laravel.com/docs/5.8/envoy).) – ceejayoz Apr 02 '19 at 03:12
  • @ceejayoz no ididn't install that. should i install it on my system or add it to my application with composer? – mafortis Apr 02 '19 at 03:14
  • @ceejayoz I already installed https://github.com/LaravelCollective/docs/blob/5.6/ssh.md#installation – mafortis Apr 02 '19 at 03:16
  • 1
    hope this will help you [Link](https://stackoverflow.com/questions/14050231/php-function-ssh2-connect-is-not-working/43624763#43624763) – Udhav Sarvaiya Apr 02 '19 at 04:07
  • @UdhavSarvaiya that did solve my issue actually thanks. `quick question` apperantly `exec()` only can run 1 command at the time how can I run multiple commands? (based on linked answer) – mafortis Apr 02 '19 at 04:38
  • this may help you https://stackoverflow.com/questions/15134421/php-install-ssh2-on-windows-machine – ManojKiran A Apr 02 '19 at 04:59

1 Answers1

2

You would need to install the following packages on the server.

Ubuntu/Debian:

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

Redhat/Centos/Amazon-Linux:

sudo yum install php-ssh2 
sudo service httpd restart (Centos/Redhat/AmazonLinux)
louisfischer
  • 1,968
  • 2
  • 20
  • 38