Skip to content

Commit

Permalink
git - Merge pull request #4 from DinoTools/librouteros_workaround
Browse files Browse the repository at this point in the history
Workaround for librouteros
  • Loading branch information
phibos authored Dec 6, 2024
2 parents 583614a + a392cdf commit b007c12
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions routeros_log_exporter/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from queue import Queue

from librouteros import connect
from librouteros.api import Api as RouterOSApi

from .exception import ConfigError
from .output import Output
from .routeros import RouterOSApi

logger = logging.getLogger("fetcher")

Expand Down Expand Up @@ -40,7 +40,7 @@ def __init__(
ssl_verify_hostname: bool = True
):
super().__init__()
self._api = None
self._api: Optional[RouterOSApi] = None
self._message_queue = message_queue

self.hostname = hostname
Expand Down Expand Up @@ -163,7 +163,8 @@ def api(self) -> RouterOSApi:
username=self.username,
password=self.password,
ssl_wrapper=ssl_ctx.wrap_socket,
port=self.port
port=self.port,
subclass=RouterOSApi,
)
logger.info("Connected")
return self._api
Expand Down
31 changes: 31 additions & 0 deletions routeros_log_exporter/routeros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-FileCopyrightText: PhiBo DinoTools (2024)
# SPDX-License-Identifier: GPL-3.0-or-later

from librouteros.api import Api
from librouteros.exceptions import TrapError, MultiTrapError
from librouteros.types import (
ResponseIter
)


class RouterOSApi(Api):
def readResponse(self) -> ResponseIter:
"""
Yield each sentence until !done is received.
:throws TrapError: If one !trap is received.
:throws MultiTrapError: If > 1 !trap is received.
"""
traps = []
reply_word = None
while reply_word != "!done":
reply_word, words = self.readSentence()
if reply_word == "!trap":
traps.append(TrapError(**words))
elif reply_word in ("!re", "!done") and words:
yield words

if len(traps) > 1:
raise MultiTrapError(*traps)
if len(traps) == 1:
raise traps[0]

0 comments on commit b007c12

Please sign in to comment.