I have a server that's using TLS 1.0
It gives me an infinite stream of data on the terminal when the command
~$ openssl s_client -connect 'serveraddress':5000
This provides me with a realtime xml data stream of what my server is currently doing.
I want to connect to it using node.js or any other way that provides me with the possibility to push this data stream as a websocket or directly onto a JS, but I can't seem to figure out how. Can any of you please help?
Thanks :)
I think something like that should solve your problem.
var terminal = require('child_process').spawn('bash');
terminal.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
terminal.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
setTimeout(function() {
console.log('Sending stdin to terminal');
terminal.stdin.write("openssl s_client -connect 'serveraddress':5000");
terminal.stdin.end();
}, 1000);
Edit:
Try this for a working example:
terminal.stdin.write("ping www.google.de");
Related
INFO: I'm referring to this question I asked on Super User, but couldn't get an answer and I think this is a good place to ask, since the problem is probably code related.
I'm currently running a simple Node.JS server with express.js on my RaspberryPi with Debian installed on it. Everything works fine, but every morning I wake up to see my server isn't running anymore (the server process I started with the command node main.js).
My first guess was, that the Pi has some kind of sleep mode, which it enters after a couple of hours without traffic/etc, and which shuts down the server, but I also run a dydns-client, which is still up every morning (I also was informed, that the RaspberryPi doesn't come with a sleep mode).
I wrote a simple script to check whether the process is running and writes it into a log file, but today morning I had to notice, that this script was wasn't running as well (only for around two hours, it logs the server state every 15 seconds and the last state was running).
Here is the script:
#!/bin/sh
MATCH="SOME_PROCESS_NAME"
while [ true ]
do
if [ "$(ps -ef | grep ${MATCH} | grep -v grep)" ]; then
echo "$(date): Process is running ..."
else
echo "$(date): Process has ended ..."
fi
sleep 15
done
Is there a way to track a process after I started it to check tomorrow morning, what killed my process or why it ended (the script obviously didn't work)?
The server itself looks pretty simple and I don't think there is some kind of auto-shutdown I missed. Here is the code I used.
var express = require('express');
var path = require('path');
var server = express();
server.use(express.static(path.join(__dirname, 'public')));
server.listen(1337);
console.log("Server listening (PORT: " + 1337 + ") ...");
Any idea what to do, to keep the server running/find out what is the stopping reason?
UPDATE: I received a working answer over at RaspberryPi-stackexchange.
My guess is the Raspberry Pi restarts at midnight or something similar. to fix this maybe add an entry for your server process rc.local file. you can add commands to the rc.local file by editing /etc/rc.local
Would this helps https://raspberrypi.stackexchange.com/questions/8741/when-does-the-os-kill-an-application ?
I would like to suggest a different approach to monitor your process until you can get more information, to edit, then check, then start (wrote on the fly)
var fs = require('fs')
var spawn = require('child_process');
var child = spawn(process.argv[0], 'your/bin.js', {stdio:['ignore', 'pipe', 'pipe']})
child.stdout.pipe(fs.createReadStream('stdout.log'))
child.stderr.pipe(fs.createReadStream('stderr.log'))
child.on('error', function (err) {
fs.writeFile('error.log', JSON.stringify(err), function () { /* void */ })
})
child.on('close', function (code, signal) {
fs.writeFile('exit.log', "code="+code+" signal="+signal, function () { /* void */ })
})
I have the following Node.js code to ssh into a server and it works great forwarding stdout, but whenever I type anything it is not forwarding to the server. How do I forward my local stdin to the ssh connections stdin?
var command = 'ssh -tt -i ' + keyPath + ' -o StrictHostKeyChecking=no ubuntu#' + hostIp;
var ssh = child_proc.exec(command, {
env: process.env
});
ssh.stdout.on('data', function (data) {
console.log(data.toString());
});
ssh.stderr.on('data', function (data) {
console.error(data.toString());
});
ssh.on('exit', function (code) {
process.exit(code);
});
There's two ways to go about this if you want to pipe the process.stdin to the child process:
Child processes have a stdin property that represents the stdin of the child process. So all you should need to do is add process.stdin.pipe(ssh.stdin)
You can specify a custom stdio when spawning the process to tell it what to use for the child process's stdin:
child_proc.exec(command, { env: process.env, stdio: [process.stdin, 'pipe', 'pipe'] })
Also, on a semi-related note, if you want to avoid spawning child processes and have more programmatic control over and/or have more lightweight ssh/sftp connections, there is the ssh2 module.
I've been trying to solve this issue for a few days with absolutely no luck. I have a node.js application that is spawning a child process - it's looking at the STDOUT of that child process and reacting to it. A snippet looks like:
var childProc = spawn('/opt/Game/LaunchGame.sh');
childProc.stdout.on('data', function(data) {
console.log("stdout: " + data);
});
childProc.on('close', function(code){
console.log('child process exited with code ' + code);
});
If I run the bash script by itself, all the log messages appear as I'd expect, but when its run through Node, I never see any "stdout: " messages.
Any ideas?
For some reason this request GET never returns, but only in Node.js. I've tried in browser, curl, httparty, ruby's Net::Http, postman, clojure and they all work fine. Node.js will not.
This will return as you'd expect
echo "require('https').get('https://github.com/jakecraige.json', function(res) { console.log(res); });" | node
This never will return, it eventually throws an ECONNRESET
echo "require('https').get('https://gateway.rpx.realpage.com/rpxgateway/PricingAndAvailability.svc?wsdl', function(res) { console.log(res); });" | node
If anyone could provide some insight into this that would be extremely helpful.
Thanks!
Zyrri in IRC directed me to here which solved the problem.
SSL Error in nodejs
Looks like it wants SSLv3_method from node.js
$ curl 'https://gateway.rpx.realpage.com/rpxgateway/PricingAndAvailability.svc' -I
HTTP/1.1 400 Bad Request
Try providing an error handler and see what you get.
Your code hangs for me whichever URL I pick. I believe that's node trying to make sure we don't exit prematurely, running its event loop just in case. All other techniques you listed are synchronous, not event-driven. Also, res is probably not what you want to print, since it's the node's internal response object. Try this:
cat <<EOF | node
require("https").get("https://github.com/jakecraige.json",
function(res) {
var body = ''
res.
on('data', function(data) { body += data}).
on('end', function() { console.log(body); process.exit(0); });
}
);
EOF
Combining #Constarr links with this original post I have this constructed request getting a valid response.
var https = require('https');
https.globalAgent.options.secureProtocol = 'SSLv3_method';
https.get("https://gateway.rpx.realpage.com/rpxgateway/PricingAndAvailability.svc?wsdl", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Docs here
I am working with node.js, and I am trying to embed a console in the web browser to work in a remote server. The web application do the connection so the user does not need to do the ssh username#host but only type commands.
I have tried the node.js' ssh2 module and other modules which use ssh2. But I'm experiencing always the same problem. Every time I execute a command programmatically using exec(), the ssh session is restarted. I'll explain it better with an example.
> ls
returns the content of home directory, one of the directories in the home directory is mydir
> cd mydir
> ls
returns the content of my home directory again, because after a command is executed the ssh session is closed/restarted.
Any node.js library which can do the job? or even a library of other technology different to javascript?
Edit: Other example for clarifying, using the node.js' module ssh-exec
The server has to execute some commands in other machine using ssh. A function in the server contains the following code
var c = exec.connection('username#host.com'); // It takes the ssh key from the default location
exec('cd mydir', c).pipe(process.stdout);
exec('ls -lh', c).pipe(process.stdout);
As you can see I am not ending the connection after the first exec but the output I obtain is the content of the home directory not the content of mydir directory, because the ssh session is reset after each exec.
The maintainer of node.js' ssh2 module provided the solution.
To use the method shell() instead of the method exec().
The method shell() creates an interactive session with the server we are connecting.
The method shell() provides a stream as a parameter of its callback (like the method exec()).
Like when using exec(), stream.on('data', function(data, extended) {...}); can be used to get the output of the commands. However, in this case, to provide commands (input) to the machine you connected with, you need to use stream.write(yourcommand+'\n');
PS. Feel free to edit to improve the accuracy of the answer.
I have to guess a bit, but you do something like child = exec('ssh username#host ls')?
You can do something like
child = exec('ssh username#host');
upfront and in the "loop" of your browser
child.stdin.write('ls\n');
When finished, just close stdin:
child.stdin.end()
which also finishes the child process.
I know this link is old but I figured this may help someone if they're looking for a solution. The
To use the method shell() instead of the method exec().
Works. Here's another solution. Use absolute file paths i.e.
conn.exec("mkdir -p /home/user/Direc/{one,two,three}/", function(err, stream) {
if (err) throw err;
stream.on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});
conn.exec("ls -la /home/user/", function(err, stream) {
if (err) throw err;
stream.on('close', function(code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});