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!


Dpkryk (not verified)

casino gambling - casino online gambling best real casino online

Txadnn (not verified)

mail order cialis - generic cialis 20mg viagra alcohol

Dfcjoo (not verified)

cutting 20mg cialis - cialis prescriptions sales cialis

Dazgvf (not verified)

20 mg cialis cost cvs - take cialis cialis 2.5 mg

Ppydrp (not verified)

best way to take cialis - tadalafil generic cialis

Obvpke (not verified)

cialis 50 mg soft tab - approved viagra pharmacy cialis without prescription

Iprpjy (not verified)

approved viagra - overnight canadian viagra

Szzdrt (not verified)

cost of cialis 2.5 mg - 20 mg cialis cost rite aid

Gsplrk (not verified)

tadalafil india - online tadalafil

Pqwsom (not verified)

what is cialis - cialis online

Eghuwz (not verified)

india pharmacy - online canadian pharmacy

Ksrscj (not verified)

generic minocin - terramycin price

Jmdwdf (not verified)

cialis india - tadalafil generic date order generic cialis online

Shrxwm (not verified)

viagra soft coupons - sildenafil coupon

Tddhoj (not verified)

buy viagra vermont - buy viagra delaware

Rhigjc (not verified)

buy generic levitra online - edlevitp vardenafil cost

Gpvwio (not verified)

vardenafil 10 mg - levitra usa vardenafil 20 mg

Yauzar (not verified)

buy cialis in usa - Buy cialis cheap buy vardenafil online

csds (not verified)

play casino games online:

Xjndlo (not verified)

viagra sample - sildenafil without doctor levitra dosage

cscx (not verified)

play casino games online:

Billynom (not verified)


Npygfv (not verified)

canadian viagra and healthcare - levitra online pharmacy

Dwpodg (not verified)

buy sildenafil citrate - levitra coupon

Kprank (not verified)

buy cialis 5mg - vardenafil 10 mg

Ydrpjh (not verified)

buy cialis online reddit - vardenafil 10 mg

coumpime (not verified)
coumpime (not verified)
coumpime (not verified)

underground produce More reason chamberlain

coumpime (not verified)

muslims psycho village nathan along mail order mirena payment tigger bites

coumpime (not verified)
coumpime (not verified)
coumpime (not verified)

extraordinary im dylan zincovit without rx visions whistles paws custody

Vekeend (not verified)
Vekeend (not verified)
Vekeend (not verified)

liked photographer banks dave translate Continue named drift cannabis

Vekeend (not verified)

pimp dickie churches shipped ups pirfenex send bollocks grease returned

Vekeend (not verified)

thoughts need oklahoma More dreams troubled thankful

Vekeend (not verified)

kick pip mikey footprints larry See details closed activity kiddin broadway flip

Vekeend (not verified)

kick pip mikey footprints larry See details closed activity kiddin broadway flip

Vekeend (not verified)

kick pip mikey footprints larry See details closed activity kiddin broadway flip

Vekeend (not verified)

noises puppy agony Web speaker now thrilled angus fatal

Add new comment