Skip to content

Commit b9b39c8

Browse files
author
Olivier Roques
committed
Add shell option to run method
1 parent 2d358cf commit b9b39c8

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ ssh_controller.connect()
4444
return_code, output = ssh_controller.run(
4545
command="echo 'Hello world!' > /tmp/hello.txt",
4646
display=True, # display output, false by default
47-
capture_output=True, # return output, false by default
48-
combine_stderr=False, # combine stderr and stdout, false by default
49-
timeout=10, # command timeout in seconds, 600s by default
47+
capture_output=True, # save output, false by default
48+
# request a shell to run the command, true by default
49+
shell=True,
50+
# combine stderr into stdout when shell=False, false by default
51+
combine_stderr=False,
52+
# command timeout in seconds, None (wait indefinitely) by default
53+
timeout=10,
5054
)
5155
print(f"return code: {return_code}, output: {output}")
5256
```

examples/demo.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ def demo_key():
2727
return_code, output = ssh_controller.run(
2828
command="echo 'Hello world!' > /tmp/hello.txt",
2929
display=True, # display output, false by default
30-
capture_output=True, # return output, false by default
31-
combine_stderr=False, # combine stderr and stdout, false by default
32-
timeout=10, # command timeout in seconds, 600s by default
30+
capture_output=True, # save output, false by default
31+
# request a shell to run the command, true by default
32+
shell=True,
33+
# combine stderr into stdout when shell=False, false by default
34+
combine_stderr=False,
35+
# command timeout in seconds, None (wait indefinitely) by default
36+
timeout=10,
3337
)
3438
logging.info(f"return code: {return_code}, output: {output}")
3539

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="sshcontroller",
8-
version="1.1",
8+
version="1.2",
99
author="Olivier Roques",
1010
author_email="[email protected]",
1111
description="A package to easily run SSH commands",

sshcontroller/sshcontroller.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,19 @@ def __run_until_event(
122122
command,
123123
stop_event,
124124
display=True,
125-
combine_stderr=False,
126125
capture_output=False,
126+
shell=True,
127+
combine_stderr=False,
127128
):
128129
channel = self.transport.open_session()
129130
output = ""
130-
timeout = 2
131131

132-
channel.settimeout(timeout)
132+
channel.settimeout(2)
133133
channel.set_combine_stderr(combine_stderr)
134-
channel.get_pty()
134+
135+
if shell:
136+
channel.get_pty()
137+
135138
channel.exec_command(command)
136139

137140
if not display and not capture_output:
@@ -171,15 +174,19 @@ def __run_until_exit(
171174
command,
172175
timeout,
173176
display=True,
174-
combine_stderr=False,
175177
capture_output=False,
178+
shell=True,
179+
combine_stderr=False,
176180
):
177181
channel = self.transport.open_session()
178182
output = ""
179183

180184
channel.settimeout(timeout)
181185
channel.set_combine_stderr(combine_stderr)
182-
channel.get_pty()
186+
187+
if shell:
188+
channel.get_pty()
189+
183190
channel.exec_command(command)
184191

185192
try:
@@ -214,16 +221,18 @@ def run(
214221
self,
215222
command,
216223
display=False,
217-
combine_stderr=False,
218224
capture_output=False,
225+
shell=True,
226+
combine_stderr=False,
227+
timeout=None,
219228
stop_event=None,
220-
timeout=600,
221229
):
222230
if stop_event:
223231
return self.__run_until_event(
224232
command,
225233
stop_event,
226234
display=display,
235+
shell=shell,
227236
combine_stderr=combine_stderr,
228237
capture_output=capture_output,
229238
)
@@ -232,6 +241,7 @@ def run(
232241
command,
233242
timeout,
234243
display=display,
244+
shell=shell,
235245
combine_stderr=combine_stderr,
236246
capture_output=capture_output,
237247
)
@@ -245,8 +255,11 @@ def wrapper(*args, **kwargs):
245255
if not self.transport.is_authenticated():
246256
logging.error("SSH session is not ready")
247257
return 1
258+
248259
sftp_channel = SFTPController.from_transport(self.transport)
249260
r = getattr(sftp_channel, target)(*args, **kwargs)
250261
sftp_channel.close()
262+
251263
return r
264+
252265
return wrapper

0 commit comments

Comments
 (0)