Python, how to run commands on remote hosts and show real-time GUI output?

I know this is a real open-ended question, but I'm new to python and am creating a simple single web application to provide some non-technical team some self-service capabilities. This command has many repetitive tasks that they kick into another command that just asks to automate, for example, restart several processes on remote hosts, grep logs, clean old files, deploy / restart new versions of the application, get the current running versions, etc. d. Users will click the buttons and watch the output in the graphical interface, they will NOT manually enter the commands to run (I know this is dangerous). Any new tasks will be written off and added to the application from the technical support group.

Now, the only part I'm not sure about is how to get (almost) real-time output from commands back to the GUI. In the past, I created a very similar application in PHP, and what I did was to clear the output of the remote commands from db, then poll db with ajax and add new output. It was pretty simple and worked fine, although the output was returning to pieces (I had the output written in a GUI, so it looked like it was in real time). Is there a better way to do this? I was thinking about using web sockets to bring the output of the command back to the GUI. A good idea? Bad idea? Anything better with python library? I can also use nodejs if that makes any difference, but I'm new to both languages ​​(I already have a python flask application,which works as an API to glue several business applications together, and not a big deal to rewrite in node).

+1
source share
1 answer

This is a broad question, but I will give you some tips.

A nice example is LogIo . After you want to run some commands and then press the output in the GUI, using Node.js will become a natural approach. This application may contain several elements:

  • part one, which runs the commands and collects the output and clicks on
  • part two, which receives the output and saves it in DB / files. After saving, this part passes the event to
  • part three, it should be a websocket server that will handle users on the network and distribute events to
  • , , websocket , , GUI.

, PHP, python, PHP ( db), , , UDP UDP .

python script, . , grep:

tail -f /var/log/apache2/access.log | /usr/share/bin/myharvester 

- myharvester.

, , , Node.js cript . script . :

var config = {};
var app = require('http').createServer().listen(config.server.port);

var io = require('socket.io').listen(app);

var listenerDgram = require('dgram').createSocket('udp4');
listenerDgram.bind(config.listeners.udp.port);

var sprintf = require('sprintf').sprintf;

var users = [];

app.on('error', function(er) {
    console.log(sprintf('[%s] [ERROR] HTTP Server at port %s has thrown %s', Date(), config.server.port, er.toString()));
    process.exit();
});

listenerDgram.on('error', function(er) {
    console.log(sprintf('[%s] [ERROR] UDP Listener at port %s has thrown %s', Date(), config.listeners.udp.port, er.toString()));
    process.exit();
});

listenerDgram.on('message', function(msg, rinfo) {
    // handling, let say, JSONized msg from part two script,
    // buildinf a var frame and finally
    if(user) {
        // emit to single user based on what happened
        // inside this method
        users[user].emit('notification', frame);
    } else {
        // emit to all users
        io.emit('notification', frame);
    }

});

io.sockets.on('connection', function(socket) {
    // handling user connection here and pushing users' sockets to
    // users aray.
});

, . script UDP , websockets. , Node.js, , , UDP , script, websocket , back-end CRM.

() , - JavaScript , .

, , Call-Center , . , ( CRM) - CRM API ( ) Asterisk Node.js .

+1

All Articles