Building App to run Linux command And Getting Output from Firebase
Hey, guys hope you all are doing good in today's blog we are going to learn what is firebase and how can we save data inside it, and how to get output from it.
In this article, we are going to make one app that can run a command on the Linux terminal and for that, we use CGI-bin(Common gateway interface) it's like there is someone in your os who is waiting as soon as you send command it will run particular command CGI-bin can be written in any language it is called server-side language here we are using python and we will make this script on AWS cloud instance don’t worry if you don’t know how to use the cloud just follow same steps as I do in you local system.
Cgi bin Redhat os can be found under the service of https server that comes under apache.
Lets first set up our API to run a command on Linux os using CGI
Step1: download httpd server and download python3 if not exist
yum install httpd=> for downloading httpd server
yum install python3=> for downloading python3
Step2: Once installed enable httpd
systemctl start httpd
systemctl enable httpd
Step3: Remember to stop firewall in Linux os from the following command
systemctl stop firewalld
systemctl disable firewalld
the above command only to use when you are running this on a local system if using cloud then in security group allows all traffic.
Step4: Disable Selinux security by using the following command
setenforce 0
Step5: Now go to the CGI-bin folder and make a file with the name you want here the name of the file is input 1.py
Step6: Now once you create a file make it executable using the final command
chmod +x filename
eg:- chmod +x input1.py
Once all the above steps are done we can go to Andriod studio to build our App.
Here first you need to go to firebase and create one folder for your app and after that make one collection here I use the date command you can use any if you face any difficulty in setting up a firebase with the app then let me know will help you or write a blog on it.
Once you have done up with setting up firebases for the app then we need some library to deploy our app to install library yu can to this Link.
we need the following library
firebase_database
cloud_firestore
firebase_core
http
once you get this library paste the lines in the following way to install in your particular app
Once done with install ling library we are all set to make our app.
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:http/http.dart' as http;
var message = "output ";
var output;
var cmd;
First, write the above code to import all necessary packages we need and define the variable
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
final databaseReference = FirebaseFirestore.instance;
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("linux app"),
),
body: mybody(),
),
);
}
}
From the above code, we have initialized our app to connect to the firebase and the basic structure is made with an app bar.
class mybody extends StatefulWidget {
@override
_mybodyState createState() => _mybodyState();
}
class _mybodyState extends State<mybody> {
web(cmd) async {
print(cmd);
var url = "http://34.204.37.111/cgi-bin/input1.py?x=${cmd}";
var r = await http.get(url);
print(r.body);
setState(() {
output = r.body;
});
createRecord();
}
void Retrive() {
setState(() {
message = "Be patient we are fetching output";
});
databaseReference.collection("date command").snapshots().listen((result) {
result.docs.forEach((result) {
Future.delayed(const Duration(milliseconds: 4000), () {
setState(() {
message = result.data().toString();
});
});
});
});
}
void createRecord() async {
await databaseReference.collection("date command").doc("result").set({
'output': output,
});
Retrive();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
TextField(
onChanged: (b) {
cmd = b;
},
decoration: new InputDecoration(
border: InputBorder.none,
hintText: 'Enter host port you want to attach',
suffixIcon: Icon(Icons.search),
contentPadding: EdgeInsets.all(20)),
),
RaisedButton(
color: Colors.blueAccent,
onPressed: () {
web(cmd);
},
child: Text('Execute'),
),
Container(
child: Text(message),
)
],
);
}
}
Now in the above code, you can see three function web, create a record, Retrieve let's understand this three function else another code is easy to understand if you have basic knowledge of flutter if still have any doubt you can connect to me will help you.
Lets first understand the function name web.
web(cmd) async {
var url = "http://34.204.37.111/cgi-bin/input1.py?x=${cmd}";
var r = await http.get(url);
setState(() {
output = r.body;
});
createRecord();
}
here we create one variable name URL inside it we are saving the URL of our CGI script we create and after that output is being saved to the output variable once output from URL is saved in the output variable then they call create record function.
Now let's understand the function name create record.
void createRecord() async {
await databaseReference.collection("date command").doc("result").set({
'output': output,
});
Retrive();
}
here we are saving the output in filed output inside the collection name date command and doc name result and once it is saved in the output field they call function Retrieve.
Now let’s understand the function name Retrieve.
void Retrive() {
setState(() {
message = "Be patient we are fetching output";
});
databaseReference.collection("date command").snapshots().listen((result) {
result.docs.forEach((result) {
Future.delayed(const Duration(milliseconds: 4000), () {
setState(() {
message = result.data().toString();
});
});
});
});
}
In the above code we change the message from output to Be patient we are fetching output and then the value comes from firebase we again change the message value. Once got done with this now we can print our message where ever we want.
The final app looks as shown below.
Guys, here we come to the end of this blog I hope you all like it and found it informative.
Here is the git hub repo https://github.com/guptaadi123/Linuxapp.git
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.