Updated crawler to support more flags
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import {ArgError, ArgParser} from '/scripts/lib/arg-parser';
|
||||
import {scanNetwork} from '/scripts/lib/utils';
|
||||
import {copyWithDependencies, scanNetwork} from '/scripts/lib/utils';
|
||||
|
||||
/**
|
||||
* BitBurner autocomplete
|
||||
@ -20,20 +20,24 @@ export async function main(ns) {
|
||||
const argParser = new ArgParser('crawler.js', 'Search the network for targets to execute a script against.', null, [
|
||||
{name: 'script', desc: 'Script to copy & execute', type: 'string'},
|
||||
{name: 'args', desc: 'Arguments for script. Forward the current target with: {{TARGET}}', optional: true, extras: true, type: 'string'},
|
||||
{name: 'cpu', desc: 'Number of CPU threads to use with script', flags: ['-c', '--cpu'], default: 1, type: 'num'},
|
||||
{name: 'cpu', desc: 'Number of CPU threads to use with script', flags: ['-c', '--cpu'], type: 'num'},
|
||||
{name: 'depth', desc: 'Depth to scan to, defaults to 3', flags: ['-d', '--depth'], default: Infinity, type: 'num'},
|
||||
{name: 'level', desc: 'Exclude targets with higher hack level, defaults to current hack level', flags: ['-l', '--level'], default: ns.getHackingLevel(), type: 'num'},
|
||||
{name: 'kill', desc: 'Kill all scripts running on device', flags: ['-k', '--kill'], type: 'bool'},
|
||||
{name: 'level', desc: 'Exclude targets with higher hack level, defaults to current hack level', flags: ['--level'], default: ns.getHackingLevel(), type: 'num'},
|
||||
{name: 'remoteExec', desc: 'Copy script to remote device & run there', flags: ['-e', '--remote-exec'], type: 'bool'},
|
||||
{name: 'rooted', desc: 'Filter to devices that have been rooted', flags: ['-r', '--rooted'], type: 'bool'},
|
||||
{name: 'notRooted', desc: 'Filter to devices that have not been rooted', flags: ['-n', '--not-rooted'], type: 'bool'},
|
||||
{name: 'ports', desc: 'Exclude targets with too many closed ports', flags: ['-p', '--ports'], default: Infinity, type: 'num'},
|
||||
{name: 'silent', desc: 'Suppress program output', flags: ['-s', '--silent'], type: 'bool'}
|
||||
{name: 'silent', desc: 'Suppress program output', flags: ['-s', '--silent'], type: 'bool'},
|
||||
{name: 'verbose', desc: 'Display the device names in the final report', flags: ['-v', '--verbose'], type: 'bool'},
|
||||
], true);
|
||||
|
||||
try {
|
||||
// Run
|
||||
const localhost = ns.getHostname();
|
||||
const args = argParser.parse(ns.args);
|
||||
const [devices, network] = scanNetwork(ns);
|
||||
let complete = 0, failed = 0, skipped = 0;
|
||||
let complete = [], failed = [], skipped = [];
|
||||
for(let device of devices) {
|
||||
// Check root status if needed
|
||||
const rooted = ns.hasRootAccess(device);
|
||||
@ -42,31 +46,47 @@ export async function main(ns) {
|
||||
|
||||
// Skip invalid devices
|
||||
if(device == 'home' || args['level'] < ns.getServerRequiredHackingLevel(device) || args['ports'] < ns.getServerNumPortsRequired(device)) {
|
||||
skipped++;
|
||||
skipped.push(device);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start script
|
||||
if(args['kill']) ns.killall(device);
|
||||
const scriptArgs = args['args'].map(arg => arg.toUpperCase() == '{{TARGET}}' ? device : arg);
|
||||
const pid = ns.run(args['script'], args['cpu'], ...scriptArgs);
|
||||
const [totalRam, usedRam] = ns.getServerRam(args['remoteExec'] ? device : localhost);
|
||||
const threads = args['cpu'] || ~~((totalRam - usedRam) / ns.getScriptRam(args['script'], localhost)) || 1;
|
||||
if(args['remoteExec']) await copyWithDependencies(ns, args['script'], device);
|
||||
const pid = ns.exec(args['script'], args['remoteExec'] ? device : localhost, threads, ...scriptArgs);
|
||||
if(pid == 0) {
|
||||
failed++;
|
||||
failed.push(device);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Wait for script to finish
|
||||
while(ns.scriptRunning(args['script'], 'home'))
|
||||
await ns.sleep(1000);
|
||||
complete++;
|
||||
// Wait for script to finish if local
|
||||
if(!args['remoteExec'])
|
||||
while(ns.scriptRunning(args['script'], localhost)) await ns.sleep(1000);
|
||||
complete.push(device);
|
||||
}
|
||||
|
||||
// Output report
|
||||
if(!args['silent']) {
|
||||
ns.tprint('===================================================');
|
||||
ns.tprint(`Crawler Report: ${complete + failed + skipped} Devices`);
|
||||
ns.tprint(`Crawler Report: ${complete.length + failed.length + skipped.length} Devices`);
|
||||
ns.tprint('===================================================');
|
||||
ns.tprint(`Complete: ${complete}\tFailed: ${failed}\tSkipped: ${skipped}`);
|
||||
ns.tprint('');
|
||||
if(args['verbose']) {
|
||||
ns.tprint(`Complete (${complete.length}):`);
|
||||
if(complete.length) ns.tprint(complete.join(', '));
|
||||
ns.tprint('');
|
||||
ns.tprint(`Failed (${failed.length}):`);
|
||||
if(failed.length) ns.tprint(failed.join(', '));
|
||||
ns.tprint('');
|
||||
ns.tprint(`Skipped (${skipped.length}):`);
|
||||
if(skipped.length) ns.tprint(skipped.join(', '));
|
||||
ns.tprint('');
|
||||
} else {
|
||||
ns.tprint(`Complete: ${complete.length}\tFailed: ${failed.length}\tSkipped: ${skipped.length}`);
|
||||
ns.tprint('');
|
||||
}
|
||||
}
|
||||
} catch(err) {
|
||||
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
|
||||
|
@ -36,7 +36,7 @@ export async function main(ns) {
|
||||
ns.disableLog('ALL');
|
||||
const argParser = new ArgParser('network-graph.js', 'Scan the network for devices and display as an ASCII tree:\n home\n ├─ n00dles (ROOTED)\n | └─ max-hardware (80|1)\n | └─ neo-net (50|1)\n ├─ foodnstuff (ROOTED)\n └─ sigma-cosmetics (ROOTED)', null, [
|
||||
{name: 'device', desc: 'Point to start scan from, defaults to current machine', optional: true, default: ns.getHostname(), type: 'string'},
|
||||
{name: 'depth', desc: 'Depth to scan to, defaults is 3', flags: ['-d', '--depth'], default: Infinity, type: 'num'},
|
||||
{name: 'depth', desc: 'Depth to scan to', flags: ['-d', '--depth'], default: Infinity, type: 'num'},
|
||||
{name: 'filter', desc: 'Filter to device matching name', flags: ['-f', '--filter'], type: 'string'},
|
||||
{name: 'regex', desc: 'Filter to devices matching pattern', flags: ['-e', '--regex'], type: 'string'},
|
||||
{name: 'rooted', desc: 'Filter to devices that have been rooted', flags: ['-r', '--rooted'], type: 'bool'},
|
||||
|
Reference in New Issue
Block a user