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

chonula (not verified)

Neurontine

GustavoJAl (not verified)

monster resume writing service
write my paper cheap

Prednisone (not verified)

como tomar viagra 50 mg

Nepeazy (not verified)

https://prednisonebuyon.com/ - prednisone dosage for 60 lb dog

Nepeazy (not verified)

http://prednisonebuyon.com/ - Prednisone

chonula (not verified)

can i take tramadol neurontine and robaxin

Illeclima (not verified)

https://buyneurontine.com/ - side effects gabapentin

Illeclima (not verified)

https://buyneurontine.com/ - Neurontine

Nepeazy (not verified)

https://prednisonebuyon.com/ - Prednisone

MatthewCap (not verified)

help with essay writing
homework help math

HtbnDrank (not verified)

viagra en bicicleta letra jake viagra two and a half men

FthtBYDAY (not verified)

jack in the box commercial with viagra headquarters of viagra photo

Stacysholi (not verified)

[url=https://vulkans-russia.ru/pinup-android-ueuen-proqram.php]pinup android ucun proqram[/url] [url=https://vulkanmega2.ru/pn-up-bet.php]p?n up bet[/url] [url=https://mostbet-online1.ru/mostbet-bukmekerskaya-kontora-1.php]mostbet букмекерская контора[/url] [url=https://clubvulkans.ru/skachat-odin-1xbet.php]скачать один 1xbet[/url] [url=https://garillabeting.com/mostbet-ofitsialniy-sayt-registratsiya-red-1.php]мостбет официальный сайт регистрация red[/url]

[url=https://reelemperor-cas.com/1-h-bet-stavka.php]1 х бет ставка[/url] [url=https://gorilla-bet1.com/algoritm-mostbet.php]алгоритм мостбет[/url] [url=https://clubvulkans.ru/promokod-1xbet-na-sloti.php]промокод 1xbet на слоты[/url] [url=https://clubvulkans.ru/1-hbet-ofitsialniy-sayt-na-segodnya.php]1 хбет официальный сайт на сегодня[/url] [url=https://vulkanplus1.ru/pin-ap-skachat-bukmekerskaya-kontora.php]пин ап скачать букмекерская контора[/url]

generic finaste... (not verified)

Viagra Pharmacy

freerurgy (not verified)

Propecia

Locuoub (not verified)

viagra generic online canada

freerurgy (not verified)

Propecia

Cafeseast (not verified)

Cialis

Cafeseast (not verified)

Cialis

Immittexy (not verified)

Viagra

Locuoub (not verified)

Viagra

knockadia (not verified)

cheapest cialis available

Cialis (not verified)

Isotretinoin Free Shipping

Immittexy (not verified)

Viagra

Viagra (not verified)

Donde Comprar Viagra Para Mujeres

Locuoub (not verified)

Viagra

Immittexy (not verified)

viagra does not work for me

buy cheap phent... (not verified)

Levitra Receta

freerurgy (not verified)

Propecia

gensywhes (not verified)

ivermectin human dosage

finasteride rec... (not verified)

levitra generique meilleur

Immittexy (not verified)

viagra cialis on line

Propecia (not verified)

Cephalexin Is Expired Is It Okay

knockadia (not verified)

Cialis

buy cialis online uk (not verified)

Namenda

Cafeseast (not verified)

cialis pills for sale

BrandonClifs (not verified)

writing a college application essay
write a reflection essay
mba essay writing services

Bobimibra (not verified)

amazon priligy

finasteride (pr... (not verified)

Viagra Donde Se Compra

Cafeseast (not verified)

Cialis

Viagra (not verified)

Viagra Cialis Simili

Immittexy (not verified)

where can i buy viagra cheap

Cialis (not verified)

5 mg dose for daily cialis

Viagra (not verified)

levitra informacion

Viagra (not verified)

Levitra Prix Pharmacie Pour

knockadia (not verified)

Cialis

buy cialis online (not verified)

Levitra Generico Trucchi

cialis reviews (not verified)

Propecia Woman

Stromectol (not verified)

Meilleur Pharmacie En Ligne Propecia

Locuoub (not verified)

Viagra

Add new comment