albertopriore
albertopriore

Reputation: 614

check connection with ssh2 functions in PHP

I need to check the connection with the function http://php.net/manual/en/function.ssh2-connect.php

But if I made a check like this

$connection = ssh2_connect('myserver.com', 22);         
if (!$connection){
    echo 'no connection';
}else{
    echo 'connection ok';
}

I never get into the line " echo 'no connection'; "

Can you explain me why? And how to make a check like that?

Thanks in advance!

Upvotes: 1

Views: 18966

Answers (4)

Quang Quach
Quang Quach

Reputation: 168

Basically, you should use the functions like ssh2_auth_password, or ssh2_auth_public_key_file and combine with ssh2_connect function to check whether the connection is authenticated or not

    $connection = ssh2_connect("myserver.com", 22);

    if (ssh2_auth_password($connection, "username", "password")) {
      echo "connection is authenticated";
    }
    else {
      echo "failed!";
    }

Hope this helps!

edit: missing semicolon

Upvotes: 2

nevershown
nevershown

Reputation: 79

Yet another example of why phpseclib, a pure PHP SSH implementation, is better than PECL's ssh2 extension.

Upvotes: -1

Kendall Hopkins
Kendall Hopkins

Reputation: 44104

It looks like the 4th parameter $callbacks could yield some feedback on this problem.

The issue is likely due to inability to connect (Duh). This could be because a firewall, no internet, heavy security on SSH. Try calling # ssh -v <host> on PHP box to see what the problem could be.

Upvotes: 0

Vladislav Rastrusny
Vladislav Rastrusny

Reputation: 29975

It depends on your server. Try to connect to some non-existing one and see what happens.

If you need to check only connection, then you can use something like http://php.net/manual/en/function.fsockopen.php also

Upvotes: 1

Related Questions