I want to use node.js to parse a remote log - javascript

I have been trying to use node.js to follow a log file that is on a remote server. I used "sequest" to connect to the server. No problem there. It even allows me to run commands, but when I try "tail -f" it fails doesn't return anything. Removing the -f works, I asume it is because there command is not "done" so there is nothing to return yet? Am I missing something? Is there another alternative to get the output of a 'tail -f' command on a remote location?
var sequest = require('sequest');
var conf = require('./config/properties.js');
var prop = conf.ssh.dev;
var seq = sequest.connect(prop.host,{password:prop.password});
seq('tail -f -n 100 /interwoven/LiveSiteDisplayServices/runtime/tomcat/logs/catalina.out', function (e, stdout) {
console.log(stdout.split('\n'));
});

Following Adrian Lynch recommendation I used piping specified in the sequest documentation at npmjs.com/package/sequest. It ended up looking something like this:
var sequest = require('sequest');
var conf = require('./config/properties.js');
var prop = conf.ssh.dev;
var seq = sequest(prop.host, { password: prop.password});
seq.pipe(process.stdout);
seq.write('tail -f -n 100 /interwoven/LiveSiteDisplayServices/runtime/tomcat/logs/catalina.out');

Related

at WebSocket.socket.onerror in LiveQueryClient.js when start app

I have a problem with my small demo for live query as in attached image.
To be honest, I have no idea why does it go wrong. Try to find some solutions around but not yet successfully. Please give me some ideas or solutions if you know this issue. Thanks so much.
Parse server example: 1.4.0
Parse JS SDK: 1.10.2
NodeJS: 8.9.1
npm: 5.5.1
P/S: I have added classes for supporting by Live Query already.
Here is the source which run successfully without using Live Query
Link to src with removed parse link:
var Parse = require('parse/node');
Parse.initialize("shetei5aeJ9Aequi6deejojiv7foh6Hieg2eesh9keingohw");
Parse.serverURL = 'serURLhere';
var Node = Parse.Object.extend('Node');
var q = new Parse.Query('Node');
var subscription = q.subscribe();
var procEventOpen = () => {
console.log('subscription opened...');
};
subscription.on('open', procEventOpen);
This happened to me when I had a typo in server url.
Try to explicit specify liveQueryServerURL parameter:
Parse.liveQueryServerURL = 'ws://yourserverurl/parse';
Note the ws instead of http(s)

Invoke pre-parsed command line with Node.js

