This commit is contained in:
Vomitblood 2026-04-29 21:55:33 +08:00
parent 24a63f785b
commit 24b277e12e
7 changed files with 144 additions and 8 deletions

View file

@ -1,9 +1,7 @@
import { NS } from '@ns';
import { nuke } from './utils/nuke';
import { scan } from './utils/scan';
import { scriptPropagator } from './utils/script-propagator';
import { startall } from './utils/startall';
export const main = (ns: NS) => {
ns.tprint(scan(ns, { rootAccess: true }));
scriptPropagator(ns);
ns.tprint(ns.purchaseServer('chungus', 128));
};

33
src/utils/killall.ts Normal file
View file

@ -0,0 +1,33 @@
import { NS } from '@ns';
import { scan } from './scan';
export const killall = (ns: NS): void => {
const allHosts = scan(ns);
let hostsWithKills = 0;
let killedProcesses = 0;
allHosts.forEach((host) => {
// do not kill any scripts on home
if (host === 'home') return;
// check if there are any running scripts on this host
const runningProcesses = ns.ps(host).length;
if (runningProcesses === 0) {
return;
} else {
// ns.killall returns true if it successfully stopped scripts
if (ns.killall(host)) {
hostsWithKills++;
killedProcesses += runningProcesses;
ns.tprint(`Killed ${runningProcesses} processes on ${host}`);
}
}
});
ns.tprint(`Processes killed: ${killedProcesses}`);
ns.tprint(`Hosts affected: ${hostsWithKills}`);
};
export const main = (ns: NS) => {
killall(ns);
};

View file

@ -33,16 +33,19 @@ export const nuke = (ns: NS) => {
skippedCount++;
}
} catch (error) {
ns.tprint(`Error nuking ${host}: ${error}`);
// ns.tprint(`Error nuking ${host}: ${error}`);
skippedCount++;
}
});
// print a summary of the results
ns.tprint(`Already rooted: ${alreadyNukedCount}`);
ns.tprint(`Newly rooted: ${newlyNukedCount}`);
ns.tprint(`Already nuked: ${alreadyNukedCount}`);
ns.tprint(`Newly nuked: ${newlyNukedCount}`);
ns.tprint(`Skipped: ${skippedCount}`);
// scan again to get the list of all hosts we have root access to
return newlyNukedHosts;
};
export const main = (ns: NS) => {
nuke(ns);
};

View file

@ -36,3 +36,7 @@ export const scan = (ns: NS, options: ScanOptions = {}): string[] => {
// convert the set to an array and return it
return Array.from(filteredHosts);
};
export const main = (ns: NS) => {
ns.tprint(scan(ns));
};

View file

@ -0,0 +1,34 @@
import { NS } from '@ns';
import { scan } from './scan';
export const scriptCleanup = (ns: NS) => {
// get all hosts with root access
const rootedHosts: string[] = scan(ns, { rootAccess: true });
let totalScriptsDeleted = 0;
let hostsCleaned = 0;
// iterate through all the rooted hosts and find .js files
rootedHosts.forEach((host) => {
let scriptsDeleted = 0;
const remoteScripts: string[] = ns.ls(host, '.js');
remoteScripts.forEach((script) => {
ns.rm(script, host);
scriptsDeleted++;
totalScriptsDeleted++;
});
ns.tprint(`${host}: ${scriptsDeleted} scripts deleted`);
hostsCleaned++;
});
ns.tprint(`Total hosts cleaned: ${hostsCleaned}`);
ns.tprint(`Total scripts deleted: ${totalScriptsDeleted}`);
// return the list of rooted hosts for potential further use
return rootedHosts;
};
export const main = (ns: NS) => {
scriptCleanup(ns);
};

View file

@ -26,3 +26,7 @@ export const scriptPropagator = (ns: NS) => {
// return the list of rooted hosts for potential further use
return rootedHosts;
};
export const main = (ns: NS) => {
scriptPropagator(ns);
};

60
src/utils/startall.ts Normal file
View file

@ -0,0 +1,60 @@
import { NS } from '@ns';
import { scan } from './scan';
interface StartOptions {
threads?: number; // explicit thread count
ramPercentage?: number; // prcentage of max ram (0.0 to 1.0)
args?: string[]; // optional arguments to pass to the script
}
export const startall = (ns: NS, scriptName: string, options: StartOptions = {}) => {
const { threads: threadCount, ramPercentage, args = [] } = options;
const rootedHosts = scan(ns, { rootAccess: true });
let totalThreadsStarted = 0;
let hostsStartedOn = 0;
// get the ram required to run one thread of the script
// assume that the script is already on all the hosts that we want to run it in
// should be ran after propagation
const ramPerThread = ns.getScriptRam(scriptName, 'home');
// iterate through all the rooted hosts and start the script
rootedHosts.forEach((host) => {
if (host === 'home') return;
// calculate how many threads we can run based on the options provided
const availableRam = ns.getServerMaxRam(host) - ns.getServerUsedRam(host);
let threadsToUse: number;
if (threadCount) {
// priority 1 explicit thread count
threadsToUse = threadCount;
} else if (ramPercentage !== undefined) {
// priority 2 percentage of total ram
const targetRam = ns.getServerMaxRam(host) * ramPercentage;
threadsToUse = Math.floor(targetRam / ramPerThread);
} else {
// default use all available ram
threadsToUse = Math.floor(availableRam / ramPerThread);
}
if (threadsToUse > 0) {
ns.exec(scriptName, host, threadsToUse, ...args);
ns.tprint(`Started ${scriptName} on ${host} with ${threadsToUse} threads`);
totalThreadsStarted += threadsToUse;
hostsStartedOn++;
} else {
ns.tprint(`Not enough RAM to run ${scriptName} on ${host}`);
}
});
ns.tprint(`Total threads started: ${totalThreadsStarted}`);
ns.tprint(`Hosts affected: ${hostsStartedOn}`);
};
export const main = (ns: NS) => {
// get the arguments from the command line
const args = ns.args as string[];
startall(ns, 'super.js', { args: args });
};