Linux integration with flutter

Yashraj Panda
4 min readOct 5, 2020

In this blog we will see how we can make a flutter application where we will integrate Flutter with Linux. In this app we will run basic Linux commands and display the output on the screen. Flutter App will fetch the command output from Linux Webserver & store the output on the Firestore. The flutter app will also have the functionality to retrieve the data from the Firestore.

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.

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.

First we have to create the app using the shown command in command prompt,

App Creation:

Let’s start with setting up of firebase,

Now After creating this app, We first login in to the firebase console and from their we will first create our project.

After creating the project we will first create the database in firebase using cloud firestore.

click on the create database icon.

After setting up the firestore service we will next go for the authentication service. (But this step is purely based on user preferences, whether or not the user wants to make the app secure by taking login credentials of the user). Go to the sign-in method and enable he email/password option. After this you can either manually create the user using the console or we can use our app also to add new user.

Coding Part

Now for the code to run successfully there are some libraries which we need to install in our app. These libraries are based on the user’s preferences and can be installed from pub.dev into the pubspec.yaml file. And make sure to restart the ide after saving this file. For codes you can go through my github.

Codes to be written on linux

To run Linux command on flutter, it is mandatory to create API. The API creation code is shown below. This code will create one API which will help to run the Linux Commands using a webserver.

Write this code in a file, save it with “.py” extension in the folder “/var/www/cgi-bin”.

Make this file executable by running the command “chmod +x <filename>”.

Some demonstration images

This Function that will execute and fetch the http response from the Linux server.
This is the final look of the app where you can see the date command running
The above shown function will retrieve the data stored in firebase and show it in the app screen

THANK YOU!!!!

--

--

Yashraj Panda

A B.tech undergrad, enthusiastic towards learning new technologies in the market and integrate the technologies with each other.