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);
});
Related
In my Node.js program, I would like to know if a specific port is used so I execute the command below with child_process.exec (example with port 3001) :
netstat -na | find "3001"
If the port is used I get informations about the port as expected but If it is not used I get the following error:
I don't understand why I get this error because when I run the same command in cmd, If the port is not used it doesn't throw error:
Can anyone please tell me what's wrong ?
Node version: v10.16.0
I think you should try this. I have created API you can directly call.
This will return the result data.
const { exec } = require("child_process");
function os_func() {
this.execCommand = function(cmd, callback) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
callback(stdout);
});
}
}
app.get("/", (req, res) => {
console.log("inside get");
var os = new os_func();
os.execCommand('netstat -na | find "3001"', function (returnvalue) {
res.end(returnvalue)
});
});
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.
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).
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
});
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"]