Last active
November 3, 2015 16:00
-
-
Save stefano-garzarella/fff8738204592d8acaad to your computer and use it in GitHub Desktop.
execute schell command in python script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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