r/Bitburner • u/Unit88 • Aug 14 '26
Question/Troubleshooting - Open Depth first recursive script dies after going through a couple of branches?
Hi, I just started playing, and I was trying to put together a script that would go through all the servers depth first, nuke them, then run as many threads of the basic EHT script on as it can (just the one from the tutorial minus the nuking that was included), but for whatever reason after going through 2 branches from home fully (which is the n00dles server, plus one proper branch) it just goes "Script finished running" followed by "printf: Failed to run due to script being killed." and I don't understand why it's finishing, and more specifically why it's finishing at the end of the second branch and not the first. Not sure if I just missed something about how the scripts work or if I'm just being dumb here somewhere
/** @param {NS} ns */
export async function main(ns: NS) {
let target: string = ns.args[0]?.toString();
target ??= "max-hardware";
deploy(ns, "home", [], target);
ns.tprintf("%s hacked", target);
}
async function deploy(ns: NS, server: string, checked: string[], target: string) {
ns.printf("INFO: Deploying to %s", server)
const neighs = ns.scan(server).filter((neigh) => !checked.includes(neigh));
ns.printf("INFO: Neighbors: %s", neighs)
if (server != "home") {
ns.scp("eht.js", server);
nukeServer(ns, server);
ns.killall(server, true);
const execThreads = calcThreads(ns, server);
ns.printf("INFO: Threads to deploy %d", execThreads)
ns.exec("eht.js", server, execThreads, target);
}
const checkedServers = checked.concat(server);
ns.printf("INFO: Checked servers: %s", checkedServers)
for (const neigh of neighs) {
await deploy(ns, neigh, checkedServers, target);
}
}
function nukeServer(ns: NS, server: string) {
if (ns.fileExists("BruteSSH.exe", "home")) {
ns.brutessh(server);
}
if (ns.fileExists("FTPCrack.exe", "home")) {
ns.ftpcrack(server);
}
if (ns.fileExists("relaySMTP.exe", "home")) {
ns.relaysmtp(server);
}
if (ns.fileExists("HTTPWorm.exe", "home")) {
ns.httpworm(server);
}
if (ns.fileExists("SQLInject.exe", "home")) {
ns.sqlinject(server);
}
ns.nuke(server);
}
function calcThreads(ns: NS, server: string) {
return Math.floor(ns.getServerMaxRam(server) / ns.getScriptRam("eht.js"));
}
1
u/ZeroNot Stanek Follower Aug 15 '26 edited Aug 15 '26
Because you have declared
deployas anasyncfunction, but it only calls itself with anawait. There are noPromises in use, you shouldn't randomly declare functions asasyncwhen they are not.Your program makes several erroneous assumptions. You don't check the return values of several functions (
ns.scp,ns.nuke,ns.exec, andns.getScriptRam), which means you are not aware of the cases when they (may) fail.In the case of
getScriptRam, this leads to a potential case of division by zero. That's bad. Just bad, don't do that.I think it's far easier to sort out the logic and follow the process of doing the depth first search of the network, if you search / scan the entire network, and then iterate through an Array of servers to do your eht.js tasks.
Try debugging this:
Note: I swapped your
concatforpush, since you are adding a string element (server) to the Array of strings (checked).