简体   繁体   中英

unable to connect using SSH2

I am using phpseclib in my project. I have installed it and am testing it using the following piece of code which is similar to the example here .

Updated Code:

1           require("Net/SSH2.php");
2   
3           if (!class_exists('Net_SSH2')) die("Class Net_SSH2 doesn't exist!");
4           
5           $ssh = new Net_SSH2('***');   // the host IP address 
6           
7           if (!method_exists($ssh, 'Net_SSH2')) die("Net_SSH2 class doesn't have Net_SSH2 method!");
8    
9           if (!$ssh->login('***', '***')) {   // (verified) username and password
10              echo "LOG: " . $ssh->getLog() . "<br>";
11              exit('Login failed!');
12          }

Code Update 2:

1       require("Net/SSH2.php");
2
3       define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
4
5       if (!class_exists('Net_SSH2')) die("Class Net_SSH2 doesn't exist!");
6       
7       $ssh = new Net_SSH2('***');   // the host IP address 
8       
9       if (!method_exists($ssh, 'Net_SSH2')) die("Net_SSH2 class doesn't have Net_SSH2 method!");
10
11      if (!$ssh->login('***', '***')) {   // (verified) username and password
12          echo "LOG: " . $ssh->getLog() . "<br>";
13          echo "ALL ERRORS: ";
14          print_r($ssh->getErrors());
15          echo "<br>LAST ERROR: " . $ssh->getLastError() . "<br>";
16          exit('Login failed!');
17      }

It exits at line # 16. getLog() gives nothing so it is hard to know where the problem is. I had the login credentials tested using PuTTY. It is not able to login to other servers either. I am not able to understand what is causing the problem.

Any help would be highly appreciated!

Net_SSH2 is not a function, this is a class and constructor within this class. So 3rd line should be

if (!class_exists('Net_SSH2')) die("Class Net_SSH2 doesn't exist!");

If you wanna check further if Net_SSH2 method exists in class Net_SSH2 after line 5 you can add this:

if (!method_exists($ssh, 'Net_SSH2')) die("Net_SSH2 class doesn't have Net_SSH2 method");

If you hit the "Enable Logging" button on the link you provided you'll see that the following is done right after Net/SSH2.php is included:

define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

Try that.

Try doing this at the top of your PHP script:

error_reporting(E_USER_NOTICE);

Setting error_reporting to 0 suppresses user_error so it's quite possible your PHP setup is just suppressing the error. eg.

error_reporting(E_USER_NOTICE);

user_error('zzzz', E_USER_NOTICE);

vs.

error_reporting(0);

user_error('zzzz', E_USER_NOTICE);

Of course you could change your line 3 to:

if( !class_exists('Net_SSH2')...

But I would suggest you throw in some Exception handling. When using the PHPSecLib, I had nothing but issues, so I created this SSH2 Class which does require the PHP SSH2 Extension to be installed.

You should try calling: getLog() to find out why it's failing.

echo $ssh->getLog();

I ended up hosting my code on the server (server B) that I was trying to connect. The same code worked in the opposite direction (I could connect to the server A where my code previously was)! I ended up concluding that the server A has environmental issues. I appreciate those were helping me here!

Given the latest comments, I assume your code exists at line 8 (where the Net_SSH2() object is instantiated)...

I just had a very similar issue where, once deployed, include() would work but check_exists() and instantiating a new Net_SSH2 object would fail... But parsing my php file (before deployment) with the php command line tool would work fine...

So the problem was on the server (Apache in my case): the directory where phpseclib was installed had permissions set so that I could not even cd to it.

As it happened, my hoem made deploying script was simply copying the files on the server and setting odd permissions so that the SSH2.php was not accessible.

Perhaps you have a similar problem here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM