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

Added crlf/lf option to telnet #440

Merged
merged 2 commits into from
Sep 6, 2023
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
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).
19 changes: 19 additions & 0 deletions docs/ansible.netcommon.telnet_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ Parameters
<div style="font-size: small; color: darkgreen"><br/>aliases: commands</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>crlf</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">boolean</span>
</div>
</td>
<td>
<ul style="margin: 0; padding: 0"><b>Choices:</b>
<li><div style="color: blue"><b>no</b>&nbsp;&larr;</div></li>
<li>yes</li>
</ul>
</td>
<td>
<div>Sends a CRLF (Carrage Return) instead of just a LF (Line Feed).</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
Expand Down
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
Loading