Skip to content

Commit

Permalink
Added crlf/lf option to telnet
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpsomiadis committed Jul 29, 2023
1 parent 07ef0c5 commit 17bfcc5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/440-telnet-add-crlf-option.yml
Original file line number Diff line number Diff line change
@@ -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).
18 changes: 13 additions & 5 deletions plugins/action/telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(",")

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions plugins/modules/telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 17bfcc5

Please sign in to comment.