Exemplo n.º 1
0
 def login(self):
     """Login method.
     Creates a login session for the program to send commands to the target device.
     """
     result = {'status': False, 'errLog': ''}
     # sshv2(ip,username,password,timeout,port=22)
     sshChannel = sshv2(self.ip, self.username, self.password, self.timeout,
                        self.port)
     if sshChannel['status']:
         # Login succeed, init shell
         try:
             result['status'] = True
             self._channel = sshChannel['content']
             # resize virtual console window size to 10000*10000
             self.shell = self._channel.invoke_shell(width=10000,
                                                     height=10000)
             self.channel = self.shell
             tmpBuffer = ''
             while (not re.search(
                     self.basePrompt,
                     tmpBuffer.split('\n')[-1])) and (not re.search(
                         '(new +password)|(password.*change)',
                         tmpBuffer.split('\n')[-1],
                         flags=re.IGNORECASE)):
                 tmpBuffer += self.shell.recv(1024)
             # if prompt is 'New Password' ,raise Error.
             if re.search('(new +password)|(password.*change)',
                          tmpBuffer.split('\n')[-1],
                          flags=re.IGNORECASE):
                 raise ForwardError(
                     '[Login Error]: %s: Password expired, needed to be updated!'
                     % self.ip)
             self.shell.settimeout(self.timeout)
             # Record login status to True.
             self.isLogin = True
             self.getPrompt()
         except Exception as e:
             result['status'] = False
             result['errLog'] = str(e)
     else:
         # Login failed
         self.isLogin = False
         result['errLog'] = sshChannel['errLog']
     return result
Exemplo n.º 2
0
 def login(self):
     """Login method.
     Creates a login session for the program to send commands to the target device.
     """
     result = {
         'status': False,
         'errLog': ''
     }
     # sshv2(ip,username,password,timeout,port=22)
     sshChannel = sshv2(self.ip, self.username, self.password, self.timeout, self.port)
     if sshChannel['status']:
         # Login succeed, init shell
         try:
             result['status'] = True
             self._channel = sshChannel['content']
             # resize virtual console window size to 10000*10000
             self.shell = self._channel.invoke_shell(width=10000, height=10000)
             self.channel = self.shell
             tmpBuffer = ''
             while (
                 not re.search(self.basePrompt, tmpBuffer.split('\n')[-1])
             ) and (
                 not re.search('new +password', tmpBuffer.split('\n')[-1], flags=re.IGNORECASE)
             ):
                 tmpBuffer += self.shell.recv(1024)
             # if prompt is 'New Password' ,raise Error.
             if re.search('new +password', tmpBuffer.split('\n')[-1], flags=re.IGNORECASE):
                 raise ForwardError(
                     '[Login Error]: %s: Password expired, needed to be updated!' % self.ip
                 )
             self.shell.settimeout(self.timeout)
             # Record login status to True.
             self.isLogin = True
             self.getPrompt()
         except Exception as e:
             result['status'] = False
             result['errLog'] = str(e)
     else:
         # Login failed
         self.isLogin = False
         result['errLog'] = sshChannel['errLog']
     return result
Exemplo n.º 3
0
 def login(self):
     """Because the device of this model is different from the other
     huawei devices in commit  parameters, it is rewritten.
     """
     njInfo = {"status": False,
               "content": "",
               "errLog": ""}
     # sshv2(ip,username,password,timeout,port=22)
     sshChannel = sshv2(self.ip, self.username, self.password, self.timeout, self.port)
     if sshChannel['status']:
         # Login succeed, init shell
         try:
             njInfo['status'] = True
             self.channel = sshChannel['content']
             # resize virtual console window size to 10000*10000
             self.shell = self.channel.invoke_shell(width=1000, height=1000)
             tmpBuffer = ''
             while not re.search(self.basePrompt, tmpBuffer.split('\n')[-1]):
                 tmpBuffer += self.shell.recv(1024)
                 # When prompted, reply N
                 if re.search('\[Y/N\]:', tmpBuffer):
                     self.shell.send('N\n')
             # set session timeout
             self.shell.settimeout(self.timeout)
             # Flag login status is True.
             self.isLogin = True
             # Get host prompt.
             self.getPrompt()
             njInfo["status"] = True
         except Exception as e:
             njInfo['status'] = False
             njInfo['errLog'] = str(e)
     else:
         # Login failed
         njInfo['errLog'] = sshChannel['errLog']
     return njInfo
Exemplo n.º 4
0
 def login(self):
     """Because the device of this model is different from the other
     huawei devices in commit  parameters, it is rewritten.
     """
     njInfo = {"status": False, "content": "", "errLog": ""}
     # sshv2(ip,username,password,timeout,port=22)
     sshChannel = sshv2(self.ip, self.username, self.password, self.timeout,
                        self.port)
     if sshChannel['status']:
         # Login succeed, init shell
         try:
             njInfo['status'] = True
             self.channel = sshChannel['content']
             # resize virtual console window size to 10000*10000
             self.shell = self.channel.invoke_shell(width=1000, height=1000)
             tmpBuffer = ''
             while not re.search(self.basePrompt,
                                 tmpBuffer.split('\n')[-1]):
                 tmpBuffer += self.shell.recv(1024).decode()
                 # When prompted, reply N
                 if re.search('\[Y/N\]:', tmpBuffer):
                     self.shell.send('N\n')
             # set session timeout
             self.shell.settimeout(self.timeout)
             # Flag login status is True.
             self.isLogin = True
             # Get host prompt.
             self.getPrompt()
             njInfo["status"] = True
         except Exception as e:
             njInfo['status'] = False
             njInfo['errLog'] = str(e)
     else:
         # Login failed
         njInfo['errLog'] = sshChannel['errLog']
     return njInfo