Skip to content

Instantly share code, notes, and snippets.

@awreece
Created November 10, 2021 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awreece/860ad196a38ce4d10e38d986ac7fb0c7 to your computer and use it in GitHub Desktop.
Save awreece/860ad196a38ce4d10e38d986ac7fb0c7 to your computer and use it in GitHub Desktop.
import subprocess
def shell(*args, **kwargs):
"""Execute a shell command and return the output as a string.
Any additional kwargs are passed directly to subprocess.run.
Examples:
shell("date")
shell("date", "-u")
message = "hello, world"
shell("echo", message)
execute the command `echo` with the message argument
shell("cat", input="hello, world")
execute the command `cat` and send it the string "hello, world" on
stdin.
shell("pwd", cwd="/etc")
execute the command `pwd` in the specified current working
directory
"""
assert "shell" not in kwargs, "The `shell` kwarg to subprocess is generally unsafe. See https://docs.python.org/3/library/subprocess.html#security-considerations"
return subprocess.run(
args, check=True, timeout=60, capture_output=True, text=True, **kwargs
).stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment