Home >PHP MYSQL
Setup PHP SSH2 Connection from php to remote box on pair networks
11/12/2007

1. Download modules

http://www.php.net/downloads.php

Download Win32 binaries and extract  php_ssh2.dll to the extensions directory

Edit the php.ini file that is being used to include this extension

extension=php_ssh2.dll

Generate a pub / private key pair on the server you wish to connect to

ssh-keygen -t rsa -C "username@boxname.pair.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/usr/home/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /usr/home/username/.ssh/id_rsa.
Your public key has been saved in /usr/home/username/.ssh/id_rsa.pub.

 Copy both the public and private key files to your local box.
Cut and paste the id_rsa.pub contents into authorized_keys2  file in /usr/home/username/.ssh - creating it if it does not exist.

Create a php program containing the following

<?php
/* Notify the user if the server terminates the connection */
function my_ssh_disconnect($reason, $message, $language) {
  printf("Server disconnected with reason code [%d] and message: %s\n",
         $reason, $message);
}

$methods = array(
  'kex' => 'diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1',
  'client_to_server' => array(
    'crypt' => '3des-cbc',
    'comp' => 'none'),
  'hostkey'=>'ssh-rsa',
  'server_to_client' => array(
    'crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc',
    'comp' => 'none'));

$callbacks = array('disconnect' => 'my_ssh_disconnect');

$connection = ssh2_connect('vepar.pair.com', 22, $methods, $callbacks);
if (!$connection) die('Connection failed');

/* Uncomment to secure even further, you want to get the known host key by getting $fingerprint the first time and then hard coding
$known_host = '4D1B79C97D996C16C3CA50FF2445208B';
$fingerprint = ssh2_fingerprint($connection,
               SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);

if ($fingerprint != $known_host) {
  die("HOSTKEY MISMATCH!\n" .
      "Possible Man-In-The-Middle Attack? $fingerprint");
}
*/

if (ssh2_auth_pubkey_file($connection, 'username',
                          'S:/sshphp/id_rsa.pub',
                          'S:/sshphp/id_rsa', '')) {
  echo "Public Key Authentication Successful\n";
} else {
  die('Public Key Authentication Failed');
}

?>



Date Title
11/12/2007 Setup PHP SSH2 Connection from php to remote box on pair networks
1. Download modules http://www.php.net/download
20/02/2006 Serialised Array Walk
This is an array_walk style serialize that works
February 2006 PHP AddSlashes to an Array
Say you have a $_POST array and and you don't know
04/01/2006 MYSQL Select time from a date
Consider you have a date field in mysql of format
 
     
   

site developed by silverarm solutions 10. php mysql development ireland