def __clear_telnet_port(self, console_ip, port): """ This is the function which will clear console port There is special functionality which will detect and update the pwd of the telnet console if user has given a wrong one. It will try only nbv123, lab, cisco123 as these are the common ones used in lab and also since we have only 3 tries. """ logging.info("Clearing console with ip=%s and ports=%s", console_ip, port) pwdList = ['cisco123', 'lab', 'nbv123'] pwdList.remove(self.console_pwd) pwdTry = 0 console = pexpect.spawn('telnet %s'%(console_ip)) console.logfile = self.log i = console.expect([pexpect.TIMEOUT, pexpect.EOF, r'Bad', r'(?i)incorrect', PWD_PROMPT, CONSOLE_PROMPT, EN_CONSOLE_PROMPT], 5) while i >= 0: if i == 0: console.close() raise TimeoutError('Clear Console Timeout error') if i == 1: console.close() raise EofError('Clear Console EOF error') if i == 2 or i == 3: console.close() raise PasswordError('Clear Console, Password error') if i == 4: logging.info("pwd %s", self.console_pwd) if pwdTry == 0: console.sendline(self.console_pwd) elif pwdTry > 0 and pwdTry <= len(pwdList): console.sendline(pwdList[pwdTry - 1]) self.console_pwd = pwdList[pwdTry-1] else: console.close() raise PasswordError('Clear Console, Password error') pwdTry = pwdTry + 1 if i == 5: logging.info("console prompt") Utils.update_console_login_details(self.switch_name, self.console_user, self.console_pwd) console.sendline('en') console.expect(PWD_PROMPT) console.sendline(self.console_pwd) if i == 6: logging.info("en console prompt") Utils.update_console_login_details(self.switch_name, self.console_user, self.console_pwd) break i = console.expect([pexpect.TIMEOUT, pexpect.EOF, r'Bad', r'(?i)incorrect', PWD_PROMPT, CONSOLE_PROMPT, EN_CONSOLE_PROMPT], 5) po = int(port)%100 console.sendline('clear line %d'%(po)) console.sendline('\r') console.expect('confirm') console.sendline('\r') console.expect(EN_CONSOLE_PROMPT) console.sendline('exit') time.sleep(1) console.close() return
def connect_mgmt_ip(self, using): """ This definition will telnet/ssh to the active sup mgmt ip provided in the switch object (Default is ssh) [IMP]Telnet handle and needs to be closed by client Eg: testuser[13:00:00]> ssh [email protected] or testuser[13:00:00]> telnet -l admin 111.111.111.111 """ if using == 'ssh': logging.info('Trying to ssh to mgmt ip %s', self.mgmt_ip) Utils.remove_known_hosts(self.mgmt_ip) console = pexpect.spawn('ssh admin@%s' % (self.mgmt_ip)) else: logging.info('Trying to telnet to mgmt ip %s', self.mgmt_ip) console = pexpect.spawn('telnet -l admin %s' % (self.mgmt_ip)) console.logfile = self.log i = console.expect([pexpect.TIMEOUT, pexpect.EOF, LOGIN_INCORRECT, \ AUTH_ISSUE, LOADER_PROMPT, BOOT_PROMPT, SSH_NEWKEY, \ PWD_PROMPT], 5) while i >= 0: if i == 0: console.close() logging.info( 'connect_mgmt_ip, Timed out, Not able to access mgmt for (%s)', self.switch_name) raise TimeoutError( 'connect_mgmt_ip, Timed out, Not able to access mgmt') if i == 1: console.close() logging.info( 'connect_mgmt_ip, Eof error, Not able to access mgmt for (%s)', self.switch_name) raise EofError( 'connect_mgmt_ip, Eof error, Not able to access mgmt') if i == 2 or i == 3: console.close() logging.info('connect_mgmt_ip, Password error') raise PasswordError('connect_mgmt_ip, Password error') if i == 4 or i == 5: console.close() logging.info('connect_mgmt_ip, Switch in loader/boot prompt') raise LoaderError( 'connect_mgmt_ip, Switch in loader/boot prompt') if i == 6: console.sendline('yes') if i == 7: console.sendline(self.switch_pwd) if i == 8: break i = console.expect([pexpect.TIMEOUT, pexpect.EOF, LOGIN_INCORRECT, \ AUTH_ISSUE, LOADER_PROMPT, BOOT_PROMPT, SSH_NEWKEY, \ PWD_PROMPT, SWITCH_PROMPT], 5) return console
def get_switch_module_details(self): """ This function will retrieve the "show module | xml" details into a xml format which inturn will be returned to the caller """ logging.info("Logging into switch %s to collect details", self.switch_name) xml = {} output = None xml['mgmt_issue'] = False xml['telnet_issue'] = False xml['inv'] = "" xml['uptime'] = "" xml['idletime'] = "" try: output = self.get_switch_details_from_mgmt("ssh") except (TimeoutError, EofError): xml['mgmt_issue'] = True #Always Clear the Consoles while working on the console self.clear_console() self.clear_screen() #Check which port is active one if self.stnd_port: cons_ip = [self.console_ip, self.stnd_console_ip] ports = [self.act_port, self.stnd_port] checkpo0 = self.check_standby(cons_ip[0], ports[0]) checkpo1 = self.check_standby(cons_ip[1], ports[1]) if not (checkpo0) and not (checkpo1): self.stnd_console_ip, self.stnd_port = cons_ip[1], ports[1] self.console_ip, self.act_port = cons_ip[0], ports[0] if checkpo0: self.stnd_console_ip, self.stnd_port = cons_ip[0], ports[0] self.console_ip, self.act_port = cons_ip[1], ports[1] if checkpo1: self.stnd_console_ip, self.stnd_port = cons_ip[1], ports[1] self.console_ip, self.act_port = cons_ip[0], ports[0] Utils.update_console_ip_and_ports(self.switch_name, self.console_ip, \ self.act_port, self.stnd_console_ip, self.stnd_port) try: output = self.get_switch_details_from_console() except BootingError: try: console = self.load_image_using_telnet() console.close() output = self.get_switch_details_from_mgmt("ssh") except (TimeoutError, EofError): xml['telnet_issue'] = True raise TimeoutError('Could not telnet nor ssh') except (TimeoutError, EofError): xml['telnet_issue'] = True raise TimeoutError('Could not telnet nor ssh') xml['clock'],xml['idletime'],xml['inv'],xml['uptime'] = \ collections.OrderedDict(sorted(output.items())).values() return xml
def get_switch_module_details(self): """ This function will retrieve the "show module | xml" details into a xml format which inturn will be returned to the caller """ logging.info("Logging into switch %s to collect details", self.switch_name) xml = {} output = None xml['mgmt_issue'] = False xml['telnet_issue'] = False xml['inv'] = "" xml['uptime'] = "" xml['idletime'] = "" try: output = self.get_switch_details_from_mgmt("ssh") except (TimeoutError, EofError): xml['mgmt_issue'] = True #Always Clear the Consoles while working on the console self.clear_console() self.clear_screen() #Check which port is active one if self.stnd_port: cons_ip = [self.console_ip, self.stnd_console_ip] ports = [self.act_port, self.stnd_port] checkpo0 = self.check_standby(cons_ip[0], ports[0]) checkpo1 = self.check_standby(cons_ip[1], ports[1]) if not(checkpo0) and not(checkpo1): self.stnd_console_ip, self.stnd_port = cons_ip[1],ports[1] self.console_ip,self.act_port = cons_ip[0],ports[0] if checkpo0: self.stnd_console_ip, self.stnd_port = cons_ip[0],ports[0] self.console_ip,self.act_port = cons_ip[1],ports[1] if checkpo1: self.stnd_console_ip, self.stnd_port = cons_ip[1],ports[1] self.console_ip,self.act_port = cons_ip[0],ports[0] Utils.update_console_ip_and_ports(self.switch_name, self.console_ip, \ self.act_port, self.stnd_console_ip, self.stnd_port) try: output = self.get_switch_details_from_console() except BootingError: try: console = self.load_image_using_telnet() console.close() output = self.get_switch_details_from_mgmt("ssh") except (TimeoutError, EofError): xml['telnet_issue'] = True raise TimeoutError('Could not telnet nor ssh') except (TimeoutError, EofError): xml['telnet_issue'] = True raise TimeoutError('Could not telnet nor ssh') xml['clock'],xml['idletime'],xml['inv'],xml['uptime'] = \ collections.OrderedDict(sorted(output.items())).values() return xml
def connect_mgmt_ip(self, using): """ This definition will telnet/ssh to the active sup mgmt ip provided in the switch object (Default is ssh) [IMP]Telnet handle and needs to be closed by client Eg: testuser[13:00:00]> ssh [email protected] or testuser[13:00:00]> telnet -l admin 111.111.111.111 """ if using == 'ssh': logging.info('Trying to ssh to mgmt ip %s', self.mgmt_ip) Utils.remove_known_hosts(self.mgmt_ip) console = pexpect.spawn('ssh admin@%s' % (self.mgmt_ip)) else: logging.info('Trying to telnet to mgmt ip %s', self.mgmt_ip) console = pexpect.spawn('telnet -l admin %s' % (self.mgmt_ip)) console.logfile = self.log i = console.expect([pexpect.TIMEOUT, pexpect.EOF, LOGIN_INCORRECT, \ AUTH_ISSUE, LOADER_PROMPT, BOOT_PROMPT, SSH_NEWKEY, \ PWD_PROMPT], 5) while i >= 0: if i == 0: console.close() logging.info('connect_mgmt_ip, Timed out, Not able to access mgmt for (%s)', self.switch_name) raise TimeoutError('connect_mgmt_ip, Timed out, Not able to access mgmt') if i == 1: console.close() logging.info('connect_mgmt_ip, Eof error, Not able to access mgmt for (%s)', self.switch_name) raise EofError('connect_mgmt_ip, Eof error, Not able to access mgmt') if i == 2 or i == 3: console.close() logging.info('connect_mgmt_ip, Password error') raise PasswordError('connect_mgmt_ip, Password error') if i == 4 or i == 5: console.close() logging.info('connect_mgmt_ip, Switch in loader/boot prompt') raise LoaderError('connect_mgmt_ip, Switch in loader/boot prompt') if i == 6: console.sendline('yes') if i == 7: console.sendline(self.switch_pwd) if i == 8: break i = console.expect([pexpect.TIMEOUT, pexpect.EOF, LOGIN_INCORRECT, \ AUTH_ISSUE, LOADER_PROMPT, BOOT_PROMPT, SSH_NEWKEY, \ PWD_PROMPT, SWITCH_PROMPT], 5) return console
search_types = prompt(u'Get Details for DUTs of type [n9k, fretta, n7k, xbow, n6k, n5k]: ').split(',') using = prompt(u'Get Details using ssh only [y|n]: ') count = 0 for s_type in search_types: for switch in Switches.select().where(Switches.switch_type==str(s_type)).dicts(): count = count + 1 switchId = str(switch['id']) swUtils.switch_details.delay(switchId, fout, str(using).lower()) print "Collected all the Current Details of %s switches" % count if whattodo == 2: username = prompt(u'Username: '******'Password: '******'Query for switch_type (n9k,fretta,n7k,xbow,n6k,n5k): ') if not Utils.check_user(username, pwd): print "Username/Password Entered is wrong" else: print "\nSending Mail to users with telnet/mgmt issue\n" swUtils.send_telnet_issue_mail(str(swtype)) if whattodo == 3: username = prompt(u'Username: '******'Password: '******'Query for switch_type (n9k,fretta,n7k,xbow,n6k,n5k): ') if not Utils.check_user(username, pwd): print "Username/Password Entered is wrong" else: print "\nSending Mail to users with password issue\n" swUtils.send_pwd_issue_mail(str(swtype))
def ascii_copy(self, switch_cons): """ This method is to copy the running confing to run_power_config and startup config to start_power_config. Then save both the files to remote server. This will even store the kickstart/system image loaded to the same remote server which will be loaded while reloading the setup. """ kick = sys = "" foundk = founds = 0 switch_cons.sendline('switchback') switch_cons.expect(SWITCH_PROMPT) #For storing the kickstart and the system images switch_cons.sendline('show version | grep "kickstart image file is"') switch_cons.expect(SWITCH_PROMPT) string = switch_cons.before if "///" in string: kick = string.split('///')[1] kick = kick.split()[0] switch_cons.sendline('show version | grep "system image file is"') switch_cons.expect(SWITCH_PROMPT) string = switch_cons.before if "///" in string: sys = string.split('///')[1] sys = sys.split()[0] if kick != "" and sys != "": check_kick = 'dir | grep "%s"' % kick check_sys = 'dir | grep "%s"' % sys switch_cons.sendline(check_kick) switch_cons.expect(SWITCH_PROMPT) dirk = switch_cons.before imgsk = dirk.split() if kick in imgsk: foundk = 1 switch_cons.sendline(check_sys) switch_cons.expect(SWITCH_PROMPT) dirsy = switch_cons.before imgss = dirsy.split() if sys in imgss: founds = 1 logging.info("Found kick %d and found sys %d", foundk, founds) if foundk==founds==1: Utils.update_images(self.switch_name, kick, sys) else: Utils.update_images(self.switch_name,"","") #Now write erase and copy the running config to file switch_cons.sendline('delete run_power_config n') switch_cons.expect(SWITCH_PROMPT, 60) switch_cons.sendline('delete start_power_config n') switch_cons.expect(SWITCH_PROMPT, 60) #no boot kick and sys switch_cons.sendline('config t') switch_cons.expect(SWITCH_PROMPT) switch_cons.sendline('no boot kick') switch_cons.expect(SWITCH_PROMPT, 120) switch_cons.sendline('config t') switch_cons.expect(SWITCH_PROMPT) switch_cons.sendline('no boot sys') switch_cons.expect(SWITCH_PROMPT, 120) #write erase switch_cons.sendline('write erase') switch_cons.expect('Do you wish to proceed anyway') switch_cons.sendline('y') switch_cons.expect(SWITCH_PROMPT, 120) #write erase boot switch_cons.sendline('write erase boot') switch_cons.expect('Do you wish to proceed anyway') switch_cons.sendline('y') switch_cons.expect(SWITCH_PROMPT, 120) Switch.setip(switch_cons, self.mgmt_ip) #Copy the kickstart and system image to server only if present if foundk==founds==1: d_file = "%s/%s.kickstart.gbin"%(self.switch_name,self.switch_name) Switch.copy_files_to_server(switch_cons=switch_cons,s_file=kick,d_file=d_file) d_file = "%s/%s.system.gbin"%(self.switch_name,self.switch_name) Switch.copy_files_to_server(switch_cons=switch_cons,s_file=sys,d_file=d_file) "Now copy the running config to run_power_config file" switch_cons.sendline('show running-config vdc-all > run_power_config') i = switch_cons.expect([SWITCH_PROMPT,r'yes/no',pexpect.TIMEOUT, pexpect.EOF], 600) if i==0: pass if i==1: switch_cons.sendline('yes') switch_cons.expect(SWITCH_PROMPT,600) if i==2 or i==3: logging.info("Something wrong with switch %s",self.switch_name) return False d_file = "%s/%s.run_power_config"%(self.switch_name,self.switch_name) Switch.copy_files_to_server(switch_cons=switch_cons,s_file='run_power_config',d_file=d_file) "Now copy the startup config to run_power_config file" switch_cons.sendline('show startup-config vdc-all > start_power_config') i = switch_cons.expect([SWITCH_PROMPT,r'yes/no',pexpect.TIMEOUT, pexpect.EOF]) if i==0: pass if i==1: switch_cons.sendline('yes') switch_cons.expect(SWITCH_PROMPT,180) if i==2 or i==3: logging.info("Something wrong with switch %s",self.switch_name) return False d_file = "%s/%s.start_power_config"%(self.switch_name,self.switch_name) Switch.copy_files_to_server(switch_cons=switch_cons,s_file='start_power_config',d_file=d_file) return True