Spaces:
Sleeping

neuronalis / tools /bash_executor.py
Nacho
HF deploy
5a6158c
Raw History Blame Contribute Delete
824 Bytes
import subprocess
import shlex
def execute_command(command, timeout=30):
"""Executes a bash command."""
try:
# Basic security check
forbidden = ['rm -rf /', ':(){ :|:& };:'] # Fork bomb
for f in forbidden:
if f in command:
return "Error: Command blocked for security reasons."
process = subprocess.run(
command,
shell=True,
capture_output=True,
text=True,
timeout=timeout
)
output = process.stdout
if process.stderr:
output += f"\nSTDERR:\n{process.stderr}"
return output
except subprocess.TimeoutExpired:
return "Error: Command timed out."
except Exception as e:
return f"Error executing command: {e}"