diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/mikrotikapi-bf.py b/mikrotikapi-bf.py index 9a153d5..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: @@ -262,7 +264,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