The documentation on ssh2_auth_password()
says:
Returns TRUE on success or FALSE on failure.
So you can just check if it returns false
if(ssh2_auth_password($ssh, $user, $pass) === false) {
//do stuff if it can't login
}
While you're doing that, you may also want to add some error checking to the ssh2_connect()
call, as it can return a non-resource if it was unable to connect.
$ssh = ssh2_connect($ip, 22);
if($ssh === false) {
//do stuff if it can't connect
}
Edit for comment
If you'd like to have it fallback on another login attempt, you can do it a couple of ways. If you'll just have one more set of credentials, you can simply duplicate the if
inside of itself.
//try the first login
if(ssh2_auth_password($ssh, $user, $pass) === false) {
//try the second set
if(ssh2_auth_password($ssh, $user2, $pass2) === false) {
//do stuff if it can't login
}
}
Alternatively, if you have a large number of credentials to try, you can store them in an array and loop over it
$creds = array(
array('user' => $user, 'pass' => $pass),
array('user' => $user2, 'pass' => $pass2),
//etc
);
$logged_in = false;
foreach($creds as $cred) {
if(ssh2_auth_password($ssh, $cred['user'], $cred['pass'])) {
$logged_in = true;
break;
}
}
if(!$logged_in) {
//do stuff when you can't login
}