luqasz/librouteros

Can Not Login Connection Reset By Peer

Closed this issue · 4 comments

Hey,

I was using librouter os to connect my Mikrotik via API and it was working fine until today. My Mikrotik version is 6.43.8, and as you know login is changed for API versions of 6.43 and later. I saw that you already created a function for it called 'login_plain'. I tried to force it and used only login_plain function to login but it did not work as well. Creating the socket part is working fine but as far as I see there is something wrong with receiving the response data.

Thank you in the advance

What version of librouteros are you using ?
Please provide full debug output from librouteros.

It's the latest version, 2.3.0. 'Read' function in 'connections.py' catches the SOCKET_ERROR and raises ConnectionError. Here is the full error message.

`Failed to read from socket. [Errno 54] Connection reset by peer
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/connections.py", line 195, in read
    data += self.sock.recv((length - len(data)))
ConnectionResetError: [Errno 54] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "x.py", line 107, in <module>)
  File "x.py", line 21, in __init__
    self.client = connect(username=username, password=password, host=host, timeout=None, login_methods=login_plain)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/__init__.py", line 47, in connect
    arguments['login_methods'](api=api, username=username, password=password)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/login.py", line 25, in login_plain
    api('/login', **{'name': username, 'password': password})
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/api.py", line 80, in __call__
    return self.rawCmd(cmd, *words)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/api.py", line 92, in rawCmd
    return self._readResponse()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/api.py", line 115, in _readResponse
    reply_word, words = self._readSentence()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/api.py", line 100, in _readSentence
    reply_word, words = self.protocol.readSentence()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/connections.py", line 147, in readSentence
    sentence = tuple(word for word in iter(self.readWord, b'\x00'))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/connections.py", line 147, in <genexpr>
    sentence = tuple(word for word in iter(self.readWord, b'\x00'))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/connections.py", line 157, in readWord
    length = self.transport.read(1)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/librouteros/connections.py", line 203, in read
    raise ConnectionError('Failed to read from socket. ' + str(error))
librouteros.exceptions.ConnectionError: Failed to read from socket. [Errno 54] Connection reset by peer`

This is a socket error directly related to underlying connection. Something had to change in your network or something else since this error shows.

Okay, so this was related to Mikrotik's API Available IP blocks. It was an unexpected behaviour since it was working fine for 3 months and all of a sudden it refused to connect. I checked the available IP's and my local network IP was not on the list (On Winbox go to IP -> Services -> Double Click on the api -> Check the 'Available From' list)