Last active
June 19, 2019 10:41
-
-
Save gustavorv86/43e90e8059ceea9acb095c5118e781c5 to your computer and use it in GitHub Desktop.
Execute command in Python and get stdout, stderr and return code.
This file contains hidden or 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
#!/usr/bin/env python | |
import subprocess | |
def execute_process(cmd) : | |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | |
out, err = p.communicate() | |
return {'stdout': out, 'stderr': err, 'retcode': p.returncode} | |
if __name__ == "__main__": | |
ret = execute_process('ping -w 3 www.google.com') | |
print('Done') | |
print('STDOUT: ' + ret['stdout']) | |
print('STDERR: ' + ret['stderr']) | |
print('RETCODE: ' + str(ret['retcode'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment