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

inticleni (not verified)

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

enetibliole (not verified)

real casino slots http://onlinecasinopc.com/ - free online slots casino games vegas slots online

enetibliole (not verified)

real casino slots http://onlinecasinopc.com/ - free online slots casino games vegas slots online

inticleni (not verified)

free casino http://onlinecasinosw.com/ - casino play slot games slots free

enetibliole (not verified)

casino real money http://onlinecasinopc.com/ no deposit casino online casino games online gambling

inticleni (not verified)

free online slots casino real money online casino real money play online casino http://playcasinosw.com/ - online gambling

enetibliole (not verified)

real money casino http://onlinecasinopc.com/ online gambling online casino slots free online slots

inticleni (not verified)

http://playcasinosw.com/ casino online slots no deposit casino real casino slots casino online slots

inticleni (not verified)

http://playcasinosw.com/ casino online slots no deposit casino real casino slots casino online slots

enetibliole (not verified)

free casino games free casino games play slots online casino bonus http://onlinecasinopc.com/ - vegas slots online

inticleni (not verified)

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

inticleni (not verified)

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

inticleni (not verified)

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

inticleni (not verified)

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

enetibliole (not verified)

free online slots online gambling free casino games

inticleni (not verified)

http://playcasinosw.com/ casino play http://playcasinosw.com/ - online casino bonus no deposit casino

enetibliole (not verified)

http://onlinecasinopc.com/ casino games free online slots online casino bonus vegas casino slots

enetibliole (not verified)

http://onlinecasinopc.com/ casino games free online slots online casino bonus vegas casino slots

enetibliole (not verified)

http://onlinecasinopc.com/ casino games free online slots online casino bonus vegas casino slots

enetibliole (not verified)

http://onlinecasinopc.com/ casino games free online slots online casino bonus vegas casino slots

inticleni (not verified)

slot games slots games best online casino

enetibliole (not verified)

world class casino slots slots games free casino play

inticleni (not verified)

http://playcasinosw.com/ online casino slots http://playcasinosw.com/ - vegas slots online slots for real money

inticleni (not verified)

http://playcasinosw.com/ online casino slots http://playcasinosw.com/ - vegas slots online slots for real money

inticleni (not verified)

http://playcasinosw.com/ online casino slots http://playcasinosw.com/ - vegas slots online slots for real money

inticleni (not verified)

http://playcasinosw.com/ online casino slots http://playcasinosw.com/ - vegas slots online slots for real money

enetibliole (not verified)

online casino real money http://onlinecasinopc.com/ best online casinos online casino slots gold fish casino slots

inticleni (not verified)

online casino gambling http://playcasinosw.com/ - online casinos free casino slot games slot games no deposit casino

inticleni (not verified)

online casino gambling http://playcasinosw.com/ - online casinos free casino slot games slot games no deposit casino

inticleni (not verified)

online casino gambling http://playcasinosw.com/ - online casinos free casino slot games slot games no deposit casino

inticleni (not verified)

online casino gambling http://playcasinosw.com/ - online casinos free casino slot games slot games no deposit casino

enetibliole (not verified)

play slots online http://casinoplaysw.com/ - slots for real money free slots games online slots

enetibliole (not verified)

play slots online http://casinoplaysw.com/ - slots for real money free slots games online slots

enetibliole (not verified)

play slots online http://casinoplaysw.com/ - slots for real money free slots games online slots

enetibliole (not verified)

play slots online http://casinoplaysw.com/ - slots for real money free slots games online slots

enetibliole (not verified)

play slots online http://casinoplaysw.com/ - slots for real money free slots games online slots

inticleni (not verified)

online casino games casino slots free casino casino bonus codes http://playcasinosw.com/ - online casinos

inticleni (not verified)

online casino games casino slots free casino casino bonus codes http://playcasinosw.com/ - online casinos

enetibliole (not verified)

free casino games online vegas casino slots free slots

enetibliole (not verified)

free casino games online vegas casino slots free slots

enetibliole (not verified)

free casino games online vegas casino slots free slots

inticleni (not verified)

play slots online http://playcasinosw.com/ - play slots online casinos online casino real money

inticleni (not verified)

play slots online http://playcasinosw.com/ - play slots online casinos online casino real money

enetibliole (not verified)

online casino bonus http://casinoplaysw.com/ - casino game casino online slots online gambling

inticleni (not verified)

http://playcasinosw.com/ free casino casino online slots online casino slots online casino gambling

enetibliole (not verified)

slots games online slots best online casinos free slots games

enetibliole (not verified)

slots games online slots best online casinos free slots games

inticleni (not verified)

free casino games online casino casino play free casino games online http://casinoslotspc.com/ - casino slots

enetibliole (not verified)

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

enetibliole (not verified)

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

Add new comment