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 = "";
$port = 22;
$conn = ssh2_connect($host);
$public_key_realpath = '/.ssh/';
$private_key_realpath = '/.ssh/yourkey';
$username = 'ssh-username';
$key_password = '123456';
if ($conn) {

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

Hey! Don't miss a new cool stuff!


azithromycin fo... (not verified)

Fast Delivery On Doxycyclene

covid 19 plaquenil (not verified)

Viagra Mit Rezept Online

azithromycin fo... (not verified)

cialis las palmas

is plaquenil an... (not verified)

Is Cephalexin Safe For Pregnant Dogs

buying cialis o... (not verified)

mejor cialis o viagra

cialis online n... (not verified)

Fedex Hydrochlorothiazide C.O.D.

brand name cial... (not verified)

Amoxil Online Pharmacy

where to buy ci... (not verified)

Levitra Zulassung

cialis 20mg for sale (not verified)

Cialis And Bactrim

gabapentin pres... (not verified)

Lambert Fish Amoxicillin

where can i buy... (not verified)

Cialis Frei Erhaltlich

buy cialis usa (not verified)


what is gabapen... (not verified)

Cialis Recreatif

buy viagra in c... (not verified)

Lowest Price For Amitriptyline

pfizer viagra coupon (not verified)

Can You Drink On Amoxicillin

viagra shelf life (not verified)

Viagra Professional

buy generic pro... (not verified)

Amoxicillin Dose

buy finasteride... (not verified)

annonce levitra en ligne

buy fincar 5mg cheap (not verified)

Viagra Kaufen In Osterreich

propecia vs rogaine (not verified)

Levitra Prix En Officine

finasteride hairline (not verified)

Cialis Dosaggio