Skip to content

Commit

Permalink
Allow send() to skip appending newline at the end (#35, #25)
Browse files Browse the repository at this point in the history
* Allow send() to skip appending newline at the end.

* Cover skipping newline in tests.

* cleanup the send() newline, and add it as new test
  • Loading branch information
fruch authored Sep 30, 2017
1 parent a80e627 commit 854fcd2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions paramiko_expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,12 @@ def expect(self, re_strings='', timeout=None, output_callback=None, default_matc
# measure, let's send back a -1
return -1

def send(self, send_string):
def send(self, send_string, newline=None):
"""Saves and sends the send string provided."""
self.current_send_string = send_string
self.channel.send(send_string + self.newline)
newline = newline if newline is not None else self.newline

self.channel.send(send_string + newline)

def tail(self, line_prefix=None, callback=None, output_callback=None, stop_callback=lambda x: False, timeout=None):
"""
Expand Down
16 changes: 16 additions & 0 deletions test/test_paramiko_expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,19 @@ def test_07_close(interact):
channel_mock.close.side_effect = [ socket.timeout ]
interact.close()

def test_08_issue_25_skip_newline():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname="localhost", username="root", port=2222, key_filename='./test/id_rsa')
with SSHClientInteraction(client, timeout=10, display=True) as interact:
interact.send('ls -all')
interact.expect(prompt, timeout=5)

# Do not actually sleep, send a ctrl-c at the end
interact.send('sleep 1', newline=chr(3))
interact.expect(prompt, timeout=5)
interact.send('sleep 1' + chr(3), newline='')
interact.expect(prompt, timeout=5)

interact.send('ls -all')
interact.expect(prompt, timeout=5)

0 comments on commit 854fcd2

Please sign in to comment.