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://cbdoilglk.com/ cbd hemp cbd pure cbd medic buy cbd oil

inticleni (not verified)

cbd vape http://cbdoilglk.com/ - hemp oil for pain cdb oils pure cbd oil cdb oils

inticleni (not verified)

cbd tinctures http://buycbdoilwalm.com/ - cbd for dogs cbd hemp cbd store cbd hemp

inticleni (not verified)

cbd tinctures http://buycbdoilwalm.com/ - cbd for dogs cbd hemp cbd store cbd hemp

inticleni (not verified)

cbd cream cbd oils cbd cream medterra cbd

inticleni (not verified)

cbd cream cbd oils cbd cream medterra cbd

inticleni (not verified)

cbd vape http://buycbdoilwalm.com/ - pure cbd oil cbd gummies best cbd oil

inticleni (not verified)

cbd oil for dogs http://buycbdoilwalm.com/ - cbd oil at walmart cbd gummies medterra cbd buy cbd oil online

inticleni (not verified)

cbd for dogs http://buycbdoilwalm.com/ hemp cbd buy hemp oil cbd cream

inticleni (not verified)

http://buycbdoilwalm.com/ buy cbd cbd oil at walmart cbd gummies walmart hemp cbd oil

inticleni (not verified)

cbd for dogs http://buycbdoilwalm.com/ buy cbd buy cbd medterra cbd

inticleni (not verified)

cbd pure http://buycbdoilwalm.com/ hemp cbd best cbd oil cbd oil at walmart

inticleni (not verified)

cbd pure http://buycbdoilwalm.com/ hemp cbd best cbd oil cbd oil at walmart

inticleni (not verified)

hemp oil cbd store cannabis oil cbd gummies walmart

inticleni (not verified)

cbd oil for sale http://buycbdoilwalm.com/ - cbd oils medterra cbd hemp cbd oil

inticleni (not verified)

http://buycbdoilonline.us.com/ buy cbd cbd online cbd oil store cbd oil at walmart

enetibliole (not verified)

hemp cbd cbd cream cbd products cannabis oil

inticleni (not verified)

http://buycbdoilonline.us.com/ cbd hemp cbd oil for dogs cbd for sale medterra cbd

enetibliole (not verified)

cbd oil for dogs http://cbdoilrx.us.com/ - cbd gummies walmart cbd for sale cbd for dogs

enetibliole (not verified)

cbd oil for dogs http://cbdoilrx.us.com/ - cbd gummies walmart cbd for sale cbd for dogs

inticleni (not verified)

cbd http://buycbdoilonline.us.com/ - cbd gummies hemp oil cbd gummies walmart

inticleni (not verified)

cbd gummies cbd oil for dogs cbd store

erectile dysfun... (not verified)

Csipvy chvckp generic ed pills impotence pills

inticleni (not verified)

buy hemp oil http://buycbdoilonline.us.com/ - cbd online buy cbd oil online medterra cbd cbd tinctures

inticleni (not verified)

cbd for sale http://buycbdoilonline.us.com/ cbd drops buy cbd oil online cbd oil benefits

ed pills gnc (not verified)

Dhfwmc ffquhg buy ed pills best ed pills

ed pills gnc (not verified)

Dhfwmc ffquhg buy ed pills best ed pills

cheapest ed pills (not verified)

Xokijj bfyaip best otc ed pills ed pills for sale

best otc ed pills (not verified)

Knstbj sscqwu erectile dysfunction pills best ed pills at gnc

best otc ed pills (not verified)

Knstbj sscqwu erectile dysfunction pills best ed pills at gnc

Add new comment