Linux Remote CLI Flutter Mobile App using python — CGI Programming.

Sparsh
5 min readOct 4, 2020

--

Linux CLI Mobile App using Firebase, Redhat , Linux Flutter Integration.

Welcome to my Demonstration Project article based on Linux Remote CLI Flutter Mobile App .

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 3 : 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.

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.

Web Browsing

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 −

<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

<Directory "/var/www/cgi-bin">
Options All
</Directory>

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.

#!/usr/bin/python3import cgi
import subprocess
print("content-type:text/html")
print()
mydata=cgi.FieldStorage()
output=subprocess.getoutput("sudo " +mydata.getvalue("x"))
print(output)

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 −

"http://192.168.84.130/cgi-bin/docker.py?x=${mycmd}";http://<serverIP>/cgi-bin/pythonfile.py?x=${mycmd}
//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:

sestatus

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.
This function will send the save the command Execution Result in firestore storage
Execute this function to retrieve the response back form Firebase storage.

Firebase Cloud Firestore Console.

Thank you for reading the article hope you liked it . Github Link for flutter App : https://github.com/sparsh308/sparsh308-Flutter_Redhat_Firebase_integration

--

--