suggestions on how to speed up switch login code?

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

suggestions on how to speed up switch login code?

Postby chubbypama » Wed Nov 13, 2013 7:10 pm

Hi there.
I'm using phpseclib's ssh class to connect to an HP procurve switch. I'm noticing that it takes over 2.5 seconds to load the classes, and then connect to the switch. Having said that, the HP switch has a banner enabled on it and so I had to change from using terminal mode to ansi... which seems to be a bit more chatty as far as reading / writing to the device.
I'm wondering why it takes so long to load the classes... and then also to see if there's a way to speed up the actual connection logic. I may be doing extra stuff that's not necessary.

Here's what the logic looks like:
Code: Select all
   public function connect()
   {
$ctime_start = microtime(true);

       $ssh = new Net_SSH2($this->_hostname);
       $ansi = new File_ANSI();
$ctime_end = microtime(true);
$ctime = $ctime_end - $ctime_start;   
echo "total time to load classes:".$ctime."<BR>";

$ctime_start = microtime(true);
       if (!$ssh->login($this->_username, $this->_password)) { //if you can't log on...
            $this->_emess = "Error: Authentication Failed for $this->_hostname";
            return false;      
       }
       else  {
            //banner changes terminal mode to ansi vs. vt100
                  
            $ansi->appendString($ssh->read('/([0-9A-Z\-\=])*(#)(\s*)/i', NET_SSH2_READ_REGEX) );
            $ssh->write("\n");   
            $ansi->appendString($ssh->read('/([0-9A-Z\-\=])*(#)(\s*)/i', NET_SSH2_READ_REGEX) );
            $prompt=htmlspecialchars_decode(strip_tags($ansi->getHistory()));

            if (!$prompt) {
                $this->_emess = "Error: Problem communication with $this->_hostname";
                return false;                  
            }
            else {
               $ssh->write("conf t\n");
               //$ssh->setTimeout(5);
               $ansi->appendString($ssh->read('/([0-9A-Z\-\=])*(#)(\s*)/i', NET_SSH2_READ_REGEX) );
               $ssh->write("console local-terminal ansi\n");
               $ansi->appendString($ssh->read('/([0-9A-Z\-\=])*(#)(\s*)/i', NET_SSH2_READ_REGEX) );
               $ssh->write("exit\n");
               //$ssh->setTimeout(5);
               $ansi->appendString($ssh->read('/([0-9A-Z\-\=])*(#)(\s*)/i', NET_SSH2_READ_REGEX) );
               $this->_connection = $ssh;
               $this->_ansi = $ansi;
$ctime_end = microtime(true);
$ctime = $ctime_end - $ctime_start;   
echo "total switch connect time :".$ctime."<BR>";         
               return true;   
            }
       }         
   } // connect


The output looks like the following:

Code: Select all
total time to load classes :1.6060111522675
total switch connect time :1.2516870498657


Thanks in advance for your time and help.
chubbypama
Traveler
 
Posts: 27
Joined: Thu Jul 19, 2012 6:54 pm

Re: suggestions on how to speed up switch login code?

Postby TerraFrost » Thu Nov 14, 2013 4:02 pm

Having said that, the HP switch has a banner enabled on it and so I had to change from using terminal mode to ansi...

I'm not completely sure what that means lol.

Can you post the banner?

Also, could you post (or email me) the SSH logs?

My suspicion is that there's not much that can be done. The time to load the class is more accurately described as the time to open and initiate an SSH connection (before auth). If fsockopen() takes 1.5 seconds to connect, for instance, then that's how long it takes and there isn't a ton that can be done about that.

My guess with the "total switch connect time" is that that's just how long it takes for the switch to process the commands.

The logs will tell us for sure how long each packet takes to be sent / received.

Thanks!
TerraFrost
Legendary Guard
 
Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

Re: suggestions on how to speed up switch login code?

Postby chubbypama » Fri Nov 15, 2013 1:32 pm

Hey there Terra Frost! I will fire off an email to you shortly with the logs attached.
Thanks.
chubbypama
Traveler
 
Posts: 27
Joined: Thu Jul 19, 2012 6:54 pm


Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 1 guest

cron