aker-gateway / Aker

SSH bastion/jump host/jumpserver
Other
566 stars 71 forks source link

Unhandled exception in SSHclient.py #98

Closed pewo closed 4 years ago

pewo commented 4 years ago

When trying to connect to down server. The connection terminates with an python exception.

Traceback (most recent call last):
  File "/usr/bin/aker/aker.py", line 174, in <module>
    Aker().build_tui()
  File "/usr/bin/aker/aker.py", line 136, in build_tui
    self.tui.start()
  File "/usr/bin/aker/tui.py", line 349, in start
    self.loop.run()
  File "/usr/lib64/python2.7/site-packages/urwid/main_loop.py", line 271, in run
    self.screen.run_wrapper(self._run)
  File "/usr/lib64/python2.7/site-packages/urwid/raw_display.py", line 241, in run_wrapper
    return fn()
  File "/usr/lib64/python2.7/site-packages/urwid/main_loop.py", line 336, in _run
    self.event_loop.run()
  File "/usr/lib64/python2.7/site-packages/urwid/main_loop.py", line 707, in run
    self._loop()
  File "/usr/lib64/python2.7/site-packages/urwid/main_loop.py", line 786, in _loop
    self._watch_files[fd]()
  File "/usr/lib64/python2.7/site-packages/urwid/main_loop.py", line 387, in _update
    self.process_input(keys)
  File "/usr/lib64/python2.7/site-packages/urwid/main_loop.py", line 487, in process_input
    k = self._topmost_widget.keypress(self.screen_size, k)
  File "/usr/lib64/python2.7/site-packages/urwid/wimp.py", line 642, in keypress
    return self._current_widget.keypress(size, key)
  File "/usr/lib64/python2.7/site-packages/urwid/container.py", line 1102, in keypress
    return self.body.keypress( (maxcol, remaining), key )
  File "/usr/lib64/python2.7/site-packages/urwid/container.py", line 1102, in keypress
    return self.body.keypress( (maxcol, remaining), key )
  File "/usr/bin/aker/tui.py", line 76, in keypress
    self.focus.original_widget.get_caption())
  File "/usr/lib64/python2.7/site-packages/urwid/signals.py", line 120, in emit
    result |= bool(callback(*args_copy))
  File "/usr/bin/aker/tui.py", line 331, in host_chosen_handler
    self.aker.init_connection(self.user.allowed_ssh_hosts[host].fqdn)
  File "/usr/bin/aker/aker.py", line 157, in init_connection
    session.connect(screen_size)
  File "/usr/bin/aker/session.py", line 40, in connect
    self._client.connect(self.host, self.host_port, size)
  File "/usr/bin/aker/SSHClient.py", line 68, in connect
    self._socket.connect((ip, port))
  File "/usr/lib64/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.timeout: timed out

I'm not a very skilled python programmer, but managed to create a fix. I have it in a fork, If you want I can create a PR. Otherwise you can pick it up from here:

         try:
            self._socket.connect((ip, port))
        except Exception as e:
            logging.debug("SSHClient: Could not connect to {0}:{1}".format(ip, port) + " (" + repr(e) + ")")
            print(e)
            self._session.close_session()
            self._socket.close()
            raw_input("Press Enter to continue...")
            return None
EoleDev commented 4 years ago

Hi @pewo , Thank you for your contributions. IMHO as the code is done, would it not be better to catch this exception in file aker.py line 157 ? It seems we should not try to do start_session if the connect fail.

pewo commented 4 years ago

Hi @pewo , Thank you for your contributions. IMHO as the code is done, would it not be better to catch this exception in file aker.py line 157 ? It seems we should not try to do start_session if the connect fail.

Haven't have the time to look at the code, but it seems like an better idea. The earlier the better.

pewo commented 4 years ago

Created a PR to fix aker.py

pewo commented 4 years ago

Do i need to do anything more to get this merged ?

EoleDev commented 4 years ago

Hi, sorry, it would need to be reviewed and validated after by @anazmy. I will make a first review as soon as possible. I think some things are not at their right place but I just took a really quick look.

anazmy commented 4 years ago

Thx @pewo will be reviewing it this week