How To Make Own UI To Run Commands On Linux
Hey, guys hope you all are doing good and leaning some coll stuff daily today we are going to see many new and interesting things and this blog gonna be full of technicality but I will try my best to make you understand things easily.
Here the only thing I am accepting from you is that you know the basics of python and you have read my article in which we create python bot if not read not to worry here I am providing a link you can read it and come back here again. To read the article Click here.
Today we are going to make one menu and its UI will on the web so if you want to run any command you can see the menu and just type that option command will run in your Linux don’t get confused by reading this it's gonna be super easy trust me :).
The technology we are going to use HTML(for web interface UI), python(we use subprocess and CGI libraries.Subprocess is used to run command to terminal from python, and CGI is used to take input from WebUI and give to subprocess). Many of you are hearing CGIterm for the first time so let's understand what is CGI it acts as a common gateway interface between an HTTP client and your system.
In this image you can see Client-server is contacting CGI via webserver and CGI will contact you server-side language and tell him to run whats return in server-side language and take the output from server-side language and give back to the webserver and via the web server, the web client can see it.
Here for the server-side language, we use python and HTML to make web UI.
Let's get started implementing the process here I am using redhat8.0 os you can use any.
Step1-We need an HTTP server that is a product of apache to install it in RedHat type yum install HTTP
Step2-We need to start httpd server and we will enable it so it will not get off when you restart your system to do the following run the followings commands
systemctl start httpd
systemctl enable httpd
once this is done we are good to go and if you type ifconfig you can see your IP and make sure the adopter is on bridge connection once you get IP you can run it your browser make sure you are connected to the same network as RedHat connect as soon as you go to IP you can see some error that because the firewall is not allowing to remove that error follow the step3.
Step3-To stop and disable the firewall run the following command
systemctl stop firewalld
systemctl disabled firewalld
Once it is stopped and disabled you can see the welcome page created by apache.
If you want to put your own HTML page go to
cd /var/www/HTML in this directory you can create your file and can see you page
Above you can see we have created our own HTML page. The code is below and in the end, I will put the GitHub repo link also.
Enter 1 to run date command<br>
Enter 2 to run cal command<br>
Enter 3 to run firefox<br><form action=”http://your ip/cgi-bin/input.py">
Enter option from above menue to run cmnd <input type=’text’ name=’x’><br>
<input type=’submit’ value=”Submite”>
</form>
Above we have written basic HTML code to take input and pass to other pages.
Once you are done with this now its time to run write our Cgi-Script for that go to the CGI-bin folder instead HTML folder
cd /var/www/CGI-bin
In the above code, you can see we have written #!/usr/bin/python3 because this tells the program where is a python in which code has to be run.
import CGI
import subprocess
The above two lines are used to import the necessary library so that we can use in our code
print(“content-type: text/html”)
print()
This is the format we have to follow to write CGI script it tells the web client the content I am giving is a type of text.
mydata = cgi.FieldStorage()
The above line is used to take input via a CGI script
myx = mydata.getvalue(“x”)
The above line helps to take the thing which is been stored in variable x
if myx==”1":
a=subprocess.getoutput(“date”)
print(a)
elif myx==”2":
b=subprocess.getoutput(“cal”)
print(b)
elif myx==”3":
c=subprocess.getoutput(“firefox”)
else:
print(“invalid option please try again from the give menue”)
Above its a basic python code with if and else loop.
Above you can see the working example of the code below I will attach the git hub repo in which you can find both the files of code.
Guys here is my Github repo https://github.com/guptaadi123/Python-menue.git you can find all the code we have discussed in the above blog.
Guys, here we come to end this is not much of a technical blog it’s just a small piece of information I want to share with you all hope you like it and found it informative.
Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link https://sites.google.com/view/adityvgupta/home.Guys Please do not hesitate to keep 👏👏👏👏👏 for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.