ssh2_auth_password

(PECL)

ssh2_auth_password --  Authenticate over SSH using a plain password

Description

bool ssh2_auth_password ( resource session, string username, string password )

Authenticate over SSH using a plain password

例子 1. Authenticating with a password

<?php
$connection
= ssh2_connect('shell.example.com', 22);

if (
ssh2_auth_password($connection, 'username', 'secret')) {
  echo
"Authentication Successful!\n";
} else {
  die(
'Authentication Failed...');
}
?>

add a note add a note User Contributed Notes
noels01 at gmx dot net
30-Aug-2005 02:34
Hey, the start for a PEAR SSH2 package! The code is available under the GNU GPL ;-)
<?php

class SSH2
{
  
private $con;
  
private $host;
  
private $port;
  
private $login;
  
  
public function __construct($host=NULL, $port=22)
   {
      
$this->host = $host;
      
$this->port = $port;

      
$this->con = ssh2_connect($this->host, $this->port);
      
// connection attempt successful ?
      
if($this->con === false)
       {
          
$this->con = NULL;
          
throw new SSH2Exception("Could not connect to host '".$this->host."' on port ".$this->port);
       }
   }
  
  
public function login($login=NULL, $password=NULL)
   {
       if(empty(
$login)) {
          
throw new IllegalArgumentException("Login failed, no username supplied");
       }
      
$this->login = $login;

      
// try to log in
      
if(!ssh2_auth_password($this->con, $this->login, $password))
       {
          
$this->con = NULL;
          
throw new SSH2Exception("Password authentication failed for user '".$this->login."'");
       }
   }
  
  
// returns an array like: array('stdout goes here', 'stderr')
  
public function exec($cmd)
   {
       if(empty(
$this->con)) {
          
throw new SSH2Exception("Exec failed, no connection available");
       }
      
// execute the command
      
$stdio  = ssh2_exec($this->con, $cmd);
      
// get the error stream, otherwise this would be lost
      
$stderr = ssh2_fetch_stream ($stdio, SSH2_STREAM_STDERR);
      
// set to blocking mode or we won't get any data
      
stream_set_blocking($stdio, true);
      
      
$data_std = '';
      
$data_err = '';
      
// let's fetch the result of our command
      
while($data = fread($stdio, 131072)) {
      
//while($data = fgets($stdio)) {
          
$data_std .= $data;
       }
       while(
$data = fread($stderr, 80000)) {
          
$data_err .= $data;
       }
      
// close the streams
      
fclose($stdio);
      
fclose($stderr);
      
       return array(
$data_std, $data_err);
   }
}

class
SSH2Exception extends Exception {}
class
IllegalArgumentException extends Exception {}

?>
noels01 at gmx dot net
29-Aug-2005 08:56
Do not try to authenticate or log in more than once on a ssh2 connection. It won't work. You'll need a new connection via ssh2_connect() which will result in a poor performance if you're doing several connects to the same server.
06-Jun-2005 10:49
The sshd you are trying to connect to requires PasswordAuthentication yes in its sshd_config file.

Most normal SSH features work with this off, but this requires it on. Note, this defaults to off in FreeBSD 5.x