简体   繁体   English

返回使用Node.js实时运行程序的结果?

[英]Return the results of running a program in realtime using Node.js?

Is there a way to run a program (for example: tcpdump) and every time it shows something through the console, from nodejs capture it and return the result to print in an html?有没有办法运行一个程序(例如:tcpdump),每次它通过控制台显示一些东西时,从 nodejs 捕获它并返回结果以在 html 中打印? no need to save it, keep it in real time.无需保存,实时保存。

My idea is that a program can be executed and while it is running and showing things in the console, they can be sent to the html where you can progressively see all the results of the program's execution.我的想法是一个程序可以被执行,当它运行并在控制台中显示内容时,它们可以被发送到 html,在那里您可以逐步查看程序执行的所有结果。

Thanks for help.感谢帮助。

I think using socket io is a good solution, check there get started tutorial我认为使用套接字 io 是一个很好的解决方案,请查看入门教程

No need to do anything like "capturing output" in Node.js.无需在 Node.js 中执行“捕获输出”之类的操作。 You can use the heredoc available in almost every shell.您可以使用几乎所有 shell 中可用的 heredoc。 The heredoc, in the most basic sense, redirects the output of a command to a file.从最基本的意义上说,heredoc 将命令的 output 重定向到文件。 In your example, if you use tcpdump , here's what you'd do:在您的示例中,如果您使用tcpdump ,请执行以下操作:

$ tcpdump [options] >> file.html
  • The >> heredoc operator appends to a file(create if doesn't exist). >> heredoc 运算符附加到文件(如果不存在则创建)。 This means if you already have some content in file.html, the content will still be there, and the output will be appended to the end of the file.这意味着如果文件.html 中已经有一些内容,则内容仍然存在,并且 output 将附加到文件末尾。
  • The > heredoc writes to a file(also, create if doesn't exist). > heredoc 写入文件(如果不存在,则创建)。 So, if you have some content in file.html, that will be overwritten with the new content.因此,如果您在 file.html 中有一些内容,则会被新内容覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM