I recently posted a question titled “ssh2_auth_pubkey_file: Can't Get To Work”. I found a solution and posted an answer.
The PHP file in that article was invoked from the command line. This time I’m trying (unsuccessfully) to invoke it from a web form. The file is mostly the same, and both files I’m using are at the same level on the same server. The following message appears in /var/log/apache2/error.log
*…PHP Warning: ssh2_auth_pubkey_file(): Authentication failed for <my username> using public key: Unable to open public key file in /var/www/html/test.php on line 12, referer: http://<my server’s IP#>/test.htm *
I’m guessing the problem has to do with owners or permissions somewhere, but I can’t get that worked out. Anyone have an idea of why authentication fails from web page invoking? Thanks for any assistance. Code from files ….
Simple HTML form - test.htm.
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Test</title>
</head>
<body>
<form method="post" action="test.php">
<p><button type="submit" >Submit</button> <button type="reset" >Reset</button></p>
</form>
</body>
</html>
Simple PHP file - test.php
<?php
$host = "receiving server IP";
$port = 22;
$conn = ssh2_connect($host, $port);
$username = "my username";
$pub_key = "/home/my username/.ssh/id_rsa.pub";
$pri_key = "/home/my username/.ssh/id_rsa";
if(ssh2_auth_pubkey_file(
$conn,
$username,
$pub_key,
$pri_key))
{
echo "Authentication succeeded";
}
else
{
echo "Authentication failed";
}
// eventual scp code here to send file to receiving server.
?>
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.