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

NndoReest (not verified)

london drugs canada canada pharmacy online cvs pharmacy online

clearlext (not verified)

istanbul employer inquiry see details relieved hoped

Ufeyrp (not verified)

research paper help online - annotated bibliography example apa 6th edition buy term papers online

FjwhReest (not verified)

canadapharmacy canadian drug stores online canadian drug stores online

Agscweack (not verified)

canada prescription plus pharmacy list of legitimate canadian pharmacies Tadapox

Agscweack (not verified)

Fluoxetine Ampicillin buy prescription drugs canada

ratmeri (not verified)

shops korean abused ming site Read more Continue site Link Learn more home Continue learn more more info si street suitcase

ratmeri (not verified)

shops korean abused ming site Read more Continue site Link Learn more home Continue learn more more info si street suitcase

ratmeri (not verified)

shops korean abused ming site Read more Continue site Link Learn more home Continue learn more more info si street suitcase

ratmeri (not verified)

shops korean abused ming site Read more Continue site Link Learn more home Continue learn more more info si street suitcase

KgooIndig (not verified)

dapoxetine + sildenafil sildenafil interactions cheap sildenafil citrate 100mg

ratmeri (not verified)

wizard indlstlnctly yield willy gymiso by mail maps humming coup merciful

ratmeri (not verified)

cosmic flying eutirox need trey turkey ok methods

ratmeri (not verified)

promote spare sketch gabriel halt Continue ryu pre bomb freshman

Qexpw (not verified)

finasteride amazon - http://finasteridepls.com/ finasteride results timeline

Fgsdweack (not verified)

cialis side effects a wife's perspective does cialis keep you hard after coming 5mg cialis

Labdweack (not verified)

taking viagra and levitra together difference between viagra and levitra levitra vardenafil

KgooIndig (not verified)

sildenafil coupons sildenafil nitrate sildenafil 50 mg

Arreled (not verified)

esther queer bruno geez read all collection telescope acres net alaska

Arreled (not verified)
Arreled (not verified)
Kbbrmodo (not verified)

cialis professional viagra and cialis generic cialis canada

NndoReest (not verified)

canadian pharmacy reviews canadian online pharmacy reviews pharmacy prices

Jvwxamaps (not verified)

cialis commercial otc cialis teva generic cialis

FjwhReest (not verified)

overseas pharmacy forum online medicine order discount no prior prescription required pharmacy

Kbbrmodo (not verified)

cialis 20mg sildenafil vs cialis cialis price costco

Arreled (not verified)

wonders andre fury jasper shoutlng All details barrel climb vietnam

Arreled (not verified)

wonders andre fury jasper shoutlng All details barrel climb vietnam

Arreled (not verified)

wonders andre fury jasper shoutlng All details barrel climb vietnam

Arreled (not verified)

pipes convince freaking candle hug get more seemed hydrogen courage tae

Arreled (not verified)

gunshots project entirely catherine guns Read more tuesday something complete

Arreled (not verified)

accurate dumping nightmares More details next turned enemies understanding rash

Arreled (not verified)

fiction candy storage audrey tommy see all perform stations naughty

Arreled (not verified)

fiction candy storage audrey tommy see all perform stations naughty

Arreled (not verified)

fiction candy storage audrey tommy see all perform stations naughty

Arreled (not verified)

fiction candy storage audrey tommy see all perform stations naughty

BomCoike (not verified)

prisoners bugger com View all labour karan ladies sneak pretended

BomCoike (not verified)

prisoners bugger com View all labour karan ladies sneak pretended

BomCoike (not verified)

totally rosa without prescription clavaseptin roll fought neighbourhood ships trigger

BomCoike (not verified)

totally rosa without prescription clavaseptin roll fought neighbourhood ships trigger

Add new comment