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

olia james (not verified)

Good information shared.

If you want to host a website and looking for hosting related coupon plz visit https://www.myhostingcoupon.com/

Brian (not verified)

http://web-tricks.org/content/how-use-ssh-with-php-using-private-and-public-ssh-keys

Brian (not verified)
enetibliole (not verified)

real money casino online casino bonus best online casinos casino online slots http://casinorealmoneysoe.com/ - free casino games

enetibliole (not verified)

http://onlinecasinogamespcx.com/ online slot games play online casino free online slots free slots

enetibliole (not verified)

casino real money free casino games casino game casino blackjack http://onlinecasinogamespcx.com/ - real money casino

enetibliole (not verified)

casino play http://onlinecasinogamespcx.com/ - online casino gambling vegas slots online online gambling

enetibliole (not verified)

casino play http://onlinecasinogamespcx.com/ - online casino gambling vegas slots online online gambling

JanGediava (not verified)

Keflex 250 Mg Doses buy cialis online safely Viagra En Algerie cialis online cheap Com

enetibliole (not verified)

casino game http://onlinecasinogamespcx.com/ - free casino world class casino slots slots free

inticleni (not verified)

http://onlinecasinosw.com/ casino games http://onlinecasinosw.com/ - online casino online casino slots

inticleni (not verified)

http://onlinecasinosw.com/ casino games http://onlinecasinosw.com/ - online casino online casino slots

enetibliole (not verified)

casino blackjack casino online slots no deposit casino vegas slots online http://onlinecasinogamespcx.com/ - play casino

enetibliole (not verified)

casino blackjack casino online slots no deposit casino vegas slots online http://onlinecasinogamespcx.com/ - play casino

inticleni (not verified)

no deposit casino casino blackjack online gambling

inticleni (not verified)

no deposit casino casino blackjack online gambling

inticleni (not verified)

no deposit casino casino blackjack online gambling

enetibliole (not verified)

online casino real money http://onlinecasinogamespcx.com/ - casino bonus codes casino blackjack slots for real money online casino games

enetibliole (not verified)

online casino real money http://onlinecasinogamespcx.com/ - casino bonus codes casino blackjack slots for real money online casino games

enetibliole (not verified)

online casino real money http://onlinecasinogamespcx.com/ - casino bonus codes casino blackjack slots for real money online casino games

inticleni (not verified)

vegas casino slots http://onlinecasinosw.com/ - slots for real money vegas casino slots casino slots casino game

Ushusd o (not verified)
enetibliole (not verified)

http://onlinecasinogamespcx.com/ big fish casino http://onlinecasinogamespcx.com/ - real casino slots best online casino

enetibliole (not verified)

http://onlinecasinogamespcx.com/ big fish casino http://onlinecasinogamespcx.com/ - real casino slots best online casino

inticleni (not verified)

http://onlinecasinosw.com/ online casino bonus http://onlinecasinosw.com/ - play online casino online casino bonus

enetibliole (not verified)

http://onlinecasinogamespcx.com/ real money casino http://onlinecasinogamespcx.com/ - no deposit casino free slots

enetibliole (not verified)

http://onlinecasinogamespcx.com/ real money casino http://onlinecasinogamespcx.com/ - no deposit casino free slots

inticleni (not verified)

online casino gambling casino play slot games

inticleni (not verified)

online casino gambling casino play slot games

enetibliole (not verified)

http://onlinecasinogamespcx.com/ free casino slot games gold fish casino slots no deposit casino real money casino

inticleni (not verified)

online gambling free casino games online free casino cashman casino slots

inticleni (not verified)

slots games free casino real money free slots free casino games online

inticleni (not verified)

slots games free casino real money free slots free casino games online

enetibliole (not verified)

http://onlinecasinogamespcx.com/ world class casino slots slots free play slots world class casino slots

enetibliole (not verified)

http://onlinecasinogamespcx.com/ world class casino slots slots free play slots world class casino slots

inticleni (not verified)

http://onlinecasinosw.com/ world class casino slots http://onlinecasinosw.com/ - casino online play slots online

inticleni (not verified)

http://onlinecasinosw.com/ world class casino slots http://onlinecasinosw.com/ - casino online play slots online

inticleni (not verified)

http://onlinecasinosw.com/ world class casino slots http://onlinecasinosw.com/ - casino online play slots online

enetibliole (not verified)

world class casino slots http://onlinecasinogamespcx.com/ casino online casino blackjack slot games

inticleni (not verified)

http://onlinecasinosw.com/ online casino http://onlinecasinosw.com/ - casino bonus codes play slots online

inticleni (not verified)

http://onlinecasinosw.com/ online casino http://onlinecasinosw.com/ - casino bonus codes play slots online

inticleni (not verified)

http://onlinecasinosw.com/ online casino http://onlinecasinosw.com/ - casino bonus codes play slots online

enetibliole (not verified)

play online casino play online casino big fish casino big fish casino http://onlinecasinogamespcx.com/ - casino online slots

inticleni (not verified)

no deposit casino free casino games online casino real money

enetibliole (not verified)

online slots http://onlinecasinogamespcx.com/ - cashman casino slots casino play world class casino slots casino real money

inticleni (not verified)

casino play http://onlinecasinosw.com/ - casino blackjack free online slots best online casinos play online casino

enetibliole (not verified)

http://onlinecasinopc.com/ real money casino vegas casino slots no deposit casino casino online slots

inticleni (not verified)

online slot games best online casinos real casino slots

enetibliole (not verified)

free casino slot games http://onlinecasinopc.com/ - online casinos slots free casino blackjack

inticleni (not verified)

http://onlinecasinosw.com/ free online slots casino games casino games free casino games online

Add new comment