Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @param {NS} ns */
- export async function main(ns) {
- var servers = [];
- var ramInfo = [];
- const maintarget = ns.args[0];
- var targetinfo = [];
- targetinfo.push(maintarget);
- targetinfo.push(ns.getServerMaxMoney(maintarget));
- targetinfo.push(ns.getServerMinSecurityLevel(maintarget));
- targetinfo.push(ns.getServerRequiredHackingLevel(maintarget));
- const moneythresh = 0.8;
- const securitythresh = 1.3;
- ns.tprint("checkpoint0");
- var usableservers = getallusableservers();
- for (let i = 0; i < usableservers.length / 2; i++) {
- servers.push(usableservers[2 * i]);
- ramInfo.push(usableservers[(2 * i) + 1]);
- }
- ns.tprint("checkpoint1");
- await attackallservers(...servers);
- ns.tprint("checkpoint2");
- if (targetinfo[3] < ns.getHackingLevel()) {
- while (true) {
- const hacktime = ns.getHackTime(maintarget);
- const growtime = ns.getGrowTime(maintarget);
- const weakentime = ns.getWeakenTime(maintarget);
- const currentmoneypercent = (ns.getServerMoneyAvailable(maintarget) / targetinfo[1]);
- const securitylevel = (ns.getServerSecurityLevel(maintarget));
- var counter = 0;
- if (securitylevel > securitythresh) {
- var weakenScriptRam = ns.getScriptRam("weaken.js", "home");
- var neededweakenthreads = Math.ceil((securitylevel - targetinfo[2]) / (ns.weakenAnalyze(1)));
- var threadsPerServer = findram((neededweakenthreads * weakenScriptRam), weakenScriptRam);
- var threadsUsed = 0;
- while (threadsUsed < neededweakenthreads) {
- ns.scp("weaken.js", threadsPerServer[2 * counter], "home");
- await ns.exec("weaken.js", threadsPerServer[2 * counter], threadsPerServer[(2 * counter) + 1], maintarget);
- ns.tprint("running weaken.js on " + threadsPerServer[(2 * counter)]);
- threadsUsed += threadsPerServer[(2 * counter) + 1];
- counter++;
- }
- ns.tprint("waiting for weaken");
- await ns.sleep(weakentime);
- } else if (currentmoneypercent < moneythresh) {
- var growthScriptRam = ns.getScriptRam("grow.js", "home");
- var neededgrowththreads = Math.ceil((ns.growthAnalyze(maintarget, 1.25)) * growthScriptRam);
- var threadsPerServer = findram((neededgrowththreads * growthScriptRam), growthScriptRam);
- var threadsUsed = 0;
- while (threadsUsed < neededgrowththreads) {
- ns.scp("grow.js", threadsPerServer[2 * counter], "home");
- await ns.exec("grow.js", threadsPerServer[2 * counter], threadsPerServer[(2 * counter) + 1], maintarget);
- ns.tprint("running grow.js on " + threadsPerServer[(2 * counter)]);
- threadsUsed += threadsPerServer[(2 * counter) + 1];
- counter++;
- }
- ns.tprint("waiting for growth");
- await ns.sleep(growtime);
- } else {
- var hackScriptRam = ns.getScriptRam("hack.js", "home");
- var neededhackthreads = Math.ceil(((moneythresh) / ns.hackAnalyze(maintarget)));
- var threadsPerServer = findram((neededhackthreads * hackScriptRam), hackScriptRam);
- var threadsUsed = 0;
- while (threadsUsed < neededhackthreads) {
- ns.scp("hack.js", threadsPerServer[2 * counter], "home");
- await ns.exec("hack.js", threadsPerServer[2 * counter], threadsPerServer[(2 * counter) + 1], maintarget);
- ns.tprint("running hack.js on " + threadsPerServer[(2 * counter)]);
- threadsUsed += threadsPerServer[(2 * counter) + 1];
- counter++;
- }
- ns.tprint("waiting for hack");
- await ns.sleep(hacktime);
- }
- ns.tprint("checkpoint3");
- await ns.sleep(5000);
- }
- }
- function findram(ramneeded, scriptram) {
- var totalramfound = 0;
- var counter = 0;
- var threadsUsedPerServer = [];
- while (totalramfound < ramneeded) {
- var currentram = (ramInfo[counter] - ns.getServerUsedRam(servers[counter]));
- var ramUsedOnThisServer = 0;
- while ((totalramfound < ramneeded) || (ramUsedOnThisServer < currentram)) {
- ramUsedOnThisServer += scriptram;
- }
- if (ramUsedOnThisServer > currentram) {
- ramUsedOnThisServer -= scriptram;
- }
- if (ramUsedOnThisServer != 0) {
- threadsUsedPerServer.push(servers[counter]);
- threadsUsedPerServer.push(Math.ceil(ramUsedOnThisServer / scriptram));
- }
- totalramfound += ramUsedOnThisServer;
- counter++;
- }
- return [...threadsUsedPerServer];
- }
- function getallusableservers() {
- var serverlist = ["home"];
- serverlist.push(ns.getServerMaxRam("home"));
- for (let i = 0; i < 20; i++) {
- for (let x = 0; x < serverlist.length / 2; x++) {
- const server = serverlist[2 * x];
- const connections = ns.scan(server);
- for (const connection of connections) {
- if ((!(serverlist.includes(connection))) && (ns.getServerMaxRam(connection) > 2) && (connection != "darkweb") && (connection != "home") && (connection != "pserv-0")) {
- serverlist.push(connection);
- serverlist.push(ns.getServerMaxRam(connection));
- //ns.toast(connection + " added to server list.");
- }
- }
- }
- }
- serverlist.push(serverlist.shift());
- serverlist.push(serverlist.shift());
- return [...serverlist];
- }
- async function attackallservers(...targetlist) {
- for (let i = 0; i < (targetlist.length - 1); i++) {
- if (targetlist[i] != "home" && targetlist[i] != "darkweb")
- await attack(targetlist[i]);
- }
- }
- async function attack(server) {
- var hacktoolnum = 0; //count and use hack tools owned if you don't have root
- if (!ns.hasRootAccess(server)) {
- ns.toast('Opening ports on ' + server);
- if (ns.fileExists('BruteSSH.exe', 'home')) {
- ns.brutessh(server);
- hacktoolnum++;
- }
- if (ns.fileExists('FTPCrack.exe', 'home')) {
- ns.ftpcrack(server);
- hacktoolnum++;
- }
- if (ns.fileExists('relaySMTP.exe', 'home')) {
- ns.relaysmtp(server);
- hacktoolnum++;
- }
- if (ns.fileExists('HTTPWorm.exe', 'home')) {
- ns.httpworm(server);
- hacktoolnum++;
- }
- if (ns.fileExists('SQLInject.exe', 'home')) {
- ns.sqlinject(server);
- hacktoolnum++;
- }
- }
- if (ns.getServerNumPortsRequired(server) <= hacktoolnum && !ns.hasRootAccess(server)) {
- ns.toast("nuking " + server);
- ns.nuke(server);
- }
- if (!ns.hasRootAccess(server)) {
- ns.toast("unable to gain root to " + server, "error");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment