Generate Public / Private Key
<?phpinclude('Crypt/RSA.php');
$rsa = new Crypt_RSA();
//$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_OPENSSH);
$rsa->createKey(2048);
extract($rsa->createKey()); // == $rsa->createKey(1024) where 1024 is the key size
echo "<pre>";
echo $privatekey;
echo "<br>";
echo "\n\n\n";
echo $publickey;
echo "</pre>";
?>
// psadmin (.ssh) Folder
[psadmin@localhost ~]$ ls -la
drwx------. 3 psadmin psadmin 44 Jun 6 23:06 .ssh
[psadmin@localhost ~]$ ls -la .ssh
drwx------. 3 psadmin psadmin 44 Jun 6 23:06 .
drwx------. 6 psadmin psadmin 198 Jun 6 22:54 ..
-rw------- 1 psadmin psadmin 237 Jun 6 22:54 authorized_keys
Type 1:
Using library http://phpseclib.sourceforge.net/
include('Net/SSH2.php');
include('Crypt/RSA.php');
$ssh = new Net_SSH2('192.168.0.14');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('C:/xampp/htdocs/ssh/ssh-privatekey.txt'));
if (!$ssh->login('psadmin', $key)) {
exit('Login Failed');
}
echo "<pre>";
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
echo "</pre>";
?>
Type 2: using ssh2 in XAMPP
<?php
$connection = ssh2_connect('192.168.0.14', 22, array('hostkey'=>'ssh-rsa'));
if (ssh2_auth_pubkey_file($connection, 'psadmin',
'C:/xampp/htdocs/ssh/ssh-publickey.txt',
'C:/xampp/htdocs/ssh/ssh-privatekey.txt', NULL)) {
echo "Public Key Authentication Successful\n";
} else {
die('Public Key Authentication Failed');
}
$stream = ssh2_exec($connection, 'ls -ltar');
stream_set_blocking($stream,TRUE);
$stream_out = ssh2_fetch_stream($stream,SSH2_STREAM_STDIO);
echo '<pre>';
echo stream_get_contents($stream_out);
echo '</pre>';
?>
Thanks
Zafrulla Khan