Created my own API interface to run Linux commands.😁😁
Certainly! Here’s an example code snippet that displays an input field and a submit button in an HTML file (`index.html`) located in `/var/www/html` directory. When the form is submitted, the input value (which is a Linux command) will be sent to a Python script (`a.py`) located in the `/var/www/cgi-bin` directory. The Python script will execute the Linux command using `subprocess` and direct the output back to the HTML page.
**index.html** (located in `/var/www/html`):
<!DOCTYPE html>
<html><head> <title>Linux Command Execution</title></head><body> <h1>Execute Linux Command</h1> <form action="/cgi-bin/a.py" method="post"> <label for="command">Enter Linux Command:</label> <input type="text" id="command" name="command"> <input type="submit" value="Submit"> </form></body></html>
**a.py** (located in `/var/www/cgi-bin`):
#!/usr/bin/python
import subprocessimport cgi
# Get the command from the form submissionform = cgi.FieldStorage()command = form.getvalue('command')
# Execute the command using subprocessoutput = subprocess.getoutput(command)
# Print the output as HTMLprint("Content-type: text/html\n\n")print("<html>")print("<head>")print("<title>Command Output</title>")print("</head>")print("<body>")print("<h1>Command Output:</h1>")print("<pre>{}</pre>".format(output))print("</body>")print("</html>")
Make sure to give executable permissions to `a.py` by running the following command:
chmod +x /var/www/cgi-bin/a.py
After setting up the files, you can access the HTML page by opening your web browser and navigating to `http://localhost` or your server’s IP address. The form will allow you to enter a Linux command, and upon submission, the command will be executed and the output will be displayed on the page.