Guest User

bitburner scripts help

a guest
Jul 28th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---------
  2. cron.js
  3. ---------
  4. /** @param {NS} ns */
  5. export async function main(ns) {
  6.   while (true) {
  7.     // Runs a recursive scan of the whole network
  8.     ns.run("h4xc4n.js", { threads: 1, preventDuplicates: true });
  9.     await ns.sleep(5000);
  10.  
  11.     // Buys and upgrades servers
  12.     ns.run("upgradeServers.js", { threads: 1, preventDuplicates: true });
  13.     await ns.sleep(1000);
  14.  
  15.     // Kills ramsharing on keks
  16.     ns.run("kekmultikill.js", 1, "1", "24", "share.js", "silent");
  17.  
  18.     // Runs a script on HOME that spawns scripts on target hacking itself
  19.     ns.run("invexec.js", { threads: 1, preventDuplicates: true }, "--silent", "--script", "/hackself.js", "--ram", "--args", "++host", "<host>");
  20.  
  21.     // Runs a singular hackex on keks targeting the "rich" inventory - 1 kek per host
  22.     //ns.run("invexec.js", { threads: 1, preventDuplicates: true }, "--silent", "--script", "/kekhack.js", "--reverse", "--richOnly", "--args", "<i>", "<i>", "<host>", "silent");
  23.  
  24.     // Runs many hackexes on keks targeting the "rich" inventory - 1 kek per host
  25.     ns.run("invexec.js", { threads: 1, preventDuplicates: true }, "--silent", "--script", "/multikekhack.js", "--reverse", "--richOnly", "--args", "<i>", "<i>", "<host>", "silent");
  26.  
  27.     // Runs ramsharing on proxycontrol
  28.     ns.run("proxyshare.js", { threads: 1, preventDuplicates: true }, "proxycontrol");
  29.  
  30.     // Runs omegagrow from idle keks on the rich inventory
  31.     await ns.sleep(1000);
  32.     ns.run("invexec.js", { threads: 1, preventDuplicates: true }, "--silent", "--script", "/omegagrow.js", "--await", "--richOnly", "--args", "1", "24", "<host>");
  33.  
  34.     // Runs ramsharing on keks
  35.     ns.run("kekshare.js", 1, "3", "24", "silent");
  36.  
  37.     // Runs hackex on home targeting everything but "useless", with allocated ram (allocating doesnt really work lol)
  38.     ns.run("invexec.js", { threads: 1, preventDuplicates: true }, "--silent", "--script", "/hackex.js", "--args", "++host", "<host>", "++allocateRam", "8192", "++leaveRam", "10240");
  39.  
  40.     // Runs many hackexes on home targeting "richest" inventory
  41.     ns.run("invexec.js", { threads: 1, preventDuplicates: true }, "--silent", "--richest", "--script", "/spawnhackex.js", "--args", "/hackex.js", 0.4, 2000, "<host>", "silent");
  42.  
  43.  
  44.     //ns.tprint("##################");
  45.     ns.tprint(ns.tFormat(Date.now() - ns.getResetInfo().lastAugReset).split(" ").splice(0,4).join(" ")," --- #!# cron was ran #!#");
  46.     //ns.tprint("##################");
  47.  
  48.     // Wait for 10 min
  49.     await ns.sleep(1000 * 60 * 10);
  50.  
  51.   }
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. ------------
  60. invexec.js
  61. ------------
  62. /** @param {NS} ns */
  63. export async function main(ns) {
  64.   var active = []
  65.   var inventory = [
  66.     "n00dles",
  67.     "foodnstuff",
  68.     "sigma-cosmetics",
  69.     "joesguns",
  70.     "hong-fang-tea",
  71.     "harakiri-sushi",
  72.     "iron-gym",
  73.     "max-hardware",
  74.     "silver-helix",
  75.     "neo-net",
  76.     "zer0",
  77.     "omega-net",
  78.     "phantasy",
  79.     "nectar-net",
  80.     "crush-fitness",  // 0 ram
  81.     "the-hub",
  82.     "johnson-ortho",  // 0 ram
  83.     "netlink",
  84.     "computek",  // 0 ram
  85.     "catalyst",
  86.     "summit-uni",
  87.     "rothman-uni",
  88.     "aevum-police",
  89.     "millenium-fitness",
  90.     "alpha-ent",
  91.     "rho-construction",
  92.     "syscore",  // 0 ram
  93.     "snap-fitness",  // 0 ram
  94.     "lexo-corp",
  95.     "zb-institute",
  96.     "global-pharm",
  97.     "unitalife",
  98.     "zb-def", // 0 ram
  99.     "applied-energetics", // 0 ram
  100.     "zeus-med", // 0 ram
  101.     "titan-labs",
  102.     "microdyne",
  103.     "nova-med", // 0 ram
  104.     "vitalife",
  105.     "solaris",
  106.     "deltaone", // 0 ram
  107.     "omnia",
  108.     "icarus", // 0 ram
  109.     "univ-energy",
  110.     "helios",
  111.     "galactic-cyber", // 0 ram
  112.     "aerocorp", // 0 ram
  113.     "taiyang-digital", // 0 ram
  114.     "infocomm", // 0 ram
  115.     "kuai-gong", // 0 ram
  116.     "b-and-a", // 0 ram
  117.     "fulcrumtech",
  118.     "omnitek",
  119.     "stormtech", // 0 ram
  120.     "nwo", // 0 ram
  121.     "ecorp", // 0 ram
  122.     "clarkinc", // 0 ram
  123.     "blade",
  124.     "powerhouse-fitness",
  125.     "4sigma", // 0 ram
  126.     "defcomm", // 0 ram
  127.     "megacorp", // 0 ram
  128.   ];
  129.   active = [];
  130.   for (const invEntry of inventory) {
  131.     if (ns.fileExists("/inventory/root." + invEntry + ".txt")) {
  132.       active.push(invEntry);
  133.     }
  134.   }
  135.   inventory = active;
  136.  
  137.   var inventoryRam = [
  138.     "n00dles",
  139.     "foodnstuff",
  140.     "sigma-cosmetics",
  141.     "joesguns",
  142.     "hong-fang-tea",
  143.     "harakiri-sushi",
  144.     "iron-gym",
  145.     "max-hardware",
  146.     "silver-helix",
  147.     "neo-net",
  148.     "zer0",
  149.     "omega-net",
  150.     "phantasy",
  151.     "nectar-net",
  152.     "the-hub",
  153.     "netlink",
  154.     "catalyst",
  155.     "aevum-police",
  156.     "rothman-uni",
  157.     "summit-uni",
  158.     "millenium-fitness",
  159.     "alpha-ent",
  160.     "rho-construction",
  161.     "lexo-corp",
  162.     "zb-institute",
  163.     "global-pharm",
  164.     "unitalife",
  165.     "titan-labs",
  166.     "microdyne",
  167.     "vitalife",
  168.     "solaris",
  169.     "omnia",
  170.     "univ-energy",
  171.     "helios",
  172.     "fulcrumtech",
  173.     "omnitek",
  174.     "blade",
  175.     "powerhouse-fitness",
  176.   ];
  177.   active = [];
  178.   for (const invEntry of inventoryRam) {
  179.     if (ns.fileExists("/inventory/root." + invEntry + ".txt")) {
  180.       active.push(invEntry);
  181.     }
  182.   }
  183.   inventoryRam = active;
  184.  
  185.   var inventoryNoRam = [
  186.     "crush-fitness",  // 0 ram
  187.     "johnson-ortho",  // 0 ram
  188.     "computek",  // 0 ram
  189.     "syscore",  // 0 ram
  190.     "snap-fitness",  // 0 ram
  191.     "zb-def", // 0 ram
  192.     "applied-energetics", // 0 ram
  193.     "zeus-med", // 0 ram
  194.     "nova-med", // 0 ram
  195.     "deltaone", // 0 ram
  196.     "icarus", // 0 ram
  197.     "galactic-cyber", // 0 ram
  198.     "aerocorp", // 0 ram
  199.     "taiyang-digital", // 0 ram
  200.     "infocomm", // 0 ram
  201.     "kuai-gong", // 0 ram
  202.     "b-and-a", // 0 ram
  203.     "stormtech", // 0 ram
  204.     "nwo", // 0 ram
  205.     "ecorp", // 0 ram
  206.     "clarkinc", // 0 ram
  207.     "4sigma", // 0 ram
  208.     "defcomm", // 0 ram
  209.     "megacorp", // 0 ram
  210.   ];
  211.   active = [];
  212.   for (const invEntry of inventoryNoRam) {
  213.     if (ns.fileExists("/inventory/root." + invEntry + ".txt")) {
  214.       active.push(invEntry);
  215.     }
  216.   }
  217.   inventoryNoRam = active;
  218.  
  219.   var inventoryRich = [
  220.     //"the-hub",
  221.     //"netlink",
  222.     //"computek", // 0 ram
  223.     //"catalyst",
  224.     //"summit-uni",
  225.     //"rothman-uni",
  226.     //"millenium-fitness",
  227.     //"alpha-ent",
  228.     //"rho-construction",
  229.     //"syscore",  // 0 ram
  230.     //"snap-fitness",  // 0 ram
  231.     //"lexo-corp",
  232.     //"titan-labs",
  233.     //"microdyne",
  234.     //"vitalife",
  235.     //"omnia",
  236.     //"helios",
  237.     //"aerocorp", // 0 ram
  238.     //"infocomm", // 0 ram
  239.     //"stormtech", // 0 ram
  240.     //"powerhouse-fitness",
  241.     //"solaris",
  242.     //"deltaone", // 0 ram
  243.     "zb-institute",
  244.     "global-pharm",
  245.     "unitalife",
  246.     "zb-def", // 0 ram
  247.     "applied-energetics", // 0 ram
  248.     "zeus-med", // 0 ram
  249.     "nova-med", // 0 ram
  250.     "icarus", // 0 ram
  251.     "univ-energy",
  252.     "galactic-cyber", // 0 ram
  253.     "taiyang-digital", // 0 ram
  254.     "kuai-gong", // 0 ram
  255.     "b-and-a", // 0 ram
  256.     "fulcrumtech",
  257.     "omnitek",
  258.     "nwo", // 0 ram
  259.     "ecorp", // 0 ram
  260.     "clarkinc", // 0 ram
  261.     "blade",
  262.     "4sigma", // 0 ram
  263.     "megacorp",
  264.   ];
  265.   active = [];
  266.   for (const invEntry of inventoryRich) {
  267.     if (ns.fileExists("/inventory/root." + invEntry + ".txt")) {
  268.       active.push(invEntry);
  269.     }
  270.   }
  271.   inventoryRich = active;
  272.   if (inventoryRich.length > 22) {
  273.     ns.tprint("WARNING, too many servers in 'rich' inventory to keep omegagrow running");
  274.   }
  275.   if (inventoryRich.length > 24) {
  276.     ns.tprint("ERROR, more servers in 'rich' inventory than keks");
  277.   }
  278.  
  279.   var inventoryRichest = [
  280.     "kuai-gong",
  281.     "b-and-a",
  282.     "omnitek",
  283.     //"nwo", // 0 ram
  284.     "ecorp", // 0 ram
  285.     "clarkinc", // 0 ram
  286.     //"blade",
  287.     "4sigma", // 0 ram
  288.     "megacorp",
  289.   ];
  290.   active = [];
  291.   for (const invEntry of inventoryRichest) {
  292.     if (ns.fileExists("/inventory/root." + invEntry + ".txt")) {
  293.       active.push(invEntry);
  294.     }
  295.   }
  296.   inventoryRichest = active;
  297.  
  298.   var inventoryUseless = [
  299.     "avmnite-02h",
  300.     "I.I.I.I",
  301.     "CSEC",
  302.     "run4theh111z",
  303.     ".",
  304.     "The-Cave", // 0 ram
  305.     "fulcrumassets",
  306.   ];
  307.   active = [];
  308.   for (const invEntry of inventoryUseless) {
  309.     if (ns.fileExists("/inventory/root." + invEntry + ".txt")) {
  310.       active.push(invEntry);
  311.     }
  312.   }
  313.   inventoryUseless = active;
  314.  
  315.   const dat = ns.flags([
  316.     ["ram", false],
  317.     ["noRam", false],
  318.     ["richOnly", false],
  319.     ["richest", false],
  320.     ["useless", false],
  321.     ["silent", false],
  322.     ["script", ""],
  323.     ["threads", 1],
  324.     ["forceDupes", false],
  325.     ["check", false],
  326.     ["await", false],
  327.     ["reverse", false],
  328.     ["24invOnly", false],
  329.     ["kekLimiter", 24],
  330.     ["args", false],
  331.   ]);
  332.  
  333.  
  334.   if (dat['ram'] && !dat['noRam']) {
  335.     inventory = inventoryRam;
  336.   }
  337.   if (!dat['ram'] && dat['noRam']) {
  338.     inventory = inventoryNoRam;
  339.   }
  340.   if (dat['richOnly']) {
  341.     inventory = inventory.filter(value => inventoryRich.includes(value));
  342.   }
  343.   if (dat['richest']) {
  344.     inventory = inventoryRichest;
  345.   }
  346.   if (dat['useless']) {
  347.     inventory = inventory.concat(inventoryUseless);
  348.   }
  349.  
  350.   if (dat['args']) {
  351.     var arg = ns.args.slice(ns.args.indexOf("--args") + 1);
  352.  
  353.     for (const i in inventory) {
  354.       if (i > 24 && dat['24invOnly']) { break; }
  355.       arg = ns.args.slice(ns.args.indexOf("--args") + 1);
  356.       for (const x in arg) {
  357.         if (typeof arg[x] == "string") {
  358.           arg[x] = arg[x].replaceAll('+', '-');
  359.           arg[x] = arg[x].replaceAll('<host>', inventory[i]);
  360.           if (dat['reverse']) {
  361.             arg[x] = arg[x].replaceAll('<i>', dat['kekLimiter'] - i);
  362.           } else {
  363.             const limitedI = parseInt(i) + 25 - dat['kekLimiter'];
  364.             arg[x] = arg[x].replaceAll('<i>', limitedI.toString());
  365.           }
  366.         }
  367.       }
  368.       if (!dat['silent']) {
  369.         ns.tprint(inventory[i], " - ", dat['threads'], " thread(s) - ", dat['script'], " ", arg);
  370.       }
  371.       if (!dat['check']) {
  372.         ns.run(dat['script'], { threads: dat['threads'], preventDuplicates: !dat['forceDupes'] }, ...arg);
  373.         if (dat['await']) {
  374.           while (ns.scriptRunning(dat['script'], ns.getHostname())) {
  375.             await ns.sleep(10000);
  376.           }
  377.         }
  378.       }
  379.     }
  380.  
  381.   } else {
  382.     ns.tprint("###########");
  383.     ns.tprint("You forgor the '--args' flag, it needs to be the last flag and args after it with + instead of - i.e. '++force'");
  384.     return 1
  385.   }
  386.   if (!dat['silent'] && dat['await']) {
  387.     ns.tprint("##################");
  388.     ns.tprint("## invExec done ##");
  389.     ns.tprint("##################");
  390.   }
  391.   return 0
  392. }
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. ---------
  401. spawnhackex.js
  402. ---------
  403. /** @param {NS} ns */
  404. export async function main(ns) {
  405.   const script = ns.args[0];
  406.   const target = ns.args[3];
  407.   const ramFraction = ns.args[1];
  408.   const delay = ns.args[2];
  409.   const me = ns.getHostname();
  410.   var maxRam = ns.getServerMaxRam(me);
  411.   if (typeof ns.args[4] == "undefined" || ns.args[4] != "silent") {
  412.     ns.tprint("Multi-", script + " '" + me + "' targeting: '" + target + "'");
  413.   }
  414.   var ramRemaining = maxRam - ns.getServerUsedRam(me);
  415.   var spawned = 0;
  416.   var inrow = 0;
  417.   while (inrow < 3600000 / delay) {
  418.     inrow += 1;
  419.     if (ramRemaining / maxRam > ramFraction) {
  420.       if (typeof ns.args[5] == "undefined") {
  421.         ns.run(script, { threads: 1, preventDuplicates: false }, "--host", target);
  422.       } else {
  423.         ns.run(script, { threads: 1, preventDuplicates: false }, "--host", target, "--hackLimiter", ns.args[5]);
  424.       }
  425.       spawned += 1;
  426.       inrow = 0;
  427.     }
  428.     await ns.asleep(delay);
  429.     maxRam = ns.getServerMaxRam(me);
  430.     ramRemaining = maxRam - ns.getServerUsedRam(me);
  431.   }
  432.   if (spawned > 10) {
  433.     ns.tprint("multi-", script + " '" + me + "' targeting: '" + target + "' has SPAWNED: ", spawned);
  434.   }
  435. }
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442. -----------
  443. hackex.js
  444. -----------
  445. /** @param {NS} ns */
  446. export async function main(ns) {
  447.   const selfver = ns.getHostname();
  448.   const dat = ns.flags([
  449.     ['host', ""],
  450.     ['moneyDiff', 500000.0],
  451.     ['securityDiff', 0.200],
  452.     ['hackScript', "/hack.js"],
  453.     ['growScript', "/grow.js"],
  454.     ['weakScript', "/weak.js"],
  455.     ['hackDivider', 2],
  456.     ['leaveRam', 0],
  457.     ['allocateRam', 999999999],
  458.   ]);
  459.  
  460.  
  461.   if (ns.getHostname() !== "home") {
  462.     const scriptuples = [[dat['hackScript'], "/hack.js"], [dat['growScript'], "/grow.js"], [dat['weakScript'], "/weak.js"]];
  463.     for (const script in scriptuples) {
  464.       ns.mv(selfver, scriptuples[script][0], scriptuples[script][1]);
  465.     }
  466.   }
  467.  
  468.  
  469.   const hackRam = ns.getScriptRam("/hack.js");
  470.   const growRam = ns.getScriptRam("/grow.js");
  471.   const weakRam = ns.getScriptRam("/weak.js");
  472.  
  473.   const securityMin = ns.getServerMinSecurityLevel(dat['host']);
  474.   const moneyMax = ns.getServerMaxMoney(dat['host']);
  475.   const myRam = ns.getScriptRam(ns.getScriptName())
  476.  
  477.  
  478.   var maxRam = ns.getServerMaxRam(selfver) - myRam - dat['leaveRam'];
  479.   var secFlag = false;
  480.   var freeRam = Math.min(maxRam - ns.getServerUsedRam(selfver), dat['allocateRam']);
  481.   var runWhat = "";
  482.   var securityCur = 0.0;
  483.   var moneyCur = 0.0;
  484.   var maxThreads = Math.floor(ns.hackAnalyzeThreads(dat['host'], moneyMax / dat['hackDivider']));
  485.   var runThreads = 0;
  486.   var runWait = 0;
  487.   var weakEff = 0.0;
  488.  
  489.   while (true) {
  490.     securityCur = ns.getServerSecurityLevel(dat['host']);
  491.     moneyCur = ns.getServerMoneyAvailable(dat['host']);
  492.     weakEff = ns.weakenAnalyze(1);
  493.     //hackThreadsRunning =
  494.  
  495.     switch (true) {
  496.       case (secFlag):
  497.         secFlag = false;
  498.         runWhat = "weak";
  499.         maxThreads = Math.floor(freeRam / weakRam);
  500.         runThreads = Math.ceil((securityCur - securityMin) / weakEff);
  501.         if (Math.max(runThreads, maxThreads) > 0) {
  502.           runWait = Math.max(ns.getWeakenTime(dat['host']), ns.getGrowTime(dat['host'])) + 20;
  503.         } else {
  504.           runWait = ns.getGrowTime(dat['host']) + 20;
  505.         }
  506.         break;
  507.  
  508.       case (securityCur - securityMin) > dat['securityDiff'] && (moneyMax - moneyCur) > dat['moneyDiff']:
  509.         secFlag = true;
  510.         runWhat = "grow";
  511.         maxThreads = Math.floor(Math.min(maxRam / growRam, (maxRam / weakRam) * 2 - ((securityCur - securityMin) / weakEff)));
  512.         if (ns.fileExists("Formulas.exe", "home")) {
  513.           runThreads = ns.formulas.hacking.growThreads(ns.getServer(dat['host']), ns.getPlayer(), moneyMax);
  514.         } else {
  515.           runThreads = Math.floor((moneyMax - moneyCur) / dat['moneyDiff']);
  516.         }
  517.         runWait = 1;
  518.         break;
  519.  
  520.       case (securityCur - securityMin) > dat['securityDiff']:
  521.         runWhat = "weak";
  522.         maxThreads = Math.floor(maxRam / weakRam);
  523.         runThreads = Math.ceil((securityCur - securityMin) / weakEff);
  524.         runWait = ns.getWeakenTime(dat['host']) + 20;
  525.         break;
  526.  
  527.       case (moneyMax - moneyCur) > dat['moneyDiff']:
  528.         runWhat = "grow";
  529.         maxThreads = Math.floor(maxRam / growRam);
  530.         if (ns.fileExists("Formulas.exe", "home")) {
  531.           runThreads = ns.formulas.hacking.growThreads(ns.getServer(dat['host']), ns.getPlayer(), moneyMax);
  532.         } else {
  533.           runThreads = Math.floor((moneyMax - moneyCur) / dat['moneyDiff']);
  534.         } runWait = ns.getGrowTime(dat['host']) + 20;
  535.         break;
  536.  
  537.       default:
  538.         runWhat = "hack";
  539.         runWait = ns.getHackTime(dat['host']) + 20;
  540.         break;
  541.     }
  542.     //if (runWhat != "hack" || Math.floor(freeRam / hackRam) > 0) {
  543.  
  544.     if (runWhat != "hack") {
  545.       /** Oh boy, here i go killing again */
  546.  
  547.       ns.scriptKill("/hack.js", selfver);
  548.       maxRam = ns.getServerMaxRam(selfver) - myRam - dat['leaveRam'];
  549.       freeRam = Math.min(maxRam - ns.getServerUsedRam(selfver), dat['allocateRam']);
  550.     }
  551.     ns.print(runWhat, " Needed:", runThreads, " Available:", maxThreads);
  552.  
  553.     /** The exec loop */
  554.     while (runWhat != "wait") {
  555.       if (runWhat == "hack") {
  556.         runThreads = Math.floor(freeRam / hackRam);
  557.         maxThreads = ns.hackAnalyzeThreads(dat['host'], moneyMax / dat['hackDivider']);
  558.       }
  559.  
  560.       runWhat = "/" + runWhat + ".js";
  561.       runThreads = Math.floor(Math.min(runThreads, maxThreads));
  562.       //ns.print(runWhat, dat['host'], " - " , runThreads);
  563.       if (runThreads > 0) {
  564.         ns.run(runWhat, { threads: runThreads, preventDuplicates: false }, dat['host']);
  565.       }
  566.  
  567.       if (secFlag == false) {
  568.         if (!runWhat.includes("hack")) {
  569.           runWhat = "hack";
  570.         } else {
  571.           runWhat = "wait";
  572.         }
  573.       } else {
  574.         runWhat = "wait";
  575.       }
  576.  
  577.       maxRam = ns.getServerMaxRam(selfver) - myRam - dat['leaveRam'];
  578.       freeRam = Math.min(maxRam - ns.getServerUsedRam(selfver), dat['allocateRam']);
  579.     }
  580.  
  581.     await ns.sleep(runWait);
  582.   }
  583. }
Advertisement
Add Comment
Please, Sign In to add comment