I decided to instead have this little helper function to encapsulate what I consider to be saner defaults. I decode the bytes into utf8 since thats the output for 99% of all bash commands.
#!/usr/bin/env python3 import subprocess def run_cmd(cmd): result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result.stdout = result.stdout.decode('utf8') result.stderr = result.stderr.decode('utf8') return result
Running that function will execute whatever command you pass it (insecure, but use it appropriately) and returns an object that you can then check the return code, stdout, and stderr.
So now, it's just:
In [25]: if 'root' in run_cmd('whoami').stdout: ....: print("you are root") ....: you are root
No comments:
Post a Comment