Exemplo n.º 1
0
 async def _establish_connection(self):
     """Establishing SSH connection to the network device"""
     logger.info(
         "Host {}: Establishing connection to port {}".format(self._host, self._port)
     )
     output = ""
     # initiate SSH connection
     fut = asyncssh.connect(**self._connect_params_dict)
     try:
         self._conn = await asyncio.wait_for(fut, self._timeout)
     except asyncssh.DisconnectError as e:
         raise DisconnectError(self._host, e.code, e.reason)
     except asyncio.TimeoutError:
         raise TimeoutError(self._host)
     self._stdin, self._stdout, self._stderr = await self._conn.open_session(
         term_type="Dumb", term_size=(200, 24)
     )
     logger.info("Host {}: Connection is established".format(self._host))
     # Flush unnecessary data
     delimiters = map(re.escape, type(self)._delimiter_list)
     delimiters = r"|".join(delimiters)
     output = await self._read_until_pattern(delimiters)
     logger.debug(
         "Host {}: Establish Connection Output: {}".format(self._host, repr(output))
     )
     return output
Exemplo n.º 2
0
 async def _read_until_pattern(self, pattern='', re_flags=0):
     """Read channel until pattern detected. Return ALL data available"""
     output = ''
     logger.info("Host {}: Reading until pattern".format(self._host))
     if not pattern:
         pattern = self._base_pattern
     logger.debug("Host {}: Reading pattern: {}".format(self._host, pattern))
     while True:
         fut = self._stdout.read(self._MAX_BUFFER)
         try:
             output += await asyncio.wait_for(fut, self._timeout)
         except asyncio.TimeoutError:
             raise TimeoutError(self._host)
         if re.search(pattern, output, flags=re_flags):
             logger.debug("Host {}: Reading pattern '{}' was found: {}".format(self._host, pattern, repr(output)))
             return output
Exemplo n.º 3
0
 async def _read_until_prompt_or_pattern(self, pattern="", re_flags=0):
     """Read until either self.base_pattern or pattern is detected. Return ALL data available"""
     output = ""
     logger.info("Host {}: Reading until prompt or pattern".format(
         self._host))
     if not pattern:
         pattern = self._base_pattern
     base_prompt_pattern = self._base_pattern
     while True:
         fut = self._stdout.read(self._MAX_BUFFER)
         try:
             output += await asyncio.wait_for(fut, self._timeout)
         except asyncio.TimeoutError:
             raise TimeoutError(self._host)
         if re.search(pattern, output, flags=re_flags) or re.search(
                 base_prompt_pattern, output, flags=re_flags):
             logger.debug(
                 "Host {}: Reading pattern '{}' or '{}' was found: {}".
                 format(self._host, pattern, base_prompt_pattern,
                        repr(output)))
             return output