Flutter app that can run any linux command using API , output will be stored in Firestore

Flutter app that can run any linux command using API , output will be stored in Firestore

Task 3 :

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.

Linux

Migrate workloads across environments with the operating system (OS) engineered for the hybrid cloud. Red Hat Enterprise Linux 8 gives organizations a consistent OS across public, private, and hybrid cloud environments. It provides version choice, long life-cycle commitments, a robust ecosystem of certified hardware, software, and cloud partners, and now comes with built-in management and predictive analytics.

We write a CGI Program here to communicate with Flutter App

#!/usr/bin/python3

import os

import subprocess




import cgi




print("content-type: text/html")

print()




mydata = cgi.FieldStorage()

mycmd = mydata.getvalue("x")




output = subprocess.getoutput(mycmd)

print(output)

Flutter Dart Code

In addition to mobile apps, Flutter supports the generation of web content rendered using standards-based web technologies: HTML, CSS and JavaScript. With web support, you can compile existing Flutter code written in Dart into a client experience that can be embedded in the browser and deployed to any web server. You can use all the features of Flutter, and you don’t need a browser plug-in.

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';


void main() {
  runApp(LinuxPage());
}




class LinuxPage extends StatefulWidget {
  @override
  _LinuxPageState createState() => _LinuxPageState();
}


class _LinuxPageState extends State<LinuxPage> {
      
  var msgtextcontroller = TextEditingController();


  var fs = FirebaseFirestore.instance;
  var authc = FirebaseAuth.instance;


  String msg;


  @override
  var cmd;
  var outputdata;


  Linuxinput(cmd) async {
    var url = "https://192.168.56.101/cgi-bin/task3_Linux.py?x=${cmd}";
    var r = await http.get(url);


    setState(() {
      outputdata = r.body;
      StreamBuilder<uiState.UiState> builder = new StreamBuilder(
      stream: new uiState.StateSubject().screenEvent(),
      builder: (context, asyncSnapshot) {
        
      return outputdata;
      });
    });
  }


  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: SizedBox(
          child: Text(
            "Linux",
            style: TextStyle(height: 1, fontSize: 26),
          ),
        ),
        backgroundColor: Colors.black,
      ),
      backgroundColor: Colors.black,
      resizeToAvoidBottomPadding: false,
      body: SingleChildScrollView(
        child: Center(
          child: Container(
              color: Colors.black,
              width: MediaQuery.of(context).size.width * 1,
              height: MediaQuery.of(context).size.height * 1,
              margin: EdgeInsets.all(0),
              child: Column(
                children: [
                  Text(
                    "Linux Controller",
                    style: TextStyle(
                        color: Colors.red,
                        fontSize: 35,
                        fontWeight: FontWeight.bold),
                  ),
                  Text(
                    "8",
                    style: TextStyle(
                        color: Colors.red,
                        fontSize: 50,
                        fontWeight: FontWeight.bold),
                  ),
                  Text("     "),
                  Container(
                    width: MediaQuery.of(context).size.width * 0.5,
                    height: MediaQuery.of(context).size.height * 0.1,
                    child: TextField(
                      obscureText: false,
                      decoration: InputDecoration(
                        suffixIcon: IconButton(
                            icon: Icon(
                              Icons.send,
                              color: Colors.red,
                            ),
                            onPressed: () async{
                              msgtextcontroller.clear();
                              await fs.collection("chat").add({
                          "text": msg,
                          
                        });
                              Linuxinput(cmd);
                            }),
                        enabled: true,
                        enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(10.0),
                          borderSide: BorderSide(color: Colors.green),
                        ),
                        labelText: 'Linux Command',
                        labelStyle: TextStyle(color: Colors.green),
                      ),
                      enabled: true,
                      autocorrect: false,
                      autofocus: false,
                      cursorColor: Colors.green,
                      showCursor: true,
                      onChanged: (value) {
                        cmd = value;
                      },
                      keyboardAppearance: Brightness.dark,
                      style: TextStyle(
                        color: Colors.green,
                      ),
                    ),
                  ),
                  Text("       "),
                  Text(
                    outputdata ?? "Loading...",
                    style: TextStyle(color: Colors.green),
                  ),
                ],
              )),
        ),
      ),
    );
  }
}

Hence using this we can utilise our linux terminal using Flutter and store output in Cloud Firestore

No alt text provided for this image

Done !!!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了