Created
November 10, 2021 19:11
-
-
Save awreece/860ad196a38ce4d10e38d986ac7fb0c7 to your computer and use it in GitHub Desktop.
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 | |
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