1

I have some project to do but killing myself and cannot do anything, i need you help . I am have to make PHP Command to use SSH Connection. I am using Putty , entering IP and Port, than browsing .PPK file ( key authentication file ) and Entering Username And Password, after using command Reboot . so server is rebooting.

Now i want to make all of this simply using PHP , i want to make a textbox and submit button , when entering IP address in textbox and clicking submit button it should make all this reboot command in background. (port is default 22). I will make this textboxes and buttons but cannot get how to make this connection with command and key authentication. if somebody know this please help me solving this problem. i cannot take it any more.

Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
user3185991
  • 13
  • 1
  • 5

1 Answers1

1

You must use phpseclib.

  • Download it from here or here ( direct link ) .

  • Include it to your project.

And then :

include('Net/SSH2.php');
$ssh = new Net_SSH2('www.example.com');
$ssh->login('username', 'password') or die("Login failed");
// $ssh->getServerPublicHostKey(); if this is your first time connecting
// check $ssh->getServerPublicHostKey() against previously saved 
//                                value on subsequent connections
echo $ssh->exec('reboot');
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
  • I have donwloaded PHPseclib from your link and uploaded on server . after created index.php with code : include('Net/SSH2.php'); $ssh = new Net_SSH2('IP address', 22); $ssh->login('myusername', 'mypass') or die("Login failed"); echo $ssh->exec('reboot'); after what to do it says login Failed . WHere i have to give Key File? – user3185991 Jan 28 '14 at 07:01
  • did you tried `$ssh = new Net_SSH2('IP address');` without port number ? – Alireza Fallah Jan 28 '14 at 07:06
  • not yet but i cannot understand where i have to put my PPK key file for authentication, without key auth it's not logging – user3185991 Jan 28 '14 at 07:11
  • what do you mean `without key auth it's not logging`, I dont think its necessary here . – Alireza Fallah Jan 28 '14 at 07:13
  • I mean that there is Private Key written in file. PuTTY-User-Key-File-2: ssh-rsa Encryption: aes256-cbc. Without loading this key putty is not connecting with server.this is secured connection so i need to load this key in case of logging – user3185991 Jan 28 '14 at 07:19
  • when you are connection to remote server in ssh, with PHP, it has nothing to do with `putty`, putty is just a software . by the way, I dont know, search about this subject, I just answered your question `ssh connection and command php`. good luck – Alireza Fallah Jan 28 '14 at 07:23
  • ahha thank you for this help , i Know that putty is a soft and i want to make the same connection web-based so the one thing was to use PHP – user3185991 Jan 28 '14 at 07:28
  • good luck , go [**here**](http://phpseclib.sourceforge.net) maybe you can find your answer . – Alireza Fallah Jan 28 '14 at 07:29
  • look at the update, I found it in [**here**](http://phpseclib.sourceforge.net/ssh/examples.html) – Alireza Fallah Jan 28 '14 at 07:58
  • 1
    You load the key by doing `$rsa = new Crypt_RSA(); $rsa->loadKey(file_get_contents('/path/to/key.ppk')); $ssh->login('username', $key);` – neubert Feb 03 '14 at 18:10