How can I exec the ping command in nodejs on lambda? - node.js

This runs locally and returns the ping output:
var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
}
execute("ping -c 3 localhost", function(name){
console.log(name);
});
Running this in lambda it completes but I never see output:
exports.handler = (event, context, callback) => {
var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
}
execute("ping -c 3 localhost", function(name){
console.log(name);
});
};
How do I get it to show output?

Sadly there is no way to do ICMP pings from inside AWS Lambda currently - the main issue is that the container environment that Lambdas run inside lacks the CAP_NET_RAW capability needed to allow an application to use raw sockets.
There's no way around this, even trying to use the command line ping utility inside the Amazon Linux container the Lambda runs inside of won't work.
Source:https://github.com/jethrocarr/lambda-ping.
They also proposed a solution you can try.

Related

How to enable access https from curl command in nodejs

My bash emulator can run curl command correctly. But when I call it within nodejs with child_process module, I get an error refering to Protocol "'https" not supported or disabled in libcurl.
When I run "curl 'https://ehire.51job.com/Candidate/SearchResumeNew.aspx'" I can get a page content.
Here's the nodejs code below:
var child_process = require("child_process");
var curl = "curl 'https://ehire.51job.com/Candidate/SearchResumeNew.aspx'";
var child = child_process.exec(curl, (err, stdout, stderr) =>
{
console.log(stdout);
console.log(err);
console.log(stderr);
});
screenshot
Referencing this, exchanged the double quotes with single quotes and vice-versa, the following code works:
var child_process = require("child_process");
var curl = 'curl "https://ehire.51job.com/Candidate/SearchResumeNew.aspx"';
var child = child_process.exec(curl, (err, stdout, stderr) =>
{
console.log(stdout);
console.log(err);
console.log(stderr);
});

webpack child_process.exec - TypeError: error is not a function

Trying to execute child_process.exe using webpack but getting error that exec is not a function. The script is working in node prompt.
test.js
const chai = require('chai');
const exec = require('child_process').exec;
describe('node version', function nodeVersion() {
it('Should display Node Version', (done) => {
exec('node -v', function(error, stdout, stderr) {
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
if (error !== null) {
console.log('exec error: ', error);
}
done();
});
});
});
Is there anyway we can execute shell commands like child_process.exec on browser?
No, there is no way to run child_process in the browser because JavaScript in the browser is not allowed to create processes. Thankfully there is no way to run a shell script in the browser, otherwise any website would have access to your machine and the bad things you could do with it are endless (e.g. delete everything with rm -rf / or at least what you've permission for).

Execute a cgi script from a Node Express server

So I have a website with a member area. That member area is managed through a payment processor called CCBill. In order for CCBill to manage a password file on my server, they need to execute a cgi script.
Right now, I've looked at cgi and serve-cgi npm modules. But I'm not sure if they can do what I need. Can anyone help me with this?
My Express Router get function:
router.get('*', function(req, res, next) {
console.log('in');
var mPath = path.join(appRoot, '/cgi-bin' + req.params[0]);
console.log(mPath);
const execFile = require('child_process').execFile;
const child = execFile(mPath, function(error, stdout, stderr) {
if (error) {
console.log(error);
throw error;
}
console.log(stdout);
});
});
Scripts (and other executables) can be invoked with the exec() function:
var exec = require('exec');
exec('/path/to/your/script',
function (stderr, stdout, errorCode) {
// You get here when the executable completes
}
}
EDIT
With newer node.js versions exec() is deprecated, so it's better to use child_process.execFile():
const execFile = require('child_process').execFile;
const child = execFile('/path/to/your/script', [parameters], (error, stdout, stderr) => {
// You get here when the executable completes
});

How to access command line arguments in a node.js child process using child_process.exec?

I am trying to write a test script in node.js for another node.js script that is executed via command line with arguments. When the script is executed in the terminal the arguments are accessible using process.argv[2], process.argv[3], etc. However, those arguments are not present when the script is executed in the test script using child_process.exec().
target.js
var arguments = {
arg1: process.argv[2],
arg2: process.argv[3]
};
console.log(arguments.arg1);
// This outputs '100' when target.js is executed from terminal
test.js
var cp = require('child_process');
cp.exec('node target.js 100 200',
function (err, stdout, stderr) {
if (err) {
console.log(err);
}
console.log(stdout);
// process.argv[2] is undefined when executed as a child process
});
Any suggestions on how to get the same behavior when executing via child_process as I do when I execute it from the terminal?
Your problem is elsewhere. (Caveat: node 0.6.12)
I ran a test using this as a.js:
console.log(JSON.stringify(process.argv));
And using your launcher below:
var cp = require('child_process');
cp.exec('node a.js 100 200',
function (err, stdout, stderr) {
if (err) {
console.log(err);
}
console.log(stdout);
});
I get identical expected output:
joe#toad:~/src$ node a.js 100 200
["node","/home/joe/src/a.js","100","200"]
joe#toad:~/src$ node b.js
["node","/home/joe/src/a.js","100","200"]

child_process module not returning directory

I'm doing this tutorial on NodeJS: http://www.nodebeginner.org.
Here is the code I find confusing:
var exec = require("child_process").exec;
function start(response) {
console.log("Request handler 'start' was called.");
var content = "empty";
exec("ls -lah", function(error, stdout, stderr) {
response.writeHead(200, {"Content-type":"text/plain"});
response.write(stdout);
console.log(stdout);
response.end();
});
}
I have a router that passes the http response to a request handler that calls the start function. This is happening without a problem. However, the stdout parameter is not returning anything in the browser or in the console. I understand that ls -lah is supposed to give a list of files in the current directory. I have 5 other files in my directory, but nothing is being returned. Any ideas on what is happening here?

Resources