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

enetibliole (not verified)

online casino http://casinoplaysw.com/ online casino real money vegas casino slots free online slots

inticleni (not verified)

gold fish casino slots play casino online casino games casino bonus codes

inticleni (not verified)

gold fish casino slots play casino online casino games casino bonus codes

enetibliole (not verified)

online casino http://casinoplaysw.com/ online casino real money vegas casino slots free online slots

enetibliole (not verified)

http://casinoplaysw.com/ casino game casino online free casino games vegas slots online

inticleni (not verified)

http://casinoslotspc.com/ online slots free casino no deposit casino no deposit casino

inticleni (not verified)

http://casinoslotspc.com/ online slots free casino no deposit casino no deposit casino

inticleni (not verified)

http://casinoslotspc.com/ online slots free casino no deposit casino no deposit casino

enetibliole (not verified)

http://casinoplaysw.com/ free slots games cashman casino slots free slots online casino bonus

inticleni (not verified)

real money casino online casinos big fish casino casino games

inticleni (not verified)

real money casino online casinos big fish casino casino games

inticleni (not verified)

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

enetibliole (not verified)

play online casino http://casinoplaysw.com/ online casino bonus slots games play slots

enetibliole (not verified)

play online casino http://casinoplaysw.com/ online casino bonus slots games play slots

inticleni (not verified)

http://casinoslotspc.com/ real money casino http://casinoslotspc.com/ - online casino slots best online casinos

enetibliole (not verified)

vegas casino slots play casino slots games free

inticleni (not verified)

online casino real money casino free slots vegas slots online

enetibliole (not verified)

vegas slots online http://casinoplaysw.com/ - online casino gambling slots games free online casino slots

enetibliole (not verified)

vegas slots online http://casinoplaysw.com/ - online casino gambling slots games free online casino slots

enetibliole (not verified)

vegas slots online http://casinoplaysw.com/ - online casino gambling slots games free online casino slots

inticleni (not verified)

real money casino casino real money slot games

inticleni (not verified)

real money casino casino real money slot games

inticleni (not verified)

real money casino casino real money slot games

Viagra original... (not verified)

Glruqp jhzztg buying generic viagra 50mg viagra

enetibliole (not verified)

http://casinogamespqr.com/ online slots free casino games online online casino slots casino game

inticleni (not verified)

casino slots vegas casino slots casino games

inticleni (not verified)

casino slots vegas casino slots casino games

enetibliole (not verified)

http://casinogamespqr.com/ slots games free free casino slot games free casino best online casinos

enetibliole (not verified)

http://casinogamespqr.com/ slots games free free casino slot games free casino best online casinos

inticleni (not verified)

casino bonus codes http://casinoslotspc.com/ - vegas slots online vegas casino slots slots games gold fish casino slots

inticleni (not verified)

casino bonus codes http://casinoslotspc.com/ - vegas slots online vegas casino slots slots games gold fish casino slots

enetibliole (not verified)

http://casinogamespqr.com/ no deposit casino http://casinogamespqr.com/ - slots games online slots

enetibliole (not verified)

http://casinogamespqr.com/ no deposit casino http://casinogamespqr.com/ - slots games online slots

inticleni (not verified)

casino play http://casinoslotspc.com/ free slots games world class casino slots free slots

enetibliole (not verified)

online casino casino slots online casino slots casino real money

enetibliole (not verified)

online casino casino slots online casino slots casino real money

enetibliole (not verified)

online casino casino slots online casino slots casino real money

inticleni (not verified)

http://casinoslotspc.com/ world class casino slots http://casinoslotspc.com/ - casino real money vegas slots online

Viagra brand (not verified)

Zvkjun ppfjlq cheapest viagra prices online Order viagra

enetibliole (not verified)

http://casinogamespqr.com/ casino bonus codes real money casino play slots online slots for real money

enetibliole (not verified)

http://casinogamespqr.com/ casino bonus codes real money casino play slots online slots for real money

inticleni (not verified)

online slot games free online slots slots for real money slots free http://casinoslotspc.com/ - real money casino

enetibliole (not verified)

online casino games http://casinogamespqr.com/ - no deposit casino casino play online casino slots online casino

inticleni (not verified)

gold fish casino slots http://casinoslotspc.com/ free online slots online casino cashman casino slots

inticleni (not verified)

gold fish casino slots http://casinoslotspc.com/ free online slots online casino cashman casino slots

enetibliole (not verified)

casino blackjack http://casinogamespqr.com/ - play slots online free casino slot games slots games

inticleni (not verified)

casino blackjack free casino slots free casino slots http://casinoslotspc.com/ - slots games

enetibliole (not verified)

online casino casino real money online slots

enetibliole (not verified)

online casino casino real money online slots

enetibliole (not verified)

online casino casino real money online slots

Add new comment