Linux Remote CLI Flutter Mobile App using python -CGI With Firebase Stream Builder (Task 4)
Linux CLI Mobile App using Firebase, Redhat , Linux Flutter Integration using streamBuilder.
This is Part Two of Linux Cli Mobile App In this Tutorial we will Use Stream Builder To Access the Firebase.
Link for part 1:click here
what is Stream Builder : Refer This Video For detail explaination : click Here
This is demonstration project , based on development of Linux Remote CLI Flutter application using Integration of Google firebase (firestore to store the response) and Redhat Linux using Python — CGI Programming.
Task 4 : Flutter App Development‼️
1. Create an app that can run any linux command using API
2. Output will be saved in Firestore.
3. From Firestore, get this output and print on screen.
4. Use Stream Builder to save data.
What is Command line?
The Linux command line is a text interface to your computer. Often referred to as the shell, terminal, console, prompt or various other names, it can give the appearance of being complex and confusing to use. To access the Command line of any Linux system we must have an access to the physical server to perform any operation on it.
In this project article we will learn how to make app from which we can perform any Linux command remotely from our mobile.
What is CGI Programming?
The Common Gateway Interface, or CGI, is a set of standards that define how information is exchanged between the web server and a custom script. The CGI specs are currently maintained by the NCSA.
What is CGI?
- The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers.
To understand the concept of CGI, let us see what happens when we click a hyper link to browse a particular web page or URL.
- Your browser contacts the HTTP web server and demands for the URL, i.e., filename.
- Web Server parses the URL and looks for the filename. If it finds that file then sends it back to the browser, otherwise sends an error message indicating that you requested a wrong file.
- Web browser takes response from web server and displays either the received file or error message.
However, it is possible to set up the HTTP server so that whenever a file in a certain directory is requested that file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your browser to display. This function is called the Common Gateway Interface or CGI and the programs are called CGI scripts. These CGI programs can be a Python Script, PERL Script, Shell Script, C or C++ program, etc.
In this we can use Python instead of shell scripts for automation. Python provides methods to run shell commands, giving us the same functionality of those shells scripts. Learning how to run shell commands in Python opens the door for us to automate computer tasks in a structured and scalable way.
To know more about cgi-programming refer this link :
Web Server Support and Configuration
Before you proceed with CGI Programming, make sure that your Web Server supports CGI and it is configured to handle CGI Programs. All the CGI Programs to be executed by the HTTP server are kept in a pre-configured directory. This directory is called CGI Directory and by convention it is named as /var/www/cgi-bin. By convention, CGI files have extension as. cgi, but you can keep your files with python extension .py as well.
By default, the Linux server is configured to run only the scripts in the cgi-bin directory in /var/www. If you want to specify any other directory to run your CGI scripts, comment the following lines in the httpd.conf file −
Allow from all
This file is kept in /var/www/cgi-bin directory and it has following content. Before running your CGI program, make sure you have change mode of file using chmod 755 linux.py UNIX command to make file executable.
output=subprocess.getoutput("sudo " +mydata.getvalue("x"))
GET and POST Methods
You must have come across many situations when you need to pass some information from your browser to web server and ultimately to your CGI Program. Most frequently, browser uses two methods two pass this information to web server. These methods are GET Method and POST Method.
Passing Information using GET method
The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows −
//mycmd filed will take the command name which we have to execute.To check the server ip address you can use ifconfig command.
The GET method is the default method to pass information from browser to web server and it produces a long string that appears in your browser’s Location:box. Never use GET method if you have password or other sensitive information to pass to the server.
Note: Before you can access the server remotely you need to set firewall To know more about firewall refer this link.
for this tutorial purpose i had disabled my firewall : To disable the firewall use command : systemctl stop firewalld
Now Check SELinux Status:
The SELinux service is enabled by default on CentOS and most other RHEL-based systems. However, this might not be the case for your system.
Start by checking the status of SELinux on your system with the command:
To disable SELinux temporarily, type in the following command in the terminal:
sudo setenforce 0
Now We will able to Perform Command operation on Linux remotely using http :
Now we will make an simple Interface flutter app to perform Linux commands.
To know how to Connect Firebase Firebase Storage Refer This article: https://firebase.flutter.dev/docs/firestore/usage/
This is a demonstration image : Enter any Command in Text filed and execute that command remotely.
Function that will execute and fetch the http response from the Linux server.
Stream Builder Code for firebase