How to Connect SSH using PHP?

By Hardik Savani | February 2, 2021 | Category : PHP


In this tutorial we will go over the demonstration of how to connect ssh using php. This article goes in detailed on how to execute ssh command in php. if you have question about php execute ssh command on remote server then i will give simple example with solution. we will help you to give example of php execute remote ssh command. Follow bellow tutorial step of php run ssh command.

I will give you very simple example to connect with ssh remove server and run command using php.

let's see 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:

i hope it can help you...



Tags : PHP


Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: