简体   繁体   中英

How can I exec the ping command in nodejs on lambda?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM