Skip to content

Commit 21c7afa

Browse files
Added crlf/lf option to telnet (#440)
* Added crlf/lf option to telnet * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c7d2bdf commit 21c7afa

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- telnet - add crlf option to send CRLF instead of just LF (https://github.com/ansible-collections/ansible.netcommon/pull/440).

docs/ansible.netcommon.telnet_module.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ Parameters
5252
<div style="font-size: small; color: darkgreen"><br/>aliases: commands</div>
5353
</td>
5454
</tr>
55+
<tr>
56+
<td colspan="1">
57+
<div class="ansibleOptionAnchor" id="parameter-"></div>
58+
<b>crlf</b>
59+
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
60+
<div style="font-size: small">
61+
<span style="color: purple">boolean</span>
62+
</div>
63+
</td>
64+
<td>
65+
<ul style="margin: 0; padding: 0"><b>Choices:</b>
66+
<li><div style="color: blue"><b>no</b>&nbsp;&larr;</div></li>
67+
<li>yes</li>
68+
</ul>
69+
</td>
70+
<td>
71+
<div>Sends a CRLF (Carrage Return) instead of just a LF (Line Feed).</div>
72+
</td>
73+
</tr>
5574
<tr>
5675
<td colspan="1">
5776
<div class="ansibleOptionAnchor" id="parameter-"></div>

plugins/action/telnet.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,18 @@ def run(self, tmp=None, task_vars=None):
4949
pause = int(self._task.args.get("pause", 1))
5050

5151
send_newline = self._task.args.get("send_newline", False)
52+
clrf = self._task.args.get("clrf", False)
5253

5354
login_prompt = to_text(self._task.args.get("login_prompt", "login: "))
5455
password_prompt = to_text(self._task.args.get("password_prompt", "Password: "))
5556
prompts = self._task.args.get("prompts", ["\\$ "])
5657
commands = self._task.args.get("command") or self._task.args.get("commands")
5758

59+
if clrf:
60+
line_ending = "\r\n"
61+
else:
62+
line_ending = "\n"
63+
5864
if isinstance(commands, text_type):
5965
commands = commands.split(",")
6066

@@ -64,25 +70,27 @@ def run(self, tmp=None, task_vars=None):
6470
self.output = bytes()
6571
try:
6672
if send_newline:
67-
self.tn.write(b"\n")
73+
self.tn.write(to_bytes(line_ending))
6874

6975
self.await_prompts([login_prompt], timeout)
70-
self.tn.write(to_bytes(user + "\n"))
76+
display.vvvvv(">>>user: %s" % user)
77+
self.tn.write(to_bytes(user + line_ending))
7178

7279
if password:
7380
self.await_prompts([password_prompt], timeout)
74-
self.tn.write(to_bytes(password + "\n"))
81+
display.vvvvv(">>>password: %s" % password)
82+
self.tn.write(to_bytes(password + line_ending))
7583

7684
self.await_prompts(prompts, timeout)
7785

7886
for cmd in commands:
7987
display.vvvvv(">>> %s" % cmd)
80-
self.tn.write(to_bytes(cmd + "\n"))
88+
self.tn.write(to_bytes(cmd + line_ending))
8189
self.await_prompts(prompts, timeout)
8290
display.vvvvv("<<< %s" % cmd)
8391
sleep(pause)
8492

85-
self.tn.write(b"exit\n")
93+
self.tn.write(to_bytes("exit" + line_ending))
8694

8795
except EOFError as e:
8896
result["failed"] = True

plugins/modules/telnet.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@
8484
required: false
8585
type: bool
8686
default: false
87+
crlf:
88+
description:
89+
- Sends a CRLF (Carrage Return) instead of just a LF (Line Feed).
90+
required: false
91+
type: bool
92+
default: false
8793
notes:
8894
- The C(environment) keyword does not work with this task
8995
author:

0 commit comments

Comments
 (0)