I need to invoke the following command, where password is user input. However, I am worried about the possibility of an attack, such as "; rm -rf / ;" being the input given by the user.
var checkPassword = exec('echo "'+password+ '"| cracklib-check\n', function(err, stdout, stderr) {
...
...
}
is there a way to invoke the command with pre-parsed arguments (preferably native to nodejs/ javascript), kind of like prepared statements which are used to avoid SQL injection?
I could probably avoid the problem by blacklisting certain characters, but that seems much less reliable, and I'd like to avoid it is possible.
As you point out, building a command line with user provided input is a security issue. Typically you would write a wrapper that verifies that each user-provided parameter meets a white-list before invoking the command.
In your case however there is a simpler solution: you are constructing a command line that simply sends the password to the stdin of cracklib-check. Instead of using child_process.exec you can switch to child_process.spawn which allows you to write directly to stdin, avoiding the need to build a command line with user-provided input.
The following sample code avoids the security problem:
const spawn = require('child_process').spawn;
// Read password from argument to nodejs invocation
var password = process.argv[2];
// Spawn cracklib-check
var cracklib_check = spawn("/usr/sbin/cracklib-check");
// Send password to cracklib-check STDIN
cracklib_check.stdin.write(password);
cracklib_check.stdin.end();
// Process results of cracklib-check
cracklib_check.stdout.on('data', function (data) {
console.log("[*] " + data);
});
cracklib_check.stderr.on('data', function (data) {
console.log("[-] " + data);
});
#Ilmora's answered me started, but I still had to handle encoding.
const spawn = require('child_process').spawn;
// Read password from argument to nodejs invocation
var password = process.argv[2];
var cracklib_check = spawn('/usr/sbin/cracklib-check');
cracklib_check.stdin.setEncoding = 'utf-8';
cracklib_check.stdin.write(password);
cracklib_check.stdin.end();
// Process results of cracklib-check
cracklib_check.stdout.on('data', function (data) {
console.log("[*] " + data.toString());
});
cracklib_check.stderr.on('data', function (data) {
console.log("[-] " + data.toString());
});

Improper parsing of strings

I'm trying to convert ansi color codes from console output into HTML. I have a found a script to do this but I cant seem to make it parse the strings inside node js. I have tried to JSON.stringify it to also include special chars but its not working.
forever list
[32minfo[39m: Forever processes running
[90mscript[39m [37mforever[39m [37mpid[39m [37mid[39m
[90mdata[39m: [37m [39m [37muid[39m [90mcommand[39m
I get output like this back from ssh2shell in node js. I have a script:
https://github.com/pixelb/scripts/blob/master/scripts/ansi2html.sh
This is supposed to convert the above to html and add the appropriate color codes. It works fine with normal terminal output for example:
npm install --color=always | ansi2html.sh > npminstall.html
This is the raw output on the linux machine piped to a file. It seems the JS strings are missing these escapes when they are shown in console.log but they are also missing newlines there. Perhaps its because im concatenating them directly into the string and its removing special chars?
total 24
-rwxr-xr-x 1 admin admin 17002 May 13 02:52 ^[[0m^[[38;5;34mansi2html.sh^[[0m
drwxr-xr-x 4 admin admin 4096 May 13 00:00 ^[[38;5;27mgit^[[0m
-rw-r--r-- 1 admin admin 0 May 13 02:57 ls.html
Hopefully some of this makes sense.
Thanks
There are a couple of filters that SSH2shell applies to the output from commands. The first removes non-standard ASCII from the response and then the colour formatting codes are removed.
In v1.6.0 I have added pipe()/unpipe(), the events for both and exposed the stream.on('data', function(data){}) event so you can access the stream output directly without SSH2shell interacting with it in any way.
This should resolve the problem of not getting the right output from SSH2shell by giving you access to the raw data.
var fs = require('fs')
var host = {
server: {
host: mydomain.com,
port: 22,
userName: user,
password: password:)
},
commands: [
"`Test session text message: passed`",
"msg:console test notification: passed",
"ls -la"
],
}
//until npm published use the cloned dir path.
var SSH2Shell = require ('ssh2shell')
//run the commands in the shell session
var SSH = new SSH2Shell(host),
callback = function( sessionText ){
console.log ( "-----Callback session text:\n" + sessionText);
console.log ( "-----Callback end" );
},
firstLog = fs.createWriteStream('first.log'),
secondLog = fs.createWriteStream('second.log'),
buffer = ""
//multiple pipes can be added but they wont be bound to the stream until the connection is established
SSH.pipe(firstLog).pipe(secondLog);
SSH.on('data', function(data){
//do something with the data chunk
console.log(data)
})
SSH.connect(callback)
tried this ?
https://github.com/hughsk/ansi-html-stream
var spawn = require('child_process').spawn
, ansi = require('ansi-html-stream')
, fs = require('fs')
var npm = spawn('npm', ['install', 'browserify', '--color', 'always'], {
cwd: process.cwd()
})
var stream = ansi({ chunked: false })
, file = fs.createWriteStream('browserify.html', 'utf8')
npm.stdout.pipe(stream)
npm.stderr.pipe(stream)
stream.pipe(file, { end: false })
stream.once('end', function() {
file.end('</pre>\n')
})
file.write('<pre>\n');

How can I get terminal size in a piped node.js process?

I'm using Grunt to kick off a unit-test framework (Intern), which ultimately pipes another node.js process that I'm then using Charm to output results to the screen. I'm having to pass in the terminal size information from a Grunt config option, but it's a bit messy and I'd like to try and get the terminal size from within the piped process, but the standard process.stdout.cols/getWindowSize are simply unavailable as the piped process doesn't register as TTY (although Charm works fine with it all).
Any suggestions?
EDIT Just to be clear here ... the Grunt JavaScript file is running in the main node.js process, but the file I'm attempting to retrieve this info from (and where I'm therefore running people's suggested commands) is in a spawned child process.
Try these:
tput cols tells you the number of columns.
tput lines tells you the number of rows.
echo -e "lines\ncols"|tput -S to get both the lines and cols
There's stty, from coreutils:
$ stty size #60 120 <= sample output
While running the below code in terminal prints the cols:
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("tput cols", puts);
The pty.js module can make a child act like a regular terminal.
var pty = require('pty.js');
var term = pty.spawn('bash', [], {
name: 'xterm-color',
cwd: process.env.HOME,
env: process.env
});
term.on('data', function(data) {
console.log(data);
});
term.write('ls\r');
term.resize(100, 40);
term.write('ls /\r');
console.log(term.process);

Using node.js with browserless jQuery via the command line

I am trying to run this script below via the command line
var argv = require('optimist').argv,
$ = require('jquery'),
fs = require('fs');
var file = argv._[0];
var html = fs.readFileSync(file, 'UTF-8');
$(html).find('p').each(function(index) {
var content = $(this).html();
console.log('Paragraph ' + (index + 1) + ': ' + content);
}); //script.js
The command is $ node script.js page.html where page.html is the argument
The error I get is:
./node_modules/jquery/dist/jquery.js:29
throw new Error( "jQuery requires a window with a document" );
Error: jQuery requires a window with a document
at module.exports (./node_modules/jquery/dist/jquery.js:29:12)
...
I am using jquery-2.1.3. I know that this used to work, once upon a time, but it looks like something has changed.
Also I did follow the instructions at http://rkulla.blogspot.com/2014/04/using-browserify-with-jquery-backbonejs.html but I am still getting the same error.
Any help to fix this error would be greatly appreciated. Thank you very much!
See this answer. The npm package for jQuery no longer includes jsdom, which provides the DOM environment needed to work with jQuery on the server.
On the other hand, you could just use cheerio, an implementation of jQuery for the server, to parse the html and use jQuery's selectors.
var argv = require('optimist').argv,
$ = require('cheerio'),
fs = require('fs');
var file = argv._[0];
...
I could not install jsdom for the life of me so I gave up on that, but cheerio (npm install worked very smoothly after editing package.json) solved the problem (see code modification above). Thank you!

Categories

Resources