Class 'Net_SSH2' not found in... when using phpseclib

Wesley Smith picture Wesley Smith · May 18, 2015 · Viewed 7.6k times · Source

I'm trying to use phpseclib to run a few ssh commands on my server. Attempting the below, I get Class 'Net_SSH2' not found in.... which seems to indicate phpseclib is not properly loaded, yet the echo shows that the include is working.

What have I done wrong here?

My php

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

$thepath = $_SERVER['DOCUMENT_ROOT']. '/inc/phpseclib/Net/SSH2.php';
echo '<strong>Full include path:</strong> '. $thepath;
include ($thepath); 


$included_files = get_included_files();

foreach ($included_files as $filename) {
     echo "<br><strong>Included file:</strong>$filename";
}


$ssh = new Net_SSH2('xx.xx.x.xx.x'); // server's ip address
if (!$ssh->login('username', 'password')) { // filled in correctly 
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
Done.

The output

Full include path: /var/chroot/home/content/08/555555555/html/inc/phpseclib/Net/SSH2.php
Included file:/home/content/08/555555555/html/TeamBoard/deploy.php
Included file:/home/content/08/555555555/html/inc/phpseclib/Net/SSH2.php
Fatal error: Class 'Net_SSH2' not found in /home/content/08/555555555/html/TeamBoard/deploy.php on line 25

Answer

neubert picture neubert · May 18, 2015

On github.com there are two branches (in addition to the master branch) - 1.0 and 2.0. 2.0 is namespaced so to call that you'd need to do \phpseclib\Net\SSH2.

If you downloaded the zip file from phpseclib.sourceforge.net then you're running the 1.0 branch. If that's what you're running you'll need to update the include path. eg.

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

If you're running the 2.0 branch (or master branch) you'll need to use an auto loader. Example:

<?php
// https://raw.githubusercontent.com/composer/composer/master/src/Composer/Autoload/ClassLoader.php

include('autoloader.php');

$loader = new \Composer\Autoload\ClassLoader();
$loader->addPsr4('phpseclib\\', __DIR__.'/phpseclib');
$loader->register();

use \phpseclib\Crypt\RSA;

$rsa = new RSA();