clock  Mon - Sun 8.00 AM - 8.00 PM
mail  edu@teknowize.com
fb
instagram
play store
pinterest

How To connect SSH using PHP?

  Hariom Prajapati
  23 Jun 2022
  PHP
Imperial Advance Hero Image

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

ssh