问题描述
我正在运行一个简单的 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();
但是,我收到了这样的错误:
节点蛤:标准输出: node-clam: 已感染! (node:109042) UnhandledPromiseRejectionWarning: Error: Error: spawn /usr/bin/clamscan --no-summary --stdout.
位置 /usr/bin/clamscan 中有一个 clamscan 文件。但这还是来了
解决方法
我通过使用 Promise 函数而不是异步函数成功地使 scan_dir 工作......我想这是库中的一个问题,但如果你使用它,它应该可以工作:)
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();
问候