Error spawn /usr/bin/clamscan in NodeJS ClamScan

1.4k Views Asked by At

I am running a simple example of clamscan.

const NodeClam = require('clamscan');

let options = {
    debug_mode: true,
    // clamdscan: {
    //     socket: '/var/run/clamav/clamd.ctl', // This is pretty typical
    //     host: '127.0.0.1', // If you want to connect locally but not through socket
    //     port: 3310, // Because, why not
    //     timeout: 300000, // 5 minutes
    //     local_fallback: true, // Use local preferred binary to scan if socket/tcp fails
    //     path: '/usr/bin/clamdscan', // Special path to the clamdscan binary on your server
    //     config_file: '/etc/clamav/clamd.conf', // A fairly typical config location
    //     multiscan: false, // You hate speed and multi-threaded awesome-sauce
    //     reload_db: true, // You want your scans to run slow like with clamscan
    //     active: false, // you don't want to use this at all because it's evil
    //     bypass_test: true, // Don't check to see if socket is available. You should probably never set this to true.
    // },
    preference: 'clamscan' 
};


async function some_function() {
    try {
        // Get instance by resolving ClamScan promise object
        console.log("-------------------hello world------------------------------");
        const clamscan = await new NodeClam().init(options);
        //console.log(clamscan);
        console.log("--------------------------------------------------------hello world------------------------------------------------");
        const {good_files, bad_files} = await clamscan.scan_dir('/home/abhishek/Desktop/JSINFO', );
        console.log("----------------------------------------------------------hello world--------------------------------------------------------------------");
        console.log(good_files);
    } catch (err) {
       console.log(err);
    }
};
 
some_function();

But, I am getting an error like this:

node-clam: stdout: node-clam: <Unknown File Path!> is INFECTED! (node:109042) UnhandledPromiseRejectionWarning: Error: Error: spawn /usr/bin/clamscan --no-summary --stdout.

There is a clamscan file in the location /usr/bin/clamscan. But still this is coming

1

There are 1 best solutions below

0
On

I succeed to get scan_dir working by using Promise function, and not async function... I guess it's an issue in the library, but if you use this, it should work :)

const NodeClam = require('clamscan');
const ClamScan = new NodeClam().init();


function main() {
  // Get instance by resolving ClamScan promise object
  ClamScan.then(async clamscan => {
      try {
          // You can re-use the `clamscan` object as many times as you want
          const path = '/path/to/your/folder';
          const version = await clamscan.get_version();
          console.log(`ClamAV Version: ${version}`);
  
          clamscan.scan_dir(path, (err, good_files, bad_files, viruses) => {
            if (err) return console.error(err);

            console.log(good_files);
        
            if (bad_files.length > 0) {
                console.log(`${path} was infected. The offending files (${bad_files.join (', ')}) have been quarantined.`);
                console.log(`Viruses Found: ${viruses.join(', ')}`);
            } else {
                console.log("Everything looks good! No problems here!.");
            }
          });
      } catch (err) {
          // Handle any errors raised by the code in the try block
      }
  }).catch(err => {
      // Handle errors that may have occurred during initialization
  });
}

main();

Regards