Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Secure Shell2 Functions : ssh2_auth_pubkey_file

ssh2_auth_pubkey_file

Authenticate using a public key (PECL ssh2:0.10-0.9)
bool ssh2_auth_pubkey_file ( resource session, string username, string pubkeyfile, string privkeyfile [, string passphrase] )

Example 2346. Authentication using a public key

<?php
$connection
= ssh2_connect('shell.example.com', 22, array('hostkey'=>'ssh-rsa'));

if (
ssh2_auth_pubkey_file($connection, 'username',
                         
'/home/username/.ssh/id_rsa.pub',
                         
'/home/username/.ssh/id_rsa', 'secret')) {
 echo
"Public Key Authentication Successful\n";
} else {
 die(
'Public Key Authentication Failed');
}
?>

Code Examples / Notes » ssh2_auth_pubkey_file

tekiedude

This is probably incredibly insecure but if you are in a closed environment, you can run it at your own risk.
I was trying to get ssh access from server A to server B but server A was running apache as 'apache' and
I needed files on server B that were owned by root. So I needed some way for the apache user to connect
to server B as root.  Here's what I did (both servers running linux - specifically CentOS4):
1. ssh-keygen -t rsa -f id_rsa  generate with no passphrase
2. append the id_rsa.pub file to server B /root/.ssh/authorized_keys2
3. copy the id_rsa and id_rsa.pub files to a directory like /var/www/.ssh/
(happens to be apache's home dir on CentOS)
4. chown -R apache.apache /var/www/.ssh
Then you can connect like this:
$connection = ssh2_connect($server,22,array('hostkey'=>'ssh-rsa'));
if (ssh2_auth_pubkey_file($connection,'root',
'/var/www/.ssh/id_rsa.pub',
'/var/www/.ssh/id_rsa')) {
echo "success!";
}
else
{
echo "no success :-(";
}


d23d23

The public key must be on one line starting with the key type, 1 space and followed by the keydata (no newlines) and not followed by comments. This is a limitation of libssh2, so remove any excess data from the file after creating it with your key generation tools.
So it would look something like this:
ssh-rsa <keydata>


andyc

The key files need to be in OpenSSH's format.
For example the pubkey file should look like this:
ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAo6uBphcbl
5iS7U0fjE1ZJ8ZPqYwezFbxyN13YYucrFDCbln9Q6S33lN
JMYKUY4711qzDOxQD0bratDQfHUl4waT35qI+NRQa2
B+UWHYaOhbt5FI0Ks7JXPxj3iJ/+joiKQW3Bj8eH8Nzg
sTx4tEVPD6QOHs682OhUNX6sqSnHA8= rsa-key-20060606


Change Language


Follow Navioo On Twitter
ssh2_auth_hostbased_file
ssh2_auth_none
ssh2_auth_password
ssh2_auth_pubkey_file
ssh2_connect
ssh2_exec
ssh2_fetch_stream
ssh2_fingerprint
ssh2_methods_negotiated
ssh2_publickey_add
ssh2_publickey_init
ssh2_publickey_list
ssh2_publickey_remove
ssh2_scp_recv
ssh2_scp_send
ssh2_sftp_lstat
ssh2_sftp_mkdir
ssh2_sftp_readlink
ssh2_sftp_realpath
ssh2_sftp_rename
ssh2_sftp_rmdir
ssh2_sftp_stat
ssh2_sftp_symlink
ssh2_sftp_unlink
ssh2_sftp
ssh2_shell
ssh2_tunnel
eXTReMe Tracker