Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding a interaction_complete_patterns does not end interactive session #318

Open
MattCatz opened this issue Feb 8, 2024 · 4 comments
Open
Labels
bug Something isn't working

Comments

@MattCatz
Copy link

MattCatz commented Feb 8, 2024

Describe the bug
According to the documentation of send_interactive (specifically in sync_driver.py)

interaction_complete_patterns: list of patterns, that if seen, indicate the interactive
                "session" has ended and we should exit the interactive session.

I take this to mean that no more input will be send if one of those patterns is seen. The actual behavior is slightly different. It handles these patterns like the prompt and continues to send input after seeing one.

This is also supported if you dig down into the code. Going GenericDriver.send_interactive -> Channel.send_inputs_interact you can see that it is impossible to gracefully break out of the for interact_event in interact_events loop (i.e. all interactive events will be sent).

for interact_event in interact_events:
channel_input = interact_event[0]
bytes_channel_input = channel_input.encode()
channel_response = interact_event[1]
prompts = [channel_response]
if interaction_complete_patterns is not None:
prompts.extend(interaction_complete_patterns)
try:
hidden_input = interact_event[2]
except IndexError:
hidden_input = False
_channel_input = channel_input if not hidden_input else "REDACTED"
self.logger.info(
f"sending interactive input: {_channel_input}; "
f"expecting: {channel_response}; "
f"hidden_input: {hidden_input}"
)
self.write(channel_input=channel_input, redacted=bool(hidden_input))
if channel_response and hidden_input is not True:
buf += self._read_until_input(channel_input=bytes_channel_input)
self.send_return()
buf += self._read_until_explicit_prompt(prompts=prompts)

@MattCatz MattCatz added the bug Something isn't working label Feb 8, 2024
@carlmontanari
Copy link
Owner

yeah probably should be a check on buf if it contains any of the complete patterns. up for pr? if yes, please do async too :)

@carlmontanari
Copy link
Owner

for whatever reason (no good one I assume) looks like this is "correct" in scrapligo, so can probably steal from that sorta too (though guess it should be fairly straight forward fix anywho)

@MattCatz
Copy link
Author

MattCatz commented Feb 8, 2024

On a tangent to this bug, would you also want to end the interactive session if the a failed_when_contains pattern is found? Something like this seems a bit redundant (but I could see it either way).

        interact = conn.channel.send_inputs_interact(
            [
                ("copy flash: scp:", "Source filename []?", False),
                ("test1.txt", "Address or name of remote host []?", False),
                ("172.31.254.100", "Destination username [carl]?", False),
                ("carl", "Password:", False),
                ("super_secure_password", prompt, True),
            ],
            failed_when_contains=["file not found"],
            interaction_complete_patterns=["file not found"]
        )

@carlmontanari
Copy link
Owner

hmm a good question for sure...

its probably not a bad idea to add, but that is certainly more work :)

in general I would kind of avoid the send_interactive stuff anyway and would prefer to use the read callback bits that are of course not really documented much (at all?!). so I guess what id vote to do is probably to just fix the initial issue and not mess about with adding the failed when stuff. for more fancy things you could do the read callback thing where you basically can do whatever you want in a less rigid way than this anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants