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 the Linux CLI Mobile App In this Tutorial, we will Use Stream Builder To Access the Firebase.
what is Stream Builder: Refer This Video For detailed explanation: click Here
Welcome to my Demonstration Project article based on Linux Remote CLI Flutter Mobile App.
This is a demonstration project, based on the development of the Linux Remote CLI Flutter application using the 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 the screen.
4. Use Stream Builder to save data.
What is the 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 access to the physical server to perform any operation on it.
In this project article, we will learn how to make an 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 hyperlink 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 the response from a 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 are 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 as those shell 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 to 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 an extension. 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 the following content. Before running your CGI program, make sure you have change the mode of the file using chmod 755 linux.py UNIX command to make the 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 the web server and ultimately to your CGI Program. Most frequently, the browser uses two methods two passes this information to a web server. These methods are GET Method and POST Method.
Passing Information using the 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 the GET method if you have a 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 to 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 a simple Interface flutter app to perform Linux commands.
To know how to Connect Firebase Firebase Storage Refer to This article: https://firebase.flutter.dev/docs/firestore/usage/
This is a demonstration image: Enter any Command in the Text field and execute that command remotely.
A function that will execute and fetch the HTTP response from the Linux server.
Stream Builder Code for firebase