Connecting to droplet pragmatically via ssh2_auth_pubkey_file
I am using the DigitalOcean API to create Droplets with the Wordpress Ubuntu Image preinstalled - when I create the Droplet I need to activate the Wordpress install by logging in via SSH.
I want a fully automated process where I can create a full install without any manual work. The DO API stuff is straightforward and working but I am having an issue connecting to my droplet via ssh2authpubkey_file.
The steps I have taken:
Generated a key via PuTTY Key Generator
Added the public key to my DO account under ‘Settings’
Exported a OpenSSH Key via PuTTY Key Generator
I have a test setup using XAMPP on Windows - current code is:
$host = “xxx.xx.xxxx”;
$port = 22;
$conn = ssh2connect($host, $port);
$username = “root”;
$pubkey = “/etc/ssh/sshhostrsakey.pub”;
$prikey = “idrsa”;
echo “Authentication succeeded”;
echo “Authentication failed”;
In the same folder I have the private key I generated from PuTTY Key Generator in a file called id_rsa.
I get the following error when trying to connect:
Warning: ssh2authpubkey_file(): Authentication failed for root using public key: Username/PublicKey combination invalid in /var/www/html/test/test.php on line 8
In the id_rsa file I have:
—–BEGIN RSA PRIVATE KEY—–
—–END RSA PRIVATE KEY—–
Can anyone see a reason why this isn’t working? It should in theory be pretty straight forward.
P.S. I have also tried saving the public key locally into a file called idrsa.pub and using that in the $pubkey variable.
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.×