Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/attackmate/executors/ssh/interactfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ def __init__(self):
self.logger = logging.getLogger('playbook')

def check_prompt(self, output: str, prompts: list[str]) -> bool:
if output and prompts:
for p in prompts:
if output.endswith(p):
self.logger.debug('found prompt!')
self.timer = None
return True
else:
if not output:
return False

if not prompts:
self.set_timer()
return False

for p in prompts:
if output.endswith(p):
self.logger.debug('found prompt!')
self.timer = None
return True

self.set_timer()
return False

def check_timer(self, seconds: int) -> bool:
Expand Down
33 changes: 33 additions & 0 deletions test/units/test_interactfeature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import time
from attackmate.executors.ssh.interactfeature import Interactive


class TestCheckPrompt:
def setup_method(self):
self.interactive = Interactive()
self.interactive.set_timer()

def test_no_prompts_configured_resets_timer(self):
before = self.interactive.timer
time.sleep(0.05)
self.interactive.check_prompt('some output', [])
assert self.interactive.timer > before

def test_prompt_not_matched_resets_timer(self):
before = self.interactive.timer
time.sleep(0.05)
result = self.interactive.check_prompt('still running...', ['$ '])
assert result is False
assert self.interactive.timer > before

def test_prompt_matched_clears_timer_and_returns_true(self):
result = self.interactive.check_prompt('root@host:~$ ', ['$ '])
assert result is True
assert self.interactive.timer is None

def test_empty_output_on_channel_eof_does_not_reset_timer(self):
before = self.interactive.timer
time.sleep(0.05)
result = self.interactive.check_prompt('', ['$ '])
assert result is False
assert self.interactive.timer == before
Loading