Beispiel #1
0
    async def open(self) -> None:
        self._pre_open_closing_log(closing=False)

        try:
            fut = asyncio.open_connection(
                host=self._base_transport_args.host, port=self._base_transport_args.port
            )
            self.stdout, self.stdin = await asyncio.wait_for(
                fut, timeout=self._base_transport_args.timeout_socket
            )
        except ConnectionError as exc:
            msg = f"Failed to open telnet session to host {self._base_transport_args.host}"
            if "connection refused" in str(exc).lower():
                msg = (
                    f"Failed to open telnet session to host {self._base_transport_args.host}, "
                    "connection refused"
                )
            raise ScrapliConnectionError(msg) from exc
        except (OSError, socket.gaierror) as exc:
            msg = (
                f"Failed to open telnet session to host {self._base_transport_args.host} -- "
                "do you have a bad host/port?"
            )
            raise ScrapliConnectionError(msg) from exc
        except asyncio.TimeoutError as exc:
            msg = "timed out opening connection to device"
            self.logger.critical(msg)
            raise ScrapliAuthenticationFailed(msg) from exc

        await self._handle_control_chars()

        self._post_open_closing_log(closing=False)
Beispiel #2
0
 def read(self) -> bytes:
     if not self.session:
         raise ScrapliConnectionNotOpened
     try:
         buf = self.session.read_eager()
     except Exception as exc:
         raise ScrapliConnectionError(
             "encountered EOF reading from transport; typically means the device closed the "
             "connection") from exc
     return buf
Beispiel #3
0
 def read(self) -> bytes:
     if not self.session_channel:
         raise ScrapliConnectionNotOpened
     try:
         buf: bytes = self.session_channel.recv(65535)
     except Exception as exc:
         msg = (
             "encountered EOF reading from transport; typically means the device closed the "
             "connection")
         self.logger.critical(msg)
         raise ScrapliConnectionError(msg) from exc
     return buf
Beispiel #4
0
    async def open(self) -> None:
        self._pre_open_closing_log(closing=False)

        try:
            self.stdout, self.stdin = await asyncio.open_connection(
                host=self._base_transport_args.host,
                port=self._base_transport_args.port)
        except ConnectionError as exc:
            msg = f"Failed to open telnet session to host {self._base_transport_args.host}"
            if "connection refused" in str(exc).lower():
                msg = (
                    f"Failed to open telnet session to host {self._base_transport_args.host}, "
                    "connection refused")
            raise ScrapliConnectionError(msg) from exc
        except (OSError, socket.gaierror) as exc:
            msg = (
                f"Failed to open telnet session to host {self._base_transport_args.host} -- "
                "do you have a bad host/port?")
            raise ScrapliConnectionError(msg) from exc

        await self._handle_control_chars()

        self._post_open_closing_log(closing=False)
Beispiel #5
0
    async def read(self) -> bytes:
        if not self.stdout:
            raise ScrapliConnectionNotOpened

        try:
            buf: bytes = await self.stdout.read(65535)
        except ConnectionLost as exc:
            msg = (
                "encountered EOF reading from transport; typically means the device closed the "
                "connection"
            )
            self.logger.critical(msg)
            raise ScrapliConnectionError(msg) from exc

        return buf
Beispiel #6
0
    def open(self) -> None:
        self._pre_open_closing_log(closing=False)

        # establish session with "socket" timeout, then reset timeout to "transport" timeout
        try:
            self.session = ScrapliTelnet(
                host=self._base_transport_args.host,
                port=self._base_transport_args.port,
                timeout=self._base_transport_args.timeout_socket,
            )
            self.session.timeout = self._base_transport_args.timeout_transport
        except ConnectionError as exc:
            msg = f"Failed to open telnet session to host {self._base_transport_args.host}"
            if "connection refused" in str(exc).lower():
                msg = (
                    f"Failed to open telnet session to host {self._base_transport_args.host}, "
                    "connection refused")
            raise ScrapliConnectionError(msg) from exc

        self._post_open_closing_log(closing=False)
Beispiel #7
0
    async def read(self) -> bytes:
        if not self.stdout:
            raise ScrapliConnectionNotOpened

        if self._initial_buf:
            buf = self._initial_buf
            self._initial_buf = b""
            return buf

        try:
            buf = await self.stdout.read(65535)
            # nxos at least sends "binary transmission" control char, but seems to not (afaik?)
            # actually advertise it during the control protocol exchange, causing us to not be able
            # to "know" that it is in binary transmit mode until later... so we will just always
            # strip this option (b"\x00") out of the buffered data...
            buf = buf.replace(b"\x00", b"")
        except EOFError as exc:
            raise ScrapliConnectionError(
                "encountered EOF reading from transport; typically means the device closed the "
                "connection") from exc

        return buf