Advertisement
Guest User

netmiko/hp/hp_procurvejinhua_ssh.py

a guest
Mar 23rd, 2018
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. ##file netmiko/hp/hp_procurvejinhua_ssh.py
  2.  
  3. from __future__ import print_function
  4. from __future__ import unicode_literals
  5. import re            
  6. import time
  7. import socket                                                
  8. from netmiko.cisco_base_connection import CiscoSSHConnection
  9. from netmiko import log                                                              
  10.                                                                  
  11.  
  12. class HPProcurveJinhuaSSH(CiscoSSHConnection):
  13.  
  14.     def session_preparation(self):
  15.         """
  16.        Prepare the session after the connection has been established.
  17.        Procurve uses - 'Press any key to continue'
  18.        """
  19.         delay_factor = self.select_delay_factor(delay_factor=0)
  20.         output = ""
  21.         count = 1
  22.         while count <= 30:
  23.             output += self.read_channel()
  24.             if 'any key to continue' in output:
  25.                 self.write_channel(self.RETURN)
  26.                 break
  27.             else:
  28.                 time.sleep(.33 * delay_factor)
  29.             count += 1
  30.  
  31.         # Try one last time to past "Press any key to continue
  32.         self.write_channel(self.RETURN)
  33.  
  34.         # HP output contains VT100 escape codes
  35.         self.ansi_escape_codes = True
  36.  
  37.         self._test_channel_read(pattern=r'[>#]')
  38.         self.set_base_prompt()
  39.         command = self.RETURN + "no page"
  40.         self.disable_paging(command=command)
  41.         self.set_terminal_width(command='terminal width 511')
  42.         # Clear the read buffer
  43.         time.sleep(.3 * self.global_delay_factor)
  44.         self.clear_buffer()
  45.  
  46.     def enable(self, cmd='_cmdline-mode on\nY\nJinhua1920unauthorized', pattern='password', re_flags=re.IGNORECASE,
  47.                default_username='manager'):
  48.         """Enter enable mode"""
  49.         output = self.send_command_timing(cmd)
  50.         log.debug("{}".format(output))
  51.         self.clear_buffer()
  52.         return output
  53.  
  54.     def cleanup(self):
  55.         """Gracefully exit the SSH session."""
  56.         self.exit_config_mode()
  57.         self.write_channel("logout" + self.RETURN)
  58.         count = 0
  59.         while count <= 5:
  60.             time.sleep(.5)
  61.             output = self.read_channel()
  62.             if 'Do you want to log out' in output:
  63.                 self.write_channel("y" + self.RETURN)
  64.             # Don't automatically save the config (user's responsibility)
  65.             elif 'Do you want to save the current' in output:
  66.                 self.write_channel("n" + self.RETURN)
  67.             try:
  68.                 self.write_channel(self.RETURN)
  69.             except socket.error:
  70.                 break
  71.             count += 1
  72.  
  73.     def save_config(self, cmd='write memory', confirm=False):
  74.         """Save Config."""
  75.         return super(HPProcurveJinhuaSSH, self).save_config(cmd=cmd, confirm=confirm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement