简体   繁体   中英

How to using phpseclib on laravel 5 php

I'm making an application based laravel that allows it to communicate with a linux server via SSH, I usually use phpseclib to communicate with servers such as rebooting and others, but when I build the application with laravel I could not or would rather not see the way how to integrate with laravel I tried to include manual of php but still could not error Class 'phpseclib\\Net\\ Net_SSH2' not found, my current code

    public function store(Request $request)
{
    // create ssh account and inserted into database here
    if(Auth::check()) // make sure user has been logging on
    {
      if($dump = DB::table('servers')->where('key', $request->_key)->get())
      {
        if($prices = DB::table('app_data')->get())
        {
          $price = $prices[0]->prices;
          if(DB::table('ssh_users')->where('name', $request->sshname)->where('on_server', $dump[0]->ip)->count() > 0 )
          {
            return view('create')->with('userexist', $request->sshname);
          }
          else
          {
            include(app_path() . "/lib/phpseclib/phpseclib/phpseclib/Net/SSH2.php");
            $command = new \phpseclib\Net\Net_SSH2($dump[0]->ip);
            $valid = array(
              'sshuser'     => $request->sshname,
              'sshpass'     => $request->sshpass,
              'sshcreated'  => date('d/m/Y'),
              'sshexpired'  => $request->sshexpired,
              'onserver'    => $dump[0]->name,
              'serveruser'  => $dump[0]->user,
              'serverpass'  => $dump[0]->password,
              'sshprice'    => $price,
              'command'     => $command
            );
            if(DB::table('ssh_users')->insert([
              'name'        => $request->sshname,
              'password'    => $request->sshpass,
              'created_at'  => date('d/m/Y'),
              'expired_on'  => $request->sshexpired,
              'on_server'   => $dump[0]->ip,
              'reseller'    => Auth::user()->name
            ])) {
              return view('create')->with('valid', $valid);
            }
          }
        }
        else
        {
          return view('create')->with('error', $request->sshname);
        }
      }
      else
      {
        return view('create')->with('serverabort', $request->sshname);
      }
    }
}

Solution 1:

Check /lib/phpseclib/phpseclib/phpseclib/Net/SSH2.php and note what class name is used, it may be SSH2 , and not Net_SSH2 (pre PSR-4 namespacing).

Then update the line, eg to:

$command = new \\phpseclib\\Net\\SSH2($dump[0]->ip);

Solution 2:

Install phpseclib as a composer package

composer require phpseclib/phpseclib

Then it will be autoloaded and you won't need the include()

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