From 0a7573f221498ba926a58398f99717cfaff5610b Mon Sep 17 00:00:00 2001 From: Dmitriusan Date: Tue, 24 Jan 2023 20:49:55 +0200 Subject: [PATCH 1/3] Fix "list index out of range" error --- mikrotikapi-bf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mikrotikapi-bf.py b/mikrotikapi-bf.py index 9a153d5..ee90d89 100644 --- a/mikrotikapi-bf.py +++ b/mikrotikapi-bf.py @@ -262,7 +262,7 @@ def read_sentence(): # Everything will be appended to paragraph variable, and then returned. paragraph = [] received_sentence = [''] - while received_sentence[0] != '!done': + while received_sentence and received_sentence[0] != '!done': received_sentence = read_sentence() paragraph.append(received_sentence) self.status = paragraph From fea6dbd9fc04f27abd9fa6cb742478d51eeab25d Mon Sep 17 00:00:00 2001 From: Dmitriusan Date: Tue, 24 Jan 2023 23:08:30 +0200 Subject: [PATCH 2/3] add retries on socket timeout --- mikrotikapi-bf.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/mikrotikapi-bf.py b/mikrotikapi-bf.py index ee90d89..943b631 100644 --- a/mikrotikapi-bf.py +++ b/mikrotikapi-bf.py @@ -127,19 +127,21 @@ def open_socket(self): self.sock = socket.socket(af, socket.SOCK_STREAM) self.sock.settimeout(5) # Set socket timeout to 5 seconds, default is None - try: - # Trying to connect to RouterOS, error can occur if IP target is not reachable, or API is blocked in - # RouterOS firewall or ip services, or port is wrong. - self.connection = self.sock.connect(sa) - - except (socket.timeout): - print("[-] SOCKET TIMEOUT! Target timed out! Exiting...") - self.close() - sys.exit(1) - - except OSError: - print("[-] SOCKET ERROR! Check Target (IP or PORT parameters). Exiting...") - raise CreateSocketError('Error: API failed to connect to socket. Host: {}, port: {}.'.format(self.target, self.port)) + connected = False + while not connected: + try: + # Trying to connect to RouterOS, error can occur if IP target is not reachable, or API is blocked in + # RouterOS firewall or ip services, or port is wrong. + self.connection = self.sock.connect(sa) + connected = True + + except (socket.timeout): + print("[-] SOCKET TIMEOUT! Target timed out!") + time.sleep(60) + + except OSError: + print("[-] SOCKET ERROR! Check Target (IP or PORT parameters). Exiting...") + raise CreateSocketError('Error: API failed to connect to socket. Host: {}, port: {}.'.format(self.target, self.port)) # if self.use_ssl: # try: From aef8ccf395558c1e49f39b4e9d12e6b15882e8b9 Mon Sep 17 00:00:00 2001 From: Dmitriusan Date: Thu, 2 Feb 2023 22:32:01 +0200 Subject: [PATCH 3/3] add gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/