How To connect SSH using PHP?


In this tutorial we will learn how to connect SSH using PHP. Follow below steps to get your answer about the question which is in your mind about PHP execute SSH command on remote server .


Here I will take a very easy and simple example to connect with SSH remove server and run command using PHP.

Lets get your answer by using below example code.


index.php

<?php

    $host = '111.111.111.111';

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

    print_r($output);

  ?> 


Output


Related Topics