ianmiell / shutit

Automation framework for programmers
http://ianmiell.github.io/shutit/
MIT License
2.15k stars 110 forks source link

ssh login crashes for "Last login:" info msg #310

Closed luto closed 6 years ago

luto commented 6 years ago

When a SSH server outputs something like this for each login ...

Last failed login: Sat Nov 18 00:28:14 UTC 2017 from 80.110.xxx on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Sat Nov 18 00:27:58 2017 from 80.110.xxx

... the following quite simple code ...

cmd = ' '.join([
    'ssh {username}@{hostname}',
    '-p {port}',
    '-i {key_filename}',
]).format(**cfg)
self.session = shutit.create_session('bash', loglevel='DEBUG')
self.session.login(cmd, user=cfg['username'], fail_on_fail=False)

... crashes shutit because the regex r'[^t] login:' matches Last login: Sat Nov, which causes that line to be interpreted as a password prompt. It then proceeds to send the server a password (None in my case). This causes it to crash:

            shutit = self.shutit
            cfg = shutit.cfg
            # Set up what we expect.
            sendspec.expect = sendspec.expect or self.default_expect
>           if sendspec.send.strip() == '':
E           AttributeError: 'NoneType' object has no attribute 'strip'

../venv/lib/python2.7/site-packages/shutit_pexpect.py:2484: AttributeError

A quick google search gave me lots of SSH servers asking login as: for a username, but not login:. Can you tell me how the actual line that should match the regex looks, so I can make it a little more strict?

ianmiell commented 6 years ago

Hi, thanks for raising (and hello to a fellow Austrian, though I'm also British!). I think it's already designed not to match 'last login' (hence the [^t] before the login). I think it's matching the 'failed login:' item. I've made and released a change to not match the 'failed login' part which should do the trick.

Out of interest, what is your use case? Maybe I can help in other ways.

ianmiell commented 6 years ago

release 1.0.34

luto commented 6 years ago

Out of interest, what is your use case? Maybe I can help in other ways.

Infrastructure testing :) We setup up servers/containers with different services and then add quite detailed tests, that they actually work.

luto commented 6 years ago

Hi, thanks for raising (and hello to a fellow Austrian, though I'm also British!).

hey there! if you every stop by or live in Vienna, drop me an email :)

hence the [^t] before the login

hah, my 2am-brain auto-corrected that to [^\t] for hours m) The fix works like a charm, thank you! :)

ianmiell commented 6 years ago

Sicherlich! No problem - so how did you come across shutit and what are you using it for?