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

Jndqweack (not verified)

[url=https://50cialmen.com/]free cialis voucher[/url] [url=https://aazmencial.com/]marley generics cialis[/url] [url=https://joicialviaed.com/]cialis for fun[/url]

LecfReest (not verified)

[url=https://hopeviagrin.com/]what viagra does[/url] [url=https://lightvigra.com/]does viagra make you horny[/url] [url=https://xuypharmacyonline.com/]the peoples pharmacy[/url] [url=https://online21rxon.com/]pharmacy 24 hours[/url] [url=https://happyrxpharmacy21.com/]vons pharmacy (inside vons)[/url] [url=https://storerxpharmcanada.com/]winn dixie pharmacy hours[/url]

KedReest (not verified)

[url=https://qqllviagria.com/]amazon viagra[/url] [url=https://ssviagriia.com/]canada viagra[/url] [url=https://canadianpharmnorx.com/]american specialty pharmacy[/url] [url=https://ggviagll.com/]no prescription viagra[/url] [url=https://viagerkr.com/]viagra vs cialis[/url] [url=https://viagriyvik.com/]how much is viagra per pill[/url]

Fbshweack (not verified)

[url=https://thesisacloud.com/]thesis formatting[/url] buy a thesis writing your thesis thesis writing help uk

Jndqweack (not verified)

[url=https://50cialmen.com/]when will cialis be generic[/url] [url=https://aazmencial.com/]viagra vs cialis vs levitra[/url] [url=https://joicialviaed.com/]how effective is cialis[/url]

LecfReest (not verified)

[url=https://hopeviagrin.com/]viagra soft tabs[/url] [url=https://lightvigra.com/]buy viagra online cheap[/url] [url=https://xuypharmacyonline.com/]retail pharmacy[/url] [url=https://online21rxon.com/]pharmacy technician exam[/url] [url=https://happyrxpharmacy21.com/]mo board of pharmacy[/url] [url=https://storerxpharmcanada.com/]cvs pharmacy app[/url]

KedReest (not verified)

[url=https://qqllviagria.com/]buy viagra cheapest[/url] [url=https://ssviagriia.com/]extenze vs viagra[/url] [url=https://canadianpharmnorx.com/]randalls pharmacy[/url] [url=https://ggviagll.com/]lovely lilith viagra falls[/url] [url=https://viagerkr.com/]where to buy viagra online[/url] [url=https://viagriyvik.com/]female viagra cvs[/url]

Brfgmodo (not verified)

[url=https://ljcialishe.com/]what are the side effects of cialis[/url] [url=https://cialisvja.com/]cialis cheapest price[/url] [url=https://viagraonlinejc.com/]does viagra increase size[/url] [url=https://viagratx.com/]taking viagra for fun[/url] [url=https://buycialisxz.com/]how much is cialis per pill[/url]

Llpweack (not verified)

[url=https://60lmentop.com/]viagra vs cialis hardness[/url] [url=https://viamengo.com/]new viagra commercial actress name[/url] [url=https://didoviamen.com/]is viagra covered by medicare[/url]

Llpweack (not verified)

[url=https://60lmentop.com/]60lmentop.com[/url] [url=https://viamengo.com/]viamengo.com[/url] [url=https://didoviamen.com/]didoviamen.com[/url]

Jndqweack (not verified)

[url=https://50cialmen.com/]50cialmen.com[/url] [url=https://aazmencial.com/]aazmencial.com[/url] [url=https://joicialviaed.com/]joicialviaed.com[/url]

Fbshweack (not verified)

[url=https://thesisacloud.com/]thesisacloud.com[/url] thesis proposal writing service thesisacloud.com writing thesis

LecfReest (not verified)

[url=https://hopeviagrin.com/]hopeviagrin.com[/url] [url=https://lightvigra.com/]lightvigra.com[/url] [url=https://xuypharmacyonline.com/]xuypharmacyonline.com[/url] [url=https://online21rxon.com/]online21rxon.com[/url] [url=https://happyrxpharmacy21.com/]happyrxpharmacy21.com[/url] [url=https://storerxpharmcanada.com/]storerxpharmcanada.com[/url]

KedReest (not verified)

[url=https://qqllviagria.com/]qqllviagria.com[/url] [url=https://ssviagriia.com/]ssviagriia.com[/url] [url=https://canadianpharmnorx.com/]canadianpharmnorx.com[/url] [url=https://ggviagll.com/]ggviagll.com[/url] [url=https://viagerkr.com/]viagerkr.com[/url] [url=https://viagriyvik.com/]viagriyvik.com[/url]

Jndqweack (not verified)

[url=https://50cialmen.com/]50cialmen.com[/url] [url=https://aazmencial.com/]aazmencial.com[/url] [url=https://joicialviaed.com/]joicialviaed.com[/url]

Llpweack (not verified)

[url=https://60lmentop.com/]60lmentop.com[/url] [url=https://viamengo.com/]viamengo.com[/url] [url=https://didoviamen.com/]didoviamen.com[/url]

Add new comment