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

Estherchera (not verified)

Nynoqy yqixil Buy viagra Buy viagra brand

bisding (not verified)

Where To Buy Tadalis Sx Soft Online https://buyciallisonline.com/ - buy cialis daily online No Rx Cephalexin Buy Cialis Generika Viagra Erfahrungen

bisding (not verified)

Where To Buy Tadalis Sx Soft Online https://buyciallisonline.com/ - buy cialis daily online No Rx Cephalexin Buy Cialis Generika Viagra Erfahrungen

Estherchera (not verified)

Hdezai ijycop canadian pharmacy canada online pharmacy

Estherchera (not verified)

Plkbwy wvaehp best canadian online pharmacy rx pharmacy

Estherchera (not verified)

Qizpqe elqrma real canadian pharmacy rx pharmacy

Estherchera (not verified)

Qizpqe elqrma real canadian pharmacy rx pharmacy

Estherchera (not verified)

Qizpqe elqrma real canadian pharmacy rx pharmacy

Estherchera (not verified)

Mdvtvy erpxgk viagra online Overnight viagra

Estherchera (not verified)

Mdvtvy erpxgk viagra online Overnight viagra

Estherchera (not verified)

Gcrfft xnnked Viagra next day delivery Rx generic viagra

Estherchera (not verified)

Gcrfft xnnked Viagra next day delivery Rx generic viagra

Estherchera (not verified)

Gcrfft xnnked Viagra next day delivery Rx generic viagra

bisding (not verified)

genuine cialis 5mg https://cheapcialisir.com/ - Cialis Zithromax Treatment For Gonorrhea safe place to buy cialis online Medicament Zithromax

bisding (not verified)

genuine cialis 5mg https://cheapcialisir.com/ - Cialis Zithromax Treatment For Gonorrhea safe place to buy cialis online Medicament Zithromax

Estherchera (not verified)

Bdphmu hqnxxs Buy viagra online 50mg viagra

Estherchera (not verified)

Vlseba pvznyu Buy viagra canadian pharmacy online

Estherchera (not verified)

Vlseba pvznyu Buy viagra canadian pharmacy online

Estherchera (not verified)

Mublwv yjovrb generic cialis online pharmacy online

Estherchera (not verified)

Mublwv yjovrb generic cialis online pharmacy online

Estherchera (not verified)

Cueelh fltoay Us cialis sales canadian pharmacy

Estherchera (not verified)

Drgbwn mudths generic cialis online canadian pharmacy

Estherchera (not verified)

Zsivpq eqhwem Buy cialis canada online pharmacy

Estherchera (not verified)

Zsivpq eqhwem Buy cialis canada online pharmacy

Canadian generi... (not verified)

Woyotc usdlfu order levitra

phefewedy (not verified)

viagra cialis buy safe paypal https://bbuycialisss.com/ - Cialis Canada Phamacy п»їcialis Ce Qui Est Amoxil

phefewedy (not verified)

viagra cialis buy safe paypal https://bbuycialisss.com/ - Cialis Canada Phamacy п»їcialis Ce Qui Est Amoxil

Viagra 100mg england (not verified)

Qcrhkc awnwhg levitra vardenafil

Viagra 100mg england (not verified)

Qcrhkc awnwhg levitra vardenafil

Buy viagra com (not verified)

Pcrbih lxcyvz buy levitra ed pills otc

Buy pfizer viag... (not verified)

Rcyzai jwtxgk super kamagra cheap erectile dysfunction pills

Buy pfizer viag... (not verified)

Xpjhhs sysyps legitimate online slots for money online casino usa

Buy pfizer viag... (not verified)

Xpjhhs sysyps legitimate online slots for money online casino usa

Buy pfizer viag... (not verified)

Xpjhhs sysyps legitimate online slots for money online casino usa

Viagra overnigh... (not verified)

Jxpehj jpzpkj order vardenafil canadian pharmacy online

Viagra overnigh... (not verified)

Jxpehj jpzpkj order vardenafil canadian pharmacy online

Viagra overnigh... (not verified)

Jxpehj jpzpkj order vardenafil canadian pharmacy online

Viagra overnigh... (not verified)

Jxpehj jpzpkj order vardenafil canadian pharmacy online

Buy online viagra (not verified)

Llygmp sxfnwj casinos online free slots

Buy viagra with... (not verified)

Bbxeqe kmyuqn generic cialis canada walmart pharmacy

Buy viagra no p... (not verified)

Kvthpe wqrwla finasteride online pills for erection

phefewedy (not verified)

Generic Levitra For Sale In Us https://bbuycialisss.com/ - Buy Cialis Tarif Levitra 10mg Cialis Derniere Plus

phefewedy (not verified)

Generic Levitra For Sale In Us https://bbuycialisss.com/ - Buy Cialis Tarif Levitra 10mg Cialis Derniere Plus

phefewedy (not verified)

Generic Levitra For Sale In Us https://bbuycialisss.com/ - Buy Cialis Tarif Levitra 10mg Cialis Derniere Plus

phefewedy (not verified)

foro viagraespana https://bbuycialisss.com/ - Cialis foros donde comprar viagra generic cialis online pharmacy foros viagra

phefewedy (not verified)

foro viagraespana https://bbuycialisss.com/ - Cialis foros donde comprar viagra generic cialis online pharmacy foros viagra

Viagra 100mg england (not verified)

Qutxse jldoma viagra online canadian pharmacy best non prescription ed pills

Order viagra usa (not verified)

Tjzznb vzwpdd generic viagra cost online ed medications

phefewedy (not verified)

Amoxicillin Doasge For Children https://bbuycialisss.com/ - Cialis Cialis Blutdrucksenker Cialis Levothyroxine 0.2mg

Buy viagra cheap (not verified)

Lteoxr ynbhum vegas casino online betfair casino online nj

Add new comment