python,如何在远程主机上运行命令并在GUI中实时显示输出? [英] python, how to run commands on remote hosts and show output in GUI in real time?

查看:49
本文介绍了python,如何在远程主机上运行命令并在GUI中实时显示输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个真正的开放式问题,但我是 Python 新手,正在构建一个简单的单机网络应用程序,以便为非技术团队提供一些自助服务功能.这个团队有一堆重复性的任务,他们把这些任务交给另一个团队,这些任务只是乞求自动化,比如在远程主机上重新启动一些进程、grep 日志、清理旧文件、部署/重新启动应用程序的新版本、获取最新信息运行版本等.用户将单击按钮并观看 GUI 中的输出,他们不会手动输入要运行的命令(我知道这很危险).任何新任务都将从技术支持团队编写并添加到应用程序中.

I know this is a real open-ended question, but I'm new to python and am building a simple one off web app to give a non technical team some self service capabilities. This team has a bunch of repetitive tasks that they kick over to another team that are just begging to be automated, like restarting a few processes on remote hosts, grep logs, cleanup old files, deploy/restart new versions of an application, get current running versions, etc. The users will be clicking buttons and watching the output in the GUI, they WILL NOT be manually entering commands to run (I know this is dangerous). Any new tasks will be scripted and added to the app from the technical support team.

现在,我唯一不确定的是如何将命令的(近)实时输出返回到 GUI.我过去在 PHP 中构建了一个非常相似的应用程序,我所做的是将远程命令的输出刷新到 db,然后使用 ajax 轮询 db 并附加新的输出.它非常简单并且工作得很好,即使输出会分块返回(我将输出逐行写入 GUI,所以它看起来是实时的).有一个更好的方法吗?我正在考虑使用网络套接字将命令的输出推回 GUI.好主意?馊主意?使用 python 库有什么更好的吗?我也可以使用 nodejs,如果这有什么不同,但我对这两种语言都是新手(我已经启动并运行了一个简单的 python flask 应用程序,它充当 API 来将几个业务应用程序粘合在一起,没什么大不了的在节点中重写).

Now, the only piece I'm not sure on is how to get (near) real time output from the commands back to the GUI. I've built a very similar app in PHP in the past, and what I did was flush the output of the remote commands to a db, and then would poll the db with ajax and append new output. It was pretty simple and worked great even though the output would come back in chunks (I had the output written to the GUI line by line, so it looked like it was real time). Is there a better way to do this? I was thinking of using web sockets to push the output of the command back to the GUI. Good idea? Bad idea? Anything better with a python library? I can also use nodejs, if that makes any difference, but I'm new to both languages (I do already have a simple python flask application up and running that acts as an API to glue together a few business applications, not a big deal to re-write in node).

推荐答案

这是一个广泛的问题,但我会给你一些线索.

This is a broad question, but I'll give you few clues.

很好的例子是 LogIo.一旦你愿意运行一些命令而不是将输出推送到 GUI,使用 Node.js 就成为自然的方法.此应用可能包含的元素很少:

Nice example is LogIo. Once you are willing to run some commands and than push output to GUI, using Node.js becomes natural approach. This app may contain few elements:

  • 第一部分,运行命令并收集输出并将其推送到
  • 第二部分,接收输出并将其保存到数据库/文件.保存后,这部分正在向
  • 抛出事件
  • 第三部分,应该是一个 websocket 服务器,它将处理在线用户并将事件分发给
  • 第四部分,这是预先编写好的脚本 GUI,能够通过 websocket 连接到第三部分,即登录用户、接收事件并将其广播到其他 GUI 元素.
  • part one that runs commands and harvests output and pushes it to
  • part two that receives output and saves it to DB/files. After save, this part is throwing event to
  • part three, that should be a websocket server, which will handle users that are online and distribute events to
  • part four, which would be preoperly scripted GUI that is able to connect via websocket to part three, log-in user, receive events and broadcast them to other GUI elements.

一旦我假设您觉得 PHP 比 Python 更强大,因为您最简单的方法是创建第二部分作为处理输入的 PHP 服务(将收获的输出保存到数据库),然后比方说使用 UDP 包来分割三部分的 UDP 监听套接字.

Once I assume you feel stronger with PHP than python, for you easiest approach would be to create part two as a PHP service to handle input (save harvested output to db) and than, let say use UDP package to part three's UDP listening-socket.

第一部分将是python脚本,用于获取命令输出并将其正确绕过第二部分.它应该像往常一样容易处理 grep 案例:

Part one would be python script to just get command output and bypass it properly to part two. It should be as easy to hadle as usual grep case:

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

在开发它的某个时候,您将需要在 myharvester 之后将用户或 unical 任务 ID 作为参数传递给那里.

at some point of developing it you will be in demand of passing there also user or unical task id as parameter after myharvester.

第三部分中创建一个 Node.js 脚本很棘手,但比您想象的要容易.作为单实例脚本,它应该能够接收输入并将其作为事件绕过给用户.我以前犯过这样的事情:

Tricky but easier than you think will be to create a Node.js cript as part three. As a single instance script it should be able to receive input and bypass it to users as events. I've commited comething like this before:

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's 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.
});

这个废纸是基本的例子,没有填满你需要的逻辑.脚本应该能够在给定端口上打开 UDP 侦听器并侦听在 websockets 中运行到它的用户.老实说,一旦你熟练使用 Node.js,你可能想用它来修复 第二部分 + 第三部分,什么会让你失去 UDP 部分,因为收割机会将输出直接推送到脚本,这维护里面的websocket.但它有一个缺点,就是从其他后端复制一些逻辑作为 CRM.

This scrap is basic example of not filled with logic what-you-need. Script should be able to open UDP listener on given port and to listen for users running into it within websockets. Honestly, once you become good in Node.js, you may want to fix both part two + part three with it, what will take UDP part off you as harvester will push output directly to script, that maintains websocket inside it. But it has a drawback of duplicating some logic from other back-end as CRM.

最后(第四)部分是使用 JavaScript 实现 Web 界面,将当前登录的用户连接到套接字服务器.

Last (fourth) part would be to implement web interface with JavaScript inside, that connects currently logged user to socket server.

我以前使用过类似的方法,它是实时工作的,因此我们甚至可以在电话真正开始响铃之前向我们的呼叫中心员工显示有关来电的信息.最后,解决方案(不包括 CRM 的接口)在两个脚本中关闭 - 专用的 CRM API 部分(所有逻辑都在这里发生)来处理来自 Asterisk 和 Node.js 事件转发器的事件.

I've used similar approach before, and it is working real-time, so we can show our Call-Center employees information about incoming call before even phone actually start to ring. Finally solutions (not counting interface of CRM) closes in two scripts - dedicated CRM API part (where all logic happen) to handle events from Asterisk and Node.js event forwarder.

这篇关于python,如何在远程主机上运行命令并在GUI中实时显示输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