1

So I have taken a example from php.net to see php ssh2 workings

so the code is

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

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

When I am putting wrong information it is giving me some error instead of telling me Authentication Failed.

The Error is

Warning: ssh2_connect(): php_network_getaddresses: getaddrinfo failed: Name not known in 
/var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/ssh2.php on line 2 Warning: ssh2_connect(): Unable to connect to 
shell.example.com on port 22 in /var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php on 
line 2 Warning: ssh2_connect(): Unable to connect to shell.example.com in 
/var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php on line 2  Warning: 
ssh2_auth_password() expects parameter 1 to be resource, boolean given in /var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php 
on line 4 Authentication Failed...

But when I am putting correct information it is not giving any error.

neubert
  • 15,947
  • 24
  • 120
  • 212

1 Answers1

4

The problem is that shell.example.com doesn't actually exist and the call to ssh2_connect() fails to return a resource.

Your code should be checking to see if ssh2_connect() successfully makes a connection and returns a resource before attempting to use the resource with ssh2_auth_password().

$connection = ssh2_connect('some.valid.ssh.host', 22);

if (!$connection) {
    throw new Exception("Could not connect to server.");
}

if (!ssh2_auth_password($connection, 'name', 'password')) {
    throw new Exception("Authentication failed!");
}

// SSH connection authenticated and ready to be used.
Trowski
  • 469
  • 5
  • 11
  • I think it is telling me the ip does not exit and that is the reason it giving me the problem because when I am giving a correct ip it is not giving me the error. –  Jul 26 '14 at 15:37
  • Exactly. Try using the code above with an incorrect host and you'll also get a much more helpful error message. – Trowski Jul 26 '14 at 15:40
  • No I used that code now only a blank page it does not say like Could not connect to server. –  Jul 26 '14 at 15:41
  • Well you have to catch the exception at some point if you don't want the request to just fail. If your code fails to connect, you'll want to display that on the page and of course skip whatever code was going to use that connection. – Trowski Jul 26 '14 at 15:48