From 17bfcc54d3f329e87ea1301b44cbf6d81264c581 Mon Sep 17 00:00:00 2001 From: Michael Psomiadis Date: Sat, 29 Jul 2023 22:05:45 +1000 Subject: [PATCH] Added crlf/lf option to telnet --- .../fragments/440-telnet-add-crlf-option.yml | 2 ++ plugins/action/telnet.py | 18 +++++++++++++----- plugins/modules/telnet.py | 6 ++++++ 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/440-telnet-add-crlf-option.yml diff --git a/changelogs/fragments/440-telnet-add-crlf-option.yml b/changelogs/fragments/440-telnet-add-crlf-option.yml new file mode 100644 index 000000000..c914268ce --- /dev/null +++ b/changelogs/fragments/440-telnet-add-crlf-option.yml @@ -0,0 +1,2 @@ +minor_changes: + - telnet - add crlf option to send CRLF instead of just LF (https://github.com/ansible-collections/ansible.netcommon/pull/440). diff --git a/plugins/action/telnet.py b/plugins/action/telnet.py index 74adb32a1..0c2a6ca71 100644 --- a/plugins/action/telnet.py +++ b/plugins/action/telnet.py @@ -49,12 +49,18 @@ def run(self, tmp=None, task_vars=None): pause = int(self._task.args.get("pause", 1)) send_newline = self._task.args.get("send_newline", False) + clrf = self._task.args.get("clrf", False) login_prompt = to_text(self._task.args.get("login_prompt", "login: ")) password_prompt = to_text(self._task.args.get("password_prompt", "Password: ")) prompts = self._task.args.get("prompts", ["\\$ "]) commands = self._task.args.get("command") or self._task.args.get("commands") + if clrf: + line_ending = "\r\n" + else: + line_ending = "\n" + if isinstance(commands, text_type): commands = commands.split(",") @@ -64,25 +70,27 @@ def run(self, tmp=None, task_vars=None): self.output = bytes() try: if send_newline: - self.tn.write(b"\n") + self.tn.write(to_bytes(line_ending)) self.await_prompts([login_prompt], timeout) - self.tn.write(to_bytes(user + "\n")) + display.vvvvv(">>>user: %s" % user) + self.tn.write(to_bytes(user + line_ending)) if password: self.await_prompts([password_prompt], timeout) - self.tn.write(to_bytes(password + "\n")) + display.vvvvv(">>>password: %s" % password) + self.tn.write(to_bytes(password + line_ending)) self.await_prompts(prompts, timeout) for cmd in commands: display.vvvvv(">>> %s" % cmd) - self.tn.write(to_bytes(cmd + "\n")) + self.tn.write(to_bytes(cmd + line_ending)) self.await_prompts(prompts, timeout) display.vvvvv("<<< %s" % cmd) sleep(pause) - self.tn.write(b"exit\n") + self.tn.write(to_bytes("exit" + line_ending)) except EOFError as e: result["failed"] = True diff --git a/plugins/modules/telnet.py b/plugins/modules/telnet.py index a5abac726..65311eede 100644 --- a/plugins/modules/telnet.py +++ b/plugins/modules/telnet.py @@ -84,6 +84,12 @@ required: false type: bool default: false + crlf: + description: + - Sends a CRLF (Carrage Return) instead of just a LF (Line Feed). + required: false + type: bool + default: false notes: - The C(environment) keyword does not work with this task author: