py-junos-eznc icon indicating copy to clipboard operation
py-junos-eznc copied to clipboard

Problem with console/tty login after rebooting RE tty.py

Open jnpr-bowen opened this issue 6 years ago • 2 comments

I am having a problem using a console connection in Ansible after rebooting the RE. I believe I have found the solution, but need others to verify the problem and my 'fix'. To recreate the issue from Ansible have a playbook that reboots the RE, delay for time to allow reboot to happen, then juniper_junos_facts via the console port. The facts task should fail. I traced the problem down into the tty.py login state machine and I think there is an error in the RE. After a reboot the console connection does not present a login after the initial '\n' is sent to the port, and so nothing is read by the login state machine, resulting in a 10 second read timeout. The state machine then tries sending a <close/> line to the console and that is the only thing read back from the console by the state machine. The <close/> appears to match the pattern and the state machine thinks it has a successful login and then attempts to start the netconf session, which fails.

During my investigation, I checked the RE at regex101.com and it did not think the cli RE was valid as it says '-' is not properly escaped. At regex101, if I take out one \ from the RE, the tag end in <close/> matches the RE. With my testing, I have changed the RE to: '(?P[^/\-"]>\s*$)'

Without any documentation of the pattern, it is hard to know what the writer was trying to accomplish. My thinking is that the cli prompt is viewed as the '>' char with or without spaces at the end of line. But if it is preceded by '\', '-', '"' it might be something else that is not the cli prompt. My change switches the negative match from '\', to '/'. If '\' should also be in the negative, the pattern would have to be something like: '(?P[^\\/\-"]>\s*$)'

The pattern is on line 55 of tty.py

_RE_PAT = [
    '(?P<loader>oader>\s*$)',
    _re_pat_login,
    '(?P<passwd>assword:\s*$)',
    '(?P<badpasswd>ogin incorrect)',
    '(?P<netconf_closed><!-- session end at .*-->\s*)',
    '(?P<shell>%|#\s*$)',
    '(?P<cli>[^\\-"]>\s*$)',
    '(?P<option>Enter your option:\s*$)',
    '(?P<hotkey>connection: <CTRL>Z)',
    '(?P<new_cli_re>[\\/\-"]>\s*$)'
]

Just noticed that markdown is messing with how backslash is being displayed. Don't copy/paste my pattern from the text, use the pattern I added to the _RE_PAT code snippet.

jnpr-bowen avatar Apr 17 '18 18:04 jnpr-bowen

Some extra details from an Ansible playbook log where one connection works and another does not: Working connection: 2018-04-16 09:49:42,845 jnpr.junos.tty TTY: logging in...... 2018-04-16 09:49:52,847 jnpr.junos.tty_telnet Got: 2018-04-16 09:49:52,847 jnpr.junos.tty_telnet Write: <close-session/> 2018-04-16 09:49:52,908 jnpr.junos.tty_telnet Got: <close-session/>^M Password: 2018-04-16 09:49:52,908 jnpr.junos.tty_telnet Write: changed_for_posting 2018-04-16 09:49:54,139 jnpr.junos.tty_telnet Got: ^M Login incorrect^M login: 2018-04-16 09:49:54,139 jnpr.junos.tty_telnet Write: bbowen 2018-04-16 09:49:54,194 jnpr.junos.tty_telnet Got: bbowen^M Password: 2018-04-16 09:49:54,195 jnpr.junos.tty_telnet Write: changed_for_posting 2018-04-16 09:49:55,953 jnpr.junos.tty_telnet Got: ^M Last login: Mon Apr 16 08:43:50 on ttyu0^M ... ;bbowen@lab-re1> 2018-04-16 09:49:55,953 jnpr.junos.tty TTY: OK.....starting NETCONF 2018-04-16 09:49:55,953 jnpr.junos.tty_telnet Write: junoscript netconf need-trailer 2018-04-16 09:49:57,570 jnpr.junos.tty_netconf Received:

Failing connection: 2018-04-16 10:02:05,173 jnpr.junos.tty TTY: logging in...... 2018-04-16 10:02:15,175 jnpr.junos.tty_telnet Got: 2018-04-16 10:02:15,176 jnpr.junos.tty_telnet Write: <close-session/> 2018-04-16 10:02:15,220 jnpr.junos.tty_telnet Got: <close-session/>^M

2018-04-16 10:02:15,220 jnpr.junos.tty TTY: OK.....starting NETCONF 2018-04-16 10:02:15,221 jnpr.junos.tty_telnet Write: junoscript netconf need-trailer 2018-04-16 10:02:36,543 jnpr.junos.console ERROR: login:Netconify Error: netconf not responding

jnpr-bowen avatar Apr 17 '18 20:04 jnpr-bowen

Hi @jnpr-bowen, Can you please provide a reproduction script. I tried testing this and I can retrieve the facts successfully.

apurvaraghu avatar Mar 05 '24 22:03 apurvaraghu