How to Connect SSH using PHP?

Share Me
  • 1

Hello Dev’s
Today in this tutorial i will go over the demonstration of how to Connect SSH using PHP. This article will goes to details on how to execute ssh command in php application. If you have any question about php execute ssh command on remote server . Then i will give you a very simple example with  a good solution. Now I will help you to give you a good example of php execute remote ssh command. So Follow bellow tutorial steps for php run ssh command.

Now i will give you a very simple example to connect with ssh remove server and by run command using php.

So let’s see the example code:


    $host = '';
    $port = 22;
    $username = 'root';
    $password = '00cd6bf584e....';
    $connection = ssh2_connect($host, $port);
    ssh2_auth_password($connection, $username, $password);
    $stream = ssh2_exec($connection, 'df -h');
    stream_set_blocking($stream, true);
    $output = stream_get_contents($stream);

Read Also : PHP AngularJS CRUD with Search and Pagination Example From Scratch

Hope it will help you… Also you can follow us on Facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →