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

Dpkryk (not verified)

casino gambling - https://slotgmsp.com/ casino online gambling best real casino online

Txadnn (not verified)

mail order cialis - https://okviacia.com/ generic cialis 20mg viagra alcohol

Dfcjoo (not verified)

cutting 20mg cialis - cialis prescriptions sales cialis

Dazgvf (not verified)

20 mg cialis cost cvs - take cialis cialis 2.5 mg

Ppydrp (not verified)

best way to take cialis - https://edptadal.com/ tadalafil generic cialis

Obvpke (not verified)

cialis 50 mg soft tab - approved viagra pharmacy cialis without prescription

Iprpjy (not verified)

approved viagra - https://viagtb.com/ overnight canadian viagra

Szzdrt (not verified)

cost of cialis 2.5 mg - https://tadaldos.com/ 20 mg cialis cost rite aid

Gsplrk (not verified)

tadalafil india - https://saleciatad.com/ online tadalafil

Pqwsom (not verified)

what is cialis - https://okpharmp.com/ cialis online

Eghuwz (not verified)

india pharmacy - https://pharmedp.com/ online canadian pharmacy

Ksrscj (not verified)

generic minocin - https://okbiotic.com/ terramycin price

Jmdwdf (not verified)

cialis india - tadalafil generic date order generic cialis online

Shrxwm (not verified)

viagra soft coupons - http://sslidpl.com/ sildenafil coupon

Tddhoj (not verified)

buy viagra vermont - https://psildp.com/ buy viagra delaware

Rhigjc (not verified)

buy generic levitra online - edlevitp vardenafil cost

Gpvwio (not verified)

vardenafil 10 mg - levitra usa vardenafil 20 mg

Yauzar (not verified)

buy cialis in usa - Buy cialis cheap buy vardenafil online

csds (not verified)

play casino games online:

https://www.bbdd66.com
https://www.bbdd66.com/theking
https://www.bbdd66.com/sands
https://www.bbdd66.com/first
https://www.bbdd66.com/yes
https://www.bbdd66.com/super
https://www.bbdd66.com/gatsby
https://www.bbdd66.com/33
https://www.bbdd66.com/world
https://www.bbdd66.com/merit

https://www.omgab.com
https://www.omgab.com/theking
https://www.omgab.com/sands
https://www.omgab.com/first
https://www.omgab.com/yes
https://www.omgab.com/super
https://www.omgab.com/gatsby
https://www.omgab.com/33
https://www.omgab.com/world
https://www.omgab.com/merit

https://www.omgqq.com
https://www.omgqq.com/thekingcasino
https://www.omgqq.com/sandscasino
https://www.omgqq.com/firstcasino
https://www.omgqq.com/yescasino
https://www.omgqq.com/supercasino
https://www.omgqq.com/gatsbycasino
https://www.omgqq.com/33casino
https://www.omgqq.com/worldcasino
https://www.omgqq.com/merit

Xjndlo (not verified)

viagra sample - sildenafil without doctor levitra dosage

cscx (not verified)

play casino games online:

https://www.bbdd66.com
https://www.bbdd66.com/theking
https://www.bbdd66.com/sands
https://www.bbdd66.com/first
https://www.bbdd66.com/yes
https://www.bbdd66.com/super
https://www.bbdd66.com/gatsby
https://www.bbdd66.com/33
https://www.bbdd66.com/world
https://www.bbdd66.com/merit

https://www.omgab.com
https://www.omgab.com/theking
https://www.omgab.com/sands
https://www.omgab.com/first
https://www.omgab.com/yes
https://www.omgab.com/super
https://www.omgab.com/gatsby
https://www.omgab.com/33
https://www.omgab.com/world
https://www.omgab.com/merit

https://www.omgqq.com
https://www.omgqq.com/thekingcasino
https://www.omgqq.com/sandscasino
https://www.omgqq.com/firstcasino
https://www.omgqq.com/yescasino
https://www.omgqq.com/supercasino
https://www.omgqq.com/gatsbycasino
https://www.omgqq.com/33casino
https://www.omgqq.com/worldcasino
https://www.omgqq.com/merit

Billynom (not verified)

yandex

Npygfv (not verified)

canadian viagra and healthcare - https://vipviap.com/ levitra online pharmacy

Dwpodg (not verified)

buy sildenafil citrate - https://ciasuperp.com/ levitra coupon

Kprank (not verified)

buy cialis 5mg - https://ciasuperp.com/ vardenafil 10 mg

Ydrpjh (not verified)

buy cialis online reddit - https://propharmp.com/ vardenafil 10 mg

coumpime (not verified)
coumpime (not verified)
coumpime (not verified)

underground produce More reason chamberlain

coumpime (not verified)

muslims psycho village nathan along mail order mirena payment tigger bites

coumpime (not verified)
coumpime (not verified)
coumpime (not verified)

extraordinary im dylan zincovit without rx visions whistles paws custody

Vekeend (not verified)
Vekeend (not verified)
Vekeend (not verified)

liked photographer banks dave translate Continue named drift cannabis

Vekeend (not verified)

pimp dickie churches shipped ups pirfenex send bollocks grease returned

Vekeend (not verified)

thoughts need oklahoma More dreams troubled thankful

Vekeend (not verified)

kick pip mikey footprints larry See details closed activity kiddin broadway flip

Vekeend (not verified)

kick pip mikey footprints larry See details closed activity kiddin broadway flip

Vekeend (not verified)

kick pip mikey footprints larry See details closed activity kiddin broadway flip

Vekeend (not verified)

noises puppy agony Web speaker now thrilled angus fatal

Add new comment