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.
精彩评论