Capturing console output in node.js with out editing server code - javascript

First stackoverflow question! I am trying to capture all console.log() output from a node.js server. Assume I am on a linux machine and I cannot edit the server's code, (project leader's request). I can, however change how I start the server (node --debug server.js) and I can attach shell scripts. My intent is to pipe the console output into another socket.io server.
I am thinking the only obvious solutions are to either write a linux command to pipe output of the server into something or node.js has a tricky --debug method to output the information.
End result, I want to be able to run the server and pipe the console output to a API testing server. That way, a tester would be able to see, streaming, the output of the server in correlation to the response JSON objects from the API end points.
Thanks, I would love to hear your solutions...

Answered over IRC, if anyone's interested:
<shesek> patrickm, can't you just pipe the output to another script?
<shesek> node server.js | node send-logs-to-somewhere.js
<shesek> patrickm, in send-logs-to-somewhere.js, you can just read from process.stdin
<patrickm> :)
<patrickm> thats easy

Related

Live console input/output for a gameserver using JavaScript or PHP

General information: I have a Minecraft server running in the basement for some friends that is running for a couple months now. We were using TeamViewer to input commands into the console and view possible errors while the server was running. The console of a Minecraft server basically shows you a log of events with timestamps as well as a command input line at the bottom that enables you to execute in-game commands with administrator rights.
The console is started via a batch file:
:startup
#echo off
set v1=6
cls
Java -Xmx%v1%G -jar spigot.jar nogui
goto startup
The spigot.jar file is the file that basically runs the server.
Because we manage multiple servers and the TeamViewer process is quite insecure and slow, i wanted to find other ways to access the console.
Target: I want to create a HTML webinterface that uses JavaScript or PHP to communicate with the server console. It should be able to show the live console log as well as being able to type commands in the browser that are then executed in the commandline on the server.
Problems:
Is there any way to send a JavaScript/PHP input to the console so that I can execute commands from a webinterface?
Can I save the live console output to a .txt file that the JavaScript/PHP script can then display live on the webinterface?
I would definitely recommend moving your setup outside of a Windows environment, as you're going to have a much easier time hosting a PHP server, etc. in a Linux environment.
Admittedly, I'm not familiar with MineCraft, but it looks like the batch file you have is effectively just launching a Java JAR which can be done from any computer with the JRE installed. Java console apps read input on the STDIN of a process and output on STDOUT for any application-generated output. This is incredibly easy to pickup in a Ubuntu environment.
For example, let's say you install the standard LAMP stack on a fresh Ubuntu install. You'll have PHP for running your server-side process, and Apache for hosting any web interface you decide to build. Keeping a focus on frontend vs. backend, I'd probably try to setup an API in PHP using Slim or another lightweight framework. In the API endpoint you use to start your server, it would look something like this:
exex('screen -d -m -S ServerOne');
exec('screen -S ServerOne -p 0 -X stuff "java -Xmx%v1%G -jar spigot.jar nogui > /some_path_to_log^M"');
The first exec() will create a screen session named "ServerOne" inside of which your server process can run. The second exec() will send the command needed to start your server process inside of the screen session. Effectively, this will run your server asynchronously to your PHP script and write the output to the specified path. Knowing where to expect the output, in the API endpoint you use to get your log, you'd have something like:
$logContent = file_get_content('/some_path_to_log');
Finally, in the API endpoint you use to write commands to your server, you would have something that looks like:
exec('screen -S ServerOne -p 0 -X stuff "<your_command_here>^M"');
This will write to the STDIN stream of your server's Java process which should, in turn, be interpreted as if you were actually typing at the keyboard of the console itself.
That's the basics of it, or at least where I'd get started. There are some pretty cool things that you could do with WebSockets to open a live I/O session for your server's console, but it'd be a bit more involved than this post. If you're interested, check out Ratchet for PHP or ws for Node.js.
Consider this ServerFault post for more information on sending input to screen sessions.
I suppose you could show the log on a website like this:
first gameserver script:
:startup
#echo off
set v1=6
cls
Java -Xmx%v1%G -jar spigot.jar nogui > log.txt
fileup log.txt
goto startup
second gameserver script(called fileup.bat){replace myusername, mypassword and servername.com with the credentials if your php server}:
#echo off
echo user MyUserName> ftpcmd.dat
echo MyPassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat SERVERNAME.COM
del ftpcmd.dat
php-server side script:
<?php
$filename = "log.txt";
$fp = fopen($filename, "r");
$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
fclose($fp);
print_r($lines);
?>
by the way: I have not tested any of these scripts and they might not work, I just based this on my knowledge of php and batch. Also, the second batch script needs to be in the same directory as the first batch script and you do not have to run the second batch script
Note:
I am an experienced PHP user but I do not know very much about batch(I use linux) but I suppose this will work and I have used batch very often in the past. Also I recommend using the PHP script on an external hosted php server from 000webhost.com or infinityfree.net because they are free and save you a lot of time + they have all the needed ftp things preconfigured.
Please let me know if this worked

