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

TeabsbevE (not verified)

Stromectol

Cigilleby (not verified)

cialis cheapest online prices

Viagra (not verified)

Strongest Viagra And Ciacis Pills

Cigilleby (not verified)

Cialis

Choiree (not verified)

http://buytadalafshop.com/ - cialis order online

Propecia (not verified)

Nuovo Cialis

Lyclolf (not verified)

http://buystromectolon.com/ - ivermectin use

Smandajax (not verified)

http://buysildenshop.com/ - Viagra

generic viagra ... (not verified)

Generic Cialis Review Online

Choiree (not verified)

http://buytadalafshop.com/ - buy cialis online

Cigilleby (not verified)

cialis 40 mg

injutty (not verified)

finasteride 5 mg online cheap

PatrickMok (not verified)

pr gay dating site india
gay dating a trans man
gay native american dating

PatrickMok (not verified)

ireland gay dating
dating with a gay couple
reddit.com: gay dating

Cigilleby (not verified)

Cialis

Cialis (not verified)

Keflex Medication

MichaelMot (not verified)

100percent free gay dating
best dating site for gay guys
gay trucker dating

gasiart (not verified)

https://buypropeciaon.com/ - Propecia

Propecia (not verified)

Zithromax Pediatric

Smandajax (not verified)

http://buysildenshop.com/ - Viagra

TeabsbevE (not verified)

price for ivermectin tablets

Smandajax (not verified)

https://buysildenshop.com/ - Viagra

gasiart (not verified)

http://buypropeciaon.com/ - Propecia

gasiart (not verified)

https://buypropeciaon.com/ - Propecia

Henrytrief (not verified)

gay dating tampa
gay dating sim romance koi
gay dating sites silver

cheap cialis (not verified)

Propecia Male Fertility Sperm Count

Lyclolf (not verified)

http://buystromectolon.com/ - Stromectol

gasiart (not verified)

https://buypropeciaon.com/ - Propecia

Choiree (not verified)

https://buytadalafshop.com/ - Cialis

Cigilleby (not verified)

Cialis

Stromectol (not verified)

Online Inhalers No Prescription Usa

Lyclolf (not verified)

https://buystromectolon.com/ - Stromectol

reddit buy zith... (not verified)

Meinungen Zu Priligy

Henrytrief (not verified)

gay asian dating white
dating uncensored 2021 gay
lgbt gay dating sites

ethipvoto (not verified)

priligy amazon uk

Envecehem (not verified)

furosemide 20 mg and weight loss

Arerrot (not verified)

antibiotic azithromycin

Priligy (not verified)

cialis free sample coupon

abejessah (not verified)

hydroxychloroquine over the counter walgreens

abejessah (not verified)

hydroxychloroquine online order

ethipvoto (not verified)

buy priligy in the usa

Lasix (not verified)

Propecia Nioxin Treatment

Whasksaks (not verified)

http://buyplaquenilcv.com/ - stopping plaquenil abruptly

Envecehem (not verified)

Lasix

Plaquenil (not verified)

viagra online pfizer

furosemide mexico (not verified)

viagra and cialis and

Envecehem (not verified)

Lasix

furosemide food... (not verified)

Achat Viagra Original

Arerrot (not verified)

Zithromax

78 buy azithrom... (not verified)

viagra droga generica

Add new comment