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

Trubbebra (not verified)

https://buypriligyhop.com/ - medicamento priligy estudios clinicos

Lasix (not verified)

cialis cheapest

Priligy (not verified)

Cialis A Basso Costo

Whasksaks (not verified)

http://buyplaquenilcv.com/ - Plaquenil

Plaquenil (not verified)

Comprar Cialis En Vina Del Mar

abejessah (not verified)

Plaquenil

abejessah (not verified)

Plaquenil

Graiveada (not verified)

http://buylasixshop.com/ - lasix contraindications

Plaquenil (not verified)

Combivent Respimat Price

Plaquenil (not verified)

Cialis Viagra

Graiveada (not verified)

https://buylasixshop.com/ - taking lasix and still swelling

Whasksaks (not verified)

http://buyplaquenilcv.com/ - hydroxychloroquine 200 mg price

Graiveada (not verified)

http://buylasixshop.com/ - furosemide 20 mg

Agobben (not verified)

https://buyzithromaxinf.com/ - Zithromax

Dbgvweack (not verified)

help with dissertation writing writing dissertation service

Agobben (not verified)

https://buyzithromaxinf.com/ - Zithromax

Whasksaks (not verified)

http://buyplaquenilcv.com/ - plaquenil toxicity symptoms

want to buy pri... (not verified)

cialis effet recherche

KrbcDrank (not verified)

writing a good thesis writing a doctoral thesis

ethipvoto (not verified)

priligy amazon uk

Envecehem (not verified)

furosemide drug card

azithromycin us... (not verified)

Fda Approved Canadian Pharmacies

Agobben (not verified)

http://buyzithromaxinf.com/ - Zithromax

Agobben (not verified)

https://buyzithromaxinf.com/ - Zithromax

ethipvoto (not verified)

priligy amazon canada

Plaquenil (not verified)

cialis generique ordonnance

Trubbebra (not verified)

http://buypriligyhop.com/ - free samples of priligy

abejessah (not verified)

Plaquenil

Whasksaks (not verified)

http://buyplaquenilcv.com/ - Plaquenil

Agobben (not verified)

http://buyzithromaxinf.com/ - buy zithromax online without prescrition

AAnlwdz (not verified)

Перші ластівки 3 сезон Перші ластівки 3 сезон hulx zfd yjpl nlh viqi rqk

tapering predni... (not verified)

Best Lisinopril No Prescription Canada

urgetty (not verified)

Prednisone

chonula (not verified)

Neurontine

urgetty (not verified)

Prednisone

neurontin side ... (not verified)

cialis online cost

chonula (not verified)

gabapentin

Neurontine (not verified)

order accutane online uk

chonula (not verified)

Neurontine

gabapentin gene... (not verified)

What To Use Instead Of Alli Diet Drug

urgetty (not verified)

Prednisone

neurontin side ... (not verified)

generic cialis from india

Prednisone (not verified)

Aciclovir Genfar

urgetty (not verified)

Prednisone

urgetty (not verified)

Prednisone

buy prednisone ... (not verified)

Compare Viagra Prices Walmart

Illeclima (not verified)

http://buyneurontine.com/ - Neurontine

Neurontine (not verified)

Levitra Avis Doctissimo

Illeclima (not verified)

https://buyneurontine.com/ - gabapentin dosage

Illeclima (not verified)

http://buyneurontine.com/ - gabapentin used for

Add new comment