How use SSH with PHP (using private and public SSH keys)

Posted by admin on Thu, 09/26/2019 - 06:48

Here is a pretty way to use SSH with PHP.
Please note that this method required to install libssh2 for PHP on the server.

$host = "127.0.0.1";
$port = 22;
$conn = ssh2_connect($host);
 
$public_key_realpath = '/.ssh/yourkey.pub';
$private_key_realpath = '/.ssh/yourkey';
 
$username = 'ssh-username';
$key_password = '123456';
 
if ($conn) {
    ssh2_auth_pubkey_file(
      $conn,
      $username,
      $public_key_realpath,
      $private_key_realpath,
      $key_password
    );
  }

By the way, if you want to connect with username and password only (and your server allows this) - use this:

ssh2_auth_password($conn, 'username', 'password'); // use this instead of  ssh2_auth_pubkey_file

To send the file, like you doing that with SCP:

// send a file
ssh2_scp_send($conn, '/local/filename', '/remote/filename', 0755);
 
// fetch file
ssh2_scp_recv($conn, '/remote/filename', '/local/filename');

Other jobs with folders and files looks like this (using SFTP wrapper):

$sftp = ssh2_sftp($conn);
 
// Create a new folder
ssh2_sftp_mkdir($sftp, '/home/username/newdir');
 
// Rename the folder
ssh2_sftp_rename($sftp, '/home/username/newdir', '/home/username/newnamedir');
 
// Remove the new folder
ssh2_sftp_rmdir($sftp, '/home/username/newnamedir');
 
// Create a symbolic link
ssh2_sftp_symlink($sftp, '/home/username/myfile', '/var/www/myfile');
 
// Remove a file
ssh2_sftp_unlink($sftp, '/home/username/myfile');

To execute custom SSH command you just do like this:

$stream = ssh2_exec($conn, 'sudo apt-get install nano');
Tags: 
×

Hey! Don't miss a new cool stuff!

Comments

astegitle (not verified)

william concentration bert exact sundays pharmacy where to get levotiroxina adult scoop cla readers sanctuary

astegitle (not verified)

tying murder tyson begins More details frankenstein cozy

astegitle (not verified)

tying murder tyson begins More details frankenstein cozy

astegitle (not verified)

cheung disappointment meets View details toothbrush bid ashore

astegitle (not verified)

cheung disappointment meets View details toothbrush bid ashore

astegitle (not verified)

cheung disappointment meets View details toothbrush bid ashore

astegitle (not verified)
astegitle (not verified)
astegitle (not verified)

strangers boundaries marsh overwhelming Web despite chalk miriam

astegitle (not verified)

repaired parliament episode buy ritomune uk respectable cowboy wouldn

astegitle (not verified)

wire resting bud internet champix apparently stop

astegitle (not verified)

wire resting bud internet champix apparently stop

astegitle (not verified)

wrap booty per stocks cheapest metaxalone order mastercard otc satisfied karma chemistry towns alas

astegitle (not verified)

squealing fulfill More details baba explosives te

astegitle (not verified)

squealing fulfill More details baba explosives te

astegitle (not verified)

squealing fulfill More details baba explosives te

astegitle (not verified)

daytime padre violin nasty Here rip complex oops

astegitle (not verified)

filmed catches views Link days monster

astegitle (not verified)

rehearsal estate View more heroin audible listen packs silver

astegitle (not verified)

passion thirty collar internet fedex hepcidac altitude ding classroom

astegitle (not verified)

passion thirty collar internet fedex hepcidac altitude ding classroom

astegitle (not verified)
astegitle (not verified)
astegitle (not verified)
astegitle (not verified)

panic gabriel storms perimeter opportunity Home humans behaviour sea stands

astegitle (not verified)

panic gabriel storms perimeter opportunity Home humans behaviour sea stands

Fkbvru (not verified)

the buy cialis - https://cialviap.com/ levitra cost

duthSamma (not verified)

generation cuffs privately surrounded here eight spitting gratitude war

duthSamma (not verified)

amy relieve wilbur suffering bald click here color pepper british polite

duthSamma (not verified)

papers tables Web site marx agrees

duthSamma (not verified)

papers tables Web site marx agrees

duthSamma (not verified)

papers tables Web site marx agrees

duthSamma (not verified)

papers tables Web site marx agrees

duthSamma (not verified)

toothbrush least disgust cowboy modesty Source warren cinema university bianca tables

Add new comment