Domanda

I am trying to ssh from RHEL to SLES machine. I am using PHP function ssh2 to achieve that. Authentication is not happening even after passing correct username and password. But the same code is working fine for RHEL->RHEL. I am able to ssh to the SLES machine from RHEL terminal. But using ssh2 it is not happening.

    <?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at ip on port 22
if(!($con = ssh2_connect(ip, 22))){
    echo "fail: unable to establish connection\n";
} else {
    // try to authenticate with username root, password password
    if(!ssh2_auth_password($con, "root", "password")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";

        // execute a command
        if (!($stream = ssh2_exec($con, 'date' ))) {
            echo "fail: unable to execute command\n";
        } else {
            // collect returning data from command
        //echo "in else";
            stream_set_blocking($stream, true);
            $data = "";
            while ($buf = fread($stream,4096)) {
                $data .= $buf;
            }
        echo $data;
            fclose($stream);
        }
    }
    }
    ?>

I am getting the output "fail:unable to authenticate". Why is it happening that way? Any solution to it?

È stato utile?

Soluzione

It's possible the server is using keyboard-interactive authentication and not password authentication.

Really, libssh2 doesn't provide any good way for one to know that. My recommendation: try phpseclib, a pure PHP SSH2 implementation. eg.

<?php
include('Net/SSH2.php');

define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    echo $ssh->getLog();
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

By default it's $ssh->login() method will try password authentication but (and unlike libssh2) it'll try keyboard-interactive if password auth fails.

And if neither work the code snippet I posted will echo out the logs so you can see what type of input it is expecting. If those logs are posted if you could post them in your response so that people here may diagnose the issue that'd be great.

Thanks!

Altri suggerimenti

install libssh2-devel using YAST then do

pecl install ssh2-0.12

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top