Home > Software engineering >  nodes run terminal command host IP address
nodes run terminal command host IP address

Time:03-28

I am needing to put a little bit of a layer between my nodeJS script and our customers, as we are seeing a batch of googleusercontent IP addresses ping our server and run our nodeJS for about 5 seconds - Then leave.

What we want to do is run the following command every time a user comes to use our NodeJS script.

for example:

Terminal Command:

 host 34.95.38.154

Will return:

154.38.95.34.in-addr.arpa domain name pointer 154.38.95.34.bc.googleusercontent.com.

In nodeJS I want to see if I get googleusercontent and if so I want to exit the script. The user should not go any further.

I have been reading https://stackabuse.com/executing-shell-commands-with-node-js/

However I don't know how to run the above script using this sample.

CodePudding user response:

From the link you provided to run the commands on the shell, have you tried:

const { exec } = require("child_process");

exec("host 34.95.38.154", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    
    // check if googleusercontent is present
    if(stdout.includes("googleusercontent") {
      // ok continue
    } else {
      // block user
    }

);
  • Related