Skip to content

Instantly share code, notes, and snippets.

@stefano-garzarella
Last active November 3, 2015 16:00
Show Gist options
  • Save stefano-garzarella/fff8738204592d8acaad to your computer and use it in GitHub Desktop.
Save stefano-garzarella/fff8738204592d8acaad to your computer and use it in GitHub Desktop.
execute schell command in python script
import subprocess
import sys
def sh(cmd, dbg = False):
if dbg:
print(cmd)
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
ret = process.communicate()[0]
if dbg:
print(ret)
return ret
def ssh(ssh, arg, dbg = False):
if dbg:
print(ssh)
print(arg)
cmd = ssh.split()
cmd.append(arg)
process = subprocess.Popen(cmd)
ret = process.communicate()[0]
if dbg:
print(ret)
return ret
def shpipe(cmds, dbg = False):
if dbg:
print(cmds)
prev_stdout = None
for cmd in cmds:
p = subprocess.Popen(cmd.split(), stdin = prev_stdout, stdout=subprocess.PIPE)
prev_stdout = p.stdout
ret = p.communicate()[0]
if dbg:
print(ret)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment