forked from smilejay/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_cmd.py
More file actions
22 lines (17 loc) · 673 Bytes
/
run_cmd.py
File metadata and controls
22 lines (17 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from subprocess import Popen, PIPE, STDOUT
def shell_output(cmd):
''' execute a shell command and get its output (stdout/stderr) '''
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
return p.communicate()[0]
def shell_rc_and_output(cmd):
''' execute a shell command and get its return code and output (stdout/stderr) '''
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
out = p.communicate()[0]
rc = p.returncode
return rc, out
if __name__ == "__main__":
cmd = 'ls -l'
print(shell_output(cmd))
rc, out = shell_rc_and_output(cmd)
print(rc)
print('rc: {}, out: {}'.format(rc, out))