note 97999 added to function.ssh2-auth-pubkey-file
deepakssn@gmailcom
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
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);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 144.160.130.16
----
Manual Page -- http://www.php.net/manual/en/function.ssh2-auth-pubkey-file.php
Edit -- https://master.php.net/note/edit/97999
Del: integrated -- https://master.php.net/note/delete/97999/integrated
Del: useless -- https://master.php.net/note/delete/97999/useless
Del: bad code -- https://master.php.net/note/delete/97999/bad+code
Del: spam -- https://master.php.net/note/delete/97999/spam
Del: non-english -- https://master.php.net/note/delete/97999/non-english
Del: in docs -- https://master.php.net/note/delete/97999/in+docs
Del: other reasons-- https://master.php.net/note/delete/97999
Reject -- https://master.php.net/note/reject/97999
Search -- https://master.php.net/manage/user-notes.php