开发者

check connection with ssh2 functions in PHP

开发者 https://www.devze.com 2023-02-15 08:30 出处:网络
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

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! 开发者_Go百科


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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号