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)

free casino http://casinoslotsfest.com/ - online gambling online casino games online casino gambling free casino games online

enetibliole (not verified)

free casino http://casinoslotsfest.com/ - online gambling online casino games online casino gambling free casino games online

inticleni (not verified)

world class casino slots slot games casino real money

inticleni (not verified)

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

inticleni (not verified)

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

inticleni (not verified)

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

enetibliole (not verified)

http://casinoslotsfest.com/ slot games casino online online casino bonus online gambling

inticleni (not verified)

casino real money http://onlinecasinofbk.com/ - free casino slot games free casino slot games online casino real money free casino slot games

inticleni (not verified)

casino real money http://onlinecasinofbk.com/ - free casino slot games free casino slot games online casino real money free casino slot games

inticleni (not verified)

casino real money http://onlinecasinofbk.com/ - free casino slot games free casino slot games online casino real money free casino slot games

enetibliole (not verified)

play slots http://casinoslotsfest.com/ - big fish casino free casino games online online casino slots

enetibliole (not verified)

play slots http://casinoslotsfest.com/ - big fish casino free casino games online online casino slots

inticleni (not verified)

best online casinos http://onlinecasinofbk.com/ - world class casino slots real casino slots big fish casino

enetibliole (not verified)

online casino bonus slots online play online casino slots online http://casinoslotsfest.com/ - cashman casino slots

inticleni (not verified)

online casino games http://onlinecasinofbk.com/ online casino games free casino games online slot games

inticleni (not verified)

online casino games http://onlinecasinofbk.com/ online casino games free casino games online slot games

enetibliole (not verified)

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

inticleni (not verified)

online casino gambling http://onlinecasinofbk.com/ online casino gambling slots games real casino slots

inticleni (not verified)

online casino gambling http://onlinecasinofbk.com/ online casino gambling slots games real casino slots

inticleni (not verified)

online casino gambling http://onlinecasinofbk.com/ online casino gambling slots games real casino slots

enetibliole (not verified)

http://casinoslotsfest.com/ play slots online slots online free slots casino game

Levitra vs viagra (not verified)

Fobzim kutwfo Buy viagra with discount Levitra or viagra

inticleni (not verified)

world class casino slots http://onlinecasinofbk.com/ casino bonus codes slots games free online casino

inticleni (not verified)

world class casino slots http://onlinecasinofbk.com/ casino bonus codes slots games free online casino

inticleni (not verified)

world class casino slots http://onlinecasinofbk.com/ casino bonus codes slots games free online casino

inticleni (not verified)

world class casino slots http://onlinecasinofbk.com/ casino bonus codes slots games free online casino

enetibliole (not verified)

http://casinoslotsfest.com/ no deposit casino casino bonus codes slots for real money casino real money

enetibliole (not verified)

http://casinoslotsfest.com/ no deposit casino casino bonus codes slots for real money casino real money

enetibliole (not verified)

online gambling http://casinoslotsfest.com/ - free casino games online online casino bonus free slots games slots for real money

enetibliole (not verified)

online gambling http://casinoslotsfest.com/ - free casino games online online casino bonus free slots games slots for real money

inticleni (not verified)

http://onlinecasinofbk.com/ casino slots play slots online free slots games play slots online

Best price for ... (not verified)

Fbodqv efcvmz Female viagra Best viagra alternative

Buy viagra internet (not verified)

Wdagis oabrhu buy viagra online reviews Order viagra us

inticleni (not verified)

vegas casino slots http://onlinecasinofbk.com/ slots for real money online casino slots play online casino

Estherchera (not verified)

Okdvma wcokte sildenafil tablets Low cost viagra

inticleni (not verified)

no deposit casino http://onlinecasinofbk.com/ real casino slots slots free casino blackjack

inticleni (not verified)

no deposit casino http://onlinecasinofbk.com/ real casino slots slots free casino blackjack

inticleni (not verified)

no deposit casino http://onlinecasinofbk.com/ real casino slots slots free casino blackjack

inticleni (not verified)

http://onlinecasinofbk.com/ casino online casino bonus codes slots games online casino gambling

inticleni (not verified)

cbd products http://cbdoilusd.com/ - best cbd oil cbd hemp cbd oil benefits hemp cbd oil

Estherchera (not verified)

Cecazg eqobsl Viagra next day Viagra jelly

inticleni (not verified)

cbd near me http://cbdoilusd.com/ cbd store hemp cbd buy hemp oil

inticleni (not verified)

cbd vape http://cbdoilusd.com/ - cbd pure cbd oils cbd tinctures

inticleni (not verified)

http://cbdoilusd.com/ buy cbd oil cbd vape hemp cbd oil cbd pills

inticleni (not verified)

http://cbdoilusd.com/ buy cbd oil cbd vape hemp cbd oil cbd pills

inticleni (not verified)

http://cbdoilusd.com/ buy cbd oil cbd vape hemp cbd oil cbd pills

inticleni (not verified)

http://cbdoilusd.com/ buy cbd oil cbd vape hemp cbd oil cbd pills

inticleni (not verified)

cbd online http://cbdoilusd.com/ - cbd medic cbd best cbd oil cbd drops

inticleni (not verified)

cbd online http://cbdoilusd.com/ - cbd medic cbd best cbd oil cbd drops

inticleni (not verified)

http://cbdoilusd.com/ cbd oil store cbd drops cbd tinctures pure cbd oil

Add new comment