Handle global variable in node.js server

I have a node.js server that reads messages from a rabbitmq server. Every message contains an url that returns a json object whit specifications to download some jsx code.
The node.js server gets the code from the urls and compiles it with webpack.
My problem is that I need to keep aware of the information of the json objects in the webpack compilation instance, because I need to print the downloaded objects in the index page.
Node Server -- Get messages --> RabbitMQ Server
RabbitMQ Server -- Return messages --> Node Server
Node Server -- Get code [from URL] --> URL service
URL service -- Return code --> Node Server
Node Server: Compile downloaded code.
I don't know if I was clear. I tried to use global variables and module.exports, but did not work. Maybe I am missing something, I am a kind of beginner in JS, node and webpack.
Could you cache those in memory, that way they would be available to access.
One of the popular module is memory-cache
Though memory caching comes in with it's own set of limitations.
Hope I understood the question correct.

where to find console in Node-RED

I can't find the console for Node-RED.
Do I have to use Node-RED Runtime API's RED.log()? Some Node-RED frontends, like FRED, seem to have a console. Also here a console is mentioned.
Is there a console accessible on the shell, or do I have to use the debug-Node?
Thanks in advance
Peter
Node-RED outputs data to the terminal it is started on.
So if you start it manually on the command line with node-red or node red it will print to the terminal.
If you've installed Node-RED as a service so it is launched at startup then it will depend on what platform you are on as to where the console log will end up. On Linux then there is a fair chance that it will end up in the journal and can be tailed with the following command:
sudo journalctl -f -u nodered -o cat
If you're running Node-RED on Bluemix then the you can see the console with the cf command line too:
cf logs <app-name>
or you can see it in the Bluemix web console.
As for sending data to this output then you have a few of options.
The Debug node has an option to output to the console, this is useful when the msg object (or msg.payload) is too big to fit in the debug side bar.
When writing your own nodes then you can use RED.log, This will add a time stamp and a node id to the output which can be useful when debugging.
At a pinch you can use console.log, but this will just dump things to standard out with no formatting or time stamp

Can I send commands using node.js?

I'm running a node.js server using Terminal (Mac) and want to be able to emit messages to the client via the command line. Is this possible? I could code the messages part of it no problem, it's more how I can send something directly from the server to the client rather than from the client-server-client relationship I only understand at the moment.
This is probably what you're after if not let me know and I'll delete answer.
Look into a module called Commander found here.
It will allow you to build up a cli that you can then hook into your methods/prototypes etc.
For Matt :)
var program = require('commander');
program.option('-s, --send', 'Send command');

Writing a pseudo terminal in python for a web based ubuntu terminal

I am interested in writing a web based terminal which can execute and autocomplete the commands in ubuntu terminal.
I have fiddled with Anyterm http://www.anyterm.org which uses ROTE: "a simple C library for VT102 terminal emulation" at the backend server and javascript/html for the terminal ui. The problem was that I couldn't have it open two terminals in the same window.
After doing some research I've decided to write a terminal server/pseudo terminal (whichever is the right terminology) in python which can:
execute commands in ubuntu terminal (such as ls, grep etc...)
autocomplete the commands (like $gedi (pressed tab) $gedit)
and have multiple instances of terminal open at the same time (when typed ls in one, the other terminal won't recognize ls as the last command since its another terminal session)
I have found that with pty python module I can write a pseudo terminal
however I'm new to python and I couldn't even get the example on that page to work.
I'm planning to have the python script serve a http server with the javascript/html terminal ui I would write (this was the main reason I wanted to choose python for this project). Then I want to let the web ui talk to the backend terminal "server" and get the results back to the web ui.
If you could point me in the right direction, maybe tell me which module I should use -if not pty- and give me some ideas on how to write the python pseudo terminal server I would appreciate it.
I know javascript/php and familiar with bash scripting- At this point I could go for a C based or python based backend server, is python right for this project?
Note: I'm planning to write a ui that uses ajax post or get methods to retrieve the terminal output from backend server.
You need to learn the basic terminology first. The shell is the program that interprets your input command lines, such as grep "foo" abc.txt. A terminal emulator is a program that mimics a terminal. Terminal is a device with display and keyboard that one
used in 1970s to access a UNIX mainframe. A pseudoterminal, pty is the device node supporting terminal emulators, as opposed to "real terminal devices" (tty1) for example.
Now, you could either build a shell, that would work in terminal instead of bash or dash; or you could build a terminal emulator that is usable over the internet; you could run any existing shell in it. Both are challenging tasks by themselves.
Python documentation is not of much use, I recommend you should start by googling more of these concepts first - and not only python references, but C, C++ too.
And lastly, if you want to run Emacs or nano or some other advanced program in your shell, you want to use the pty module.
I had a friends who did something similar but he did it over Google chat, I don't know how much help it will be but take a look at xmpp (python modual) maybe it could help you, or even here is a link to his source code:
chatIO
I didn't work with him on it but it was really easy for him to use
Good Luck

Categories