import os import subprocess import gradio as gr # Fetch the app password from environment variables APP_PASSWORD = os.getenv("APP_PASSWORD") # Function to execute shell commands def execute_command(password, script): # Check if the password is correct if password != APP_PASSWORD: return {"error": "Invalid password!"} # Validate the script input if not script: return {"error": "No script provided!"} try: # Execute the command using subprocess process = subprocess.Popen( script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) stdout, stderr = process.communicate() # Return the output and errors return { "stdout": stdout.decode("utf-8"), "stderr": stderr.decode("utf-8"), "exit_code": process.returncode } except Exception as e: return {"error": f"Error executing script: {str(e)}"} # Define Gradio interface def gradio_interface(password, script): return execute_command(password, script) # Create Gradio app interface = gr.Interface( fn=gradio_interface, inputs=[ gr.Textbox(label="Password", type="password"), gr.Textbox(label="Script", lines=5, placeholder="Enter your script here...") ], outputs="json", title="Command Executor", description="Provide a password and a shell script to execute commands remotely." ) # Launch the Gradio interface if __name__ == "__main__": interface.launch(share=True, inline=True)