Guest User

need help

a guest
Jun 20th, 2023
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns */
  2. export async function main(ns) {
  3.   var servers = [];
  4.   var ramInfo = [];
  5.   const maintarget = ns.args[0];
  6.   var targetinfo = [];
  7.   targetinfo.push(maintarget);
  8.   targetinfo.push(ns.getServerMaxMoney(maintarget));
  9.   targetinfo.push(ns.getServerMinSecurityLevel(maintarget));
  10.   targetinfo.push(ns.getServerRequiredHackingLevel(maintarget));
  11.   const moneythresh = 0.8;
  12.   const securitythresh = 1.3;
  13.   ns.tprint("checkpoint0");
  14.   var usableservers = getallusableservers();
  15.   for (let i = 0; i < usableservers.length / 2; i++) {
  16.     servers.push(usableservers[2 * i]);
  17.     ramInfo.push(usableservers[(2 * i) + 1]);
  18.   }
  19.   ns.tprint("checkpoint1");
  20.   await attackallservers(...servers);
  21.   ns.tprint("checkpoint2");
  22.   if (targetinfo[3] < ns.getHackingLevel()) {
  23.     while (true) {
  24.       const hacktime = ns.getHackTime(maintarget);
  25.       const growtime = ns.getGrowTime(maintarget);
  26.       const weakentime = ns.getWeakenTime(maintarget);
  27.       const currentmoneypercent = (ns.getServerMoneyAvailable(maintarget) / targetinfo[1]);
  28.       const securitylevel = (ns.getServerSecurityLevel(maintarget));
  29.       var counter = 0;
  30.       if (securitylevel > securitythresh) {
  31.         var weakenScriptRam = ns.getScriptRam("weaken.js", "home");
  32.         var neededweakenthreads = Math.ceil((securitylevel - targetinfo[2]) / (ns.weakenAnalyze(1)));
  33.         var threadsPerServer = findram((neededweakenthreads * weakenScriptRam), weakenScriptRam);
  34.         var threadsUsed = 0;
  35.         while (threadsUsed < neededweakenthreads) {
  36.           ns.scp("weaken.js", threadsPerServer[2 * counter], "home");
  37.           await ns.exec("weaken.js", threadsPerServer[2 * counter], threadsPerServer[(2 * counter) + 1], maintarget);
  38.           ns.tprint("running weaken.js on " + threadsPerServer[(2 * counter)]);
  39.           threadsUsed += threadsPerServer[(2 * counter) + 1];
  40.           counter++;
  41.         }
  42.         ns.tprint("waiting for weaken");
  43.         await ns.sleep(weakentime);
  44.       } else if (currentmoneypercent < moneythresh) {
  45.         var growthScriptRam = ns.getScriptRam("grow.js", "home");
  46.         var neededgrowththreads = Math.ceil((ns.growthAnalyze(maintarget, 1.25)) * growthScriptRam);
  47.         var threadsPerServer = findram((neededgrowththreads * growthScriptRam), growthScriptRam);
  48.         var threadsUsed = 0;
  49.         while (threadsUsed < neededgrowththreads) {
  50.           ns.scp("grow.js", threadsPerServer[2 * counter], "home");
  51.           await ns.exec("grow.js", threadsPerServer[2 * counter], threadsPerServer[(2 * counter) + 1], maintarget);
  52.           ns.tprint("running grow.js on " + threadsPerServer[(2 * counter)]);
  53.           threadsUsed += threadsPerServer[(2 * counter) + 1];
  54.           counter++;
  55.         }
  56.         ns.tprint("waiting for growth");
  57.         await ns.sleep(growtime);
  58.       } else {
  59.         var hackScriptRam = ns.getScriptRam("hack.js", "home");
  60.         var neededhackthreads = Math.ceil(((moneythresh) / ns.hackAnalyze(maintarget)));
  61.         var threadsPerServer = findram((neededhackthreads * hackScriptRam), hackScriptRam);
  62.         var threadsUsed = 0;
  63.         while (threadsUsed < neededhackthreads) {
  64.           ns.scp("hack.js", threadsPerServer[2 * counter], "home");
  65.           await ns.exec("hack.js", threadsPerServer[2 * counter], threadsPerServer[(2 * counter) + 1], maintarget);
  66.           ns.tprint("running hack.js on " + threadsPerServer[(2 * counter)]);
  67.           threadsUsed += threadsPerServer[(2 * counter) + 1];
  68.           counter++;
  69.         }
  70.         ns.tprint("waiting for hack");
  71.         await ns.sleep(hacktime);
  72.       }
  73.       ns.tprint("checkpoint3");
  74.       await ns.sleep(5000);
  75.     }
  76.   }
  77.  
  78.   function findram(ramneeded, scriptram) {
  79.     var totalramfound = 0;
  80.     var counter = 0;
  81.     var threadsUsedPerServer = [];
  82.     while (totalramfound < ramneeded) {
  83.       var currentram = (ramInfo[counter] - ns.getServerUsedRam(servers[counter]));
  84.       var ramUsedOnThisServer = 0;
  85.       while ((totalramfound < ramneeded) || (ramUsedOnThisServer < currentram)) {
  86.         ramUsedOnThisServer += scriptram;
  87.       }
  88.       if (ramUsedOnThisServer > currentram) {
  89.         ramUsedOnThisServer -= scriptram;
  90.       }
  91.       if (ramUsedOnThisServer != 0) {
  92.         threadsUsedPerServer.push(servers[counter]);
  93.         threadsUsedPerServer.push(Math.ceil(ramUsedOnThisServer / scriptram));
  94.       }
  95.       totalramfound += ramUsedOnThisServer;
  96.       counter++;
  97.     }
  98.     return [...threadsUsedPerServer];
  99.   }
  100.   function getallusableservers() {
  101.     var serverlist = ["home"];
  102.     serverlist.push(ns.getServerMaxRam("home"));
  103.     for (let i = 0; i < 20; i++) {
  104.       for (let x = 0; x < serverlist.length / 2; x++) {
  105.         const server = serverlist[2 * x];
  106.         const connections = ns.scan(server);
  107.         for (const connection of connections) {
  108.           if ((!(serverlist.includes(connection))) && (ns.getServerMaxRam(connection) > 2) && (connection != "darkweb") && (connection != "home") && (connection != "pserv-0")) {
  109.             serverlist.push(connection);
  110.             serverlist.push(ns.getServerMaxRam(connection));
  111.             //ns.toast(connection + " added to server list.");
  112.           }
  113.         }
  114.       }
  115.     }
  116.     serverlist.push(serverlist.shift());
  117.     serverlist.push(serverlist.shift());
  118.     return [...serverlist];
  119.   }
  120.   async function attackallservers(...targetlist) {
  121.     for (let i = 0; i < (targetlist.length - 1); i++) {
  122.       if (targetlist[i] != "home" && targetlist[i] != "darkweb")
  123.         await attack(targetlist[i]);
  124.     }
  125.   }
  126.   async function attack(server) {
  127.     var hacktoolnum = 0;                                                                                   //count and use hack tools owned if you don't have root
  128.     if (!ns.hasRootAccess(server)) {
  129.       ns.toast('Opening ports on ' + server);
  130.       if (ns.fileExists('BruteSSH.exe', 'home')) {
  131.         ns.brutessh(server);
  132.         hacktoolnum++;
  133.       }
  134.       if (ns.fileExists('FTPCrack.exe', 'home')) {
  135.         ns.ftpcrack(server);
  136.         hacktoolnum++;
  137.  
  138.       }
  139.       if (ns.fileExists('relaySMTP.exe', 'home')) {
  140.         ns.relaysmtp(server);
  141.         hacktoolnum++;
  142.  
  143.       }
  144.       if (ns.fileExists('HTTPWorm.exe', 'home')) {
  145.         ns.httpworm(server);
  146.         hacktoolnum++;
  147.  
  148.       }
  149.       if (ns.fileExists('SQLInject.exe', 'home')) {
  150.         ns.sqlinject(server);
  151.         hacktoolnum++;
  152.  
  153.       }
  154.     }
  155.     if (ns.getServerNumPortsRequired(server) <= hacktoolnum && !ns.hasRootAccess(server)) {
  156.       ns.toast("nuking " + server);
  157.       ns.nuke(server);
  158.     }
  159.     if (!ns.hasRootAccess(server)) {
  160.       ns.toast("unable to gain root to " + server, "error");
  161.     }
  162.   }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment