note 97999 modified in function.ssh2-auth-pubkey-file by danbrown
| From: | danbrown@php.net | Date: | Thu, 20 May 2010 15:49:10 +0000 |
| Subject: | note 97999 modified in function.ssh2-auth-pubkey-file by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169463@lists.php.net to get a copy of this message | ||
Setting up public key authentication:
1. Login to the unix server you want to connect using putty.
2. mkdir .ssh (there is a dot before ssh)
3. cd .ssh
4. ssh-keygen -t rsa mykey
5. Enter passphrase as test
6. ls -al -> you will find two files mykey , mykey.pub
7. cat mykey.pub >>authorized_keys
8. cat mykey
9. Copy what you get on screen to notepad and save it as "c:\mykey" (within quotes)
10. cat mykey.pub
11. Copy what you get on screen to notepad and save it as "c:\mykey.pub" (within quotes)
<?php
function my_ssh_disconnect($reason, $message, $language)
{
printf("Server disconnected with reason code [%d] and message: %s\n", $reason,
$message);
}
//Open a connection forcing 3des-cbc when sending packets, any strength aes cipher when receiving
packets, no compression in either direction, and Group1 key exchange.
$methods = array(
'kex' => 'diffie-hellman-group1-sha1',
'client_to_server' => array(
'crypt' => 'aes256-cbc',
'comp' => 'none',
'mac' => 'hmac-sha1'),
'server_to_client' => array(
'crypt' => 'aes256-cbc',
'comp' => 'none',
'mac' => 'hmac-sha1'));
$callbacks = array('disconnect' => 'my_ssh_disconnect');
// Function to run a command on the unix server
function run_cmd($ssh_host, $user_name, $keyfilename, $ssh_command)
{
$connection = ssh2_connect($ssh_host, 22, $methods, $callbacks);
if (!$connection) die('Connection failed');
if (ssh2_auth_pubkey_file($connection, $user_name, $keyfilename.".pub",
$keyfilename, 'test'))
{
echo "Public Key Authentication Successful as user: $user_name";
}
else
{
die('Public Key Authentication Failed');
}
$stream = ssh2_exec($connection, $ssh_command);
$i=0;
stream_set_blocking($stream, true);
$line = stream_get_line($stream, 1024, "\n");
while (!feof($stream))
{
echo $line.' ';
$line = stream_get_line($stream, 1024, "\n");
$i++;
}
echo "Count : ".$i;
flush();
unset($stream);
}
function my_ssh_disconnect($reason, $message, $language)
{
printf("Server disconnected with reason code [%d] and message: %s\n",
$reason, $message);
}
// Main Code
$user_name = "USERID";
$keydir = "c:\\";
$search_string = 'needle';
$keyfilename= $keydir.'mykey';
$ssh_host = "foo.bar.com";
$ssh_command = 'grep "'.$search_string.'" /haystack/*.log';
run_cmd($ssh_host, $user_name, $keyfilename, $ssh_command);
$ssh_command = 'ls -al';
run_cmd($ssh_host, $user_name, $keyfilename, $ssh_command);
?>
--was--
Setting up public key authentication:
1. Login to the unix server you want to connect using putty.
2. mkdir .ssh (there is a dot before ssh)
3. cd .ssh
4. ssh-keygen -t rsa mykey
5. Enter passphrase as test
6. ls -al -> you will find two files mykey , mykey.pub
7. cat mykey.pub >>authorized_keys
8. cat mykey
9. Copy what you get on screen to notepad and save it as "c:\mykey" (within quotes)
10. cat mykey.pub
11. Copy what you get on screen to notepad and save it as "c:\mykey.pub" (within quotes)
PHP Code:
function my_ssh_disconnect($reason, $message, $language)
{
printf("Server disconnected with reason code [%d] and message: %s\n", $reason,
$message);
}
//Open a connection forcing 3des-cbc when sending packets, any strength aes cipher when receiving
packets, no compression in either direction, and Group1 key exchange.
$methods = array(
'kex' => 'diffie-hellman-group1-sha1',
'client_to_server' => array(
'crypt' => 'aes256-cbc',
'comp' => 'none',
'mac' => 'hmac-sha1'),
'server_to_client' => array(
'crypt' => 'aes256-cbc',
'comp' => 'none',
'mac' => 'hmac-sha1'));
$callbacks = array('disconnect' => 'my_ssh_disconnect');
// Function to run a command on the unix server
function run_cmd($ssh_host, $user_name, $keyfilename, $ssh_command)
{
$connection = ssh2_connect($ssh_host, 22, $methods, $callbacks);
if (!$connection) die('Connection failed');
if (ssh2_auth_pubkey_file($connection, $user_name, $keyfilename.".pub",
$keyfilename, 'test'))
{
echo "Public Key Authentication Successful as user: $user_name";
}
else
{
die('Public Key Authentication Failed');
}
$stream = ssh2_exec($connection, $ssh_command);
$i=0;
stream_set_blocking($stream, true);
$line = stream_get_line($stream, 1024, "\n");
while (!feof($stream))
{
echo $line.' ';
$line = stream_get_line($stream, 1024, "\n");
$i++;
}
echo "Count : ".$i;
flush();
unset($stream);
}
function my_ssh_disconnect($reason, $message, $language)
{
printf("Server disconnected with reason code [%d] and message: %s\n",
$reason, $message);
}
// Main Code
$user_name = "USERID";
$keydir = "c:\\";
$search_string = 'needle';
$keyfilename= $keydir.'mykey';
$ssh_host = "foo.bar.com";
$ssh_command = 'grep "'.$search_string.'" /haystack/*.log';
run_cmd($ssh_host, $user_name, $keyfilename, $ssh_command);
$ssh_command = 'ls -al';
run_cmd($ssh_host, $user_name, $keyfilename, $ssh_command);
?>
http://php.net/manual/en/function.ssh2-auth-pubkey-file.php