Bitburner

Bitburner

Not enough ratings
Basic script.ns to everyone
By IsKon
Here is simple script, that can be started in any moment of the game. It will buy servers, find all accessible servers, open ports, nuke, find target server, hack, grow and weak servers. And will do it again and again.

/** @param {NS} ns **/

//additional function, needed to merge arrays.
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};

export async function main(ns) {
ns.disableLog("ALL"); //Disable it for troubleshoting
ns.print("Start");
//You should create basic scripts with exact names. Like await ns.hack(ns.args[0]);
var files = ["weaken.ns", "grow.ns", "hack.ns"];
var servers = ns.scan("home");
var newservers;
var koefs = [0];
var targetserver = "n00dles"; //will be changed later
var serversnuked = ["home"]; //list of nuked, active servers

while(true){
//Find servers, nuke and add to list
for(var element of servers) { //It is infinity for, that scan again and again all servers an new ones.
await ns.sleep(1000);
ns.clearLog();
var newservers = ns.scan(element);
var servers = servers.concat(newservers).unique();
ns.print("Start for " + element);

//Buy servers
var ram = 8;
var myservers = ns.getPurchasedServers();
if(myservers[1]){var ramofmyservers = ns.getServerMaxRam(myservers[1])}
await ns.sleep(1000);
if (myservers.length < ns.getPurchasedServerLimit() && ns.getServerMoneyAvailable("home") > ns.getPurchasedServerCost(ram)) {
var number = myservers.length + 1;
var hostname = ns.purchaseServer("pserv-" + number, ram);
await ns.scp(files, "home", hostname);
ns.killall(hostname);
ns.print("Buy " + hostname);
}
else if(ns.getPurchasedServerCost(ramofmyservers * 2) * 250 < ns.getServerMoneyAvailable("home")){
for(var ser of myservers){
ns.killall(ser);
ns.deleteServer(ser);
}
for(var i = 0; i < 25; i++){
var myservers = ns.getPurchasedServers();
var ram = ramofmyservers * 2;
var number = myservers.length + 1;
var hostname = ns.purchaseServer("pserv-" + number, ram);
await ns.scp(files, "home", hostname);
ns.killall(hostname);
ns.print("Buy " + hostname);
}
}

//Check ports power
var portpower = [0, 0, 0, 0, 0];
var portcounter = 0;
if (ns.fileExists("BruteSSH.exe")) { portpower[0] = 1 ; portcounter++ };
if (ns.fileExists("FTPCrack.exe")) { portpower[1] = 1 ; portcounter++ };
if (ns.fileExists("relaySMTP.exe")) { portpower[2] = 1 ; portcounter++ };
if (ns.fileExists("HTTPWorm.exe")) { portpower[3] = 1 ; portcounter++ };
if (ns.fileExists("SQLInject.exe")) { portpower[4] = 1 ; portcounter++ };

//Nuke servers
if (serversnuked.indexOf(element) >= 0) {ns.print("Already nuked " + element)}
else if (ns.hasRootAccess(element)) {
serversnuked.push(element);
await ns.scp(files, "home", element);
}
else if (ns.getServerNumPortsRequired(element) > portcounter) { ns.print("No port power for " + element) }
else if (ns.getHackingLevel() < ns.getServerRequiredHackingLevel(element)) { ns.print("No skill for" + element) }
else {
if (portpower[0]) { ns.brutessh(element) };
if (portpower[1]) { ns.ftpcrack(element) };
if (portpower[2]) { ns.relaysmtp(element) };
if (portpower[3]) { ns.httpworm(element) };
if (portpower[4]) { ns.sqlinject(element) };
ns.nuke(element);
ns.print("Nuked " + element);
//ns.installBackdoor(element); //Will work in mid game

//To push files
await ns.scp(files, "home", element);
await ns.killall(element);
serversnuked.push(element);
ns.print("all files on " + element);
}

//To find a target, You can comment this part and just use targetserver
if(serversnuked.indexOf(element) >= 0){
var money = ns.getServerMaxMoney(element);
var security = ns.getServerMinSecurityLevel(element);
var koef = money / security * 0.000001 ;
if(koefs.some(el => el >= koef)) {
koefs.push(koef);
}
else {
targetserver = element;
koefs.push(koef);
ns.print(targetserver);
}
}

//Launch action on all servers nuked.
//Check what should be done, weak, grow or hack
//You can change % to experiment, try to do hack often but for small portions. 0.01 it is for late game!!! Sum of 3 numbers should be 1!!
for(var rs of serversnuked){
if((ns.getServerMaxRam(rs) - ns.getServerUsedRam(rs)) >= 2) {

var threadsH = Math.floor(Math.floor((ns.getServerMaxRam(rs)) / 1.75)* 0.26);
var threadsW = Math.floor(Math.floor((ns.getServerMaxRam(rs)) / 1.75)* 0.62);
var threadsG = Math.floor(Math.floor((ns.getServerMaxRam(rs)) / 1.75)* 0.30);
if(rs == "home"){
var threadsH = Math.floor(((ns.getServerMaxRam(rs) - 20) / 1.75)* 0.26);
var threadsW = Math.floor(((ns.getServerMaxRam(rs) - 20) / 1.75)* 0.62);
var threadsG = Math.floor(((ns.getServerMaxRam(rs) - 20) / 1.75)* 0.30);
}
if(ns.scriptRunning("weaken.ns", rs)){}
else if(threadsW > 0){ns.exec("weaken.ns", rs, threadsW, targetserver);}
if(ns.scriptRunning("grow.ns", rs)){}
else if(threadsG > 0){ns.exec("grow.ns", rs, threadsG, targetserver);}
if(ns.scriptRunning("hack.ns", rs)){}
else if(threadsH > 0){ns.exec("hack.ns", rs, threadsH, targetserver);}
}
else{}
}
}
}
}
2
2
   
Award
Favorite
Favorited
Unfavorite
25 Comments
Daemonmatrix 9 Apr, 2022 @ 11:11pm 
Using this script you can buy enormous servers though I'm not sure I recommend it lol I bought all 16tb servers for like 900m+ each.
Crypto_Nym 9 Apr, 2022 @ 5:09pm 
The install backdoor part of the script doesn't work for me, every time I try to enable it, the script complains that it's waiting for a grow or weaken or hack to finish and terminates
Commissioner Jean Claude Van Dan 8 Apr, 2022 @ 6:43pm 
This isn't doing anything but going through the cycles of the servers, then it buys a new server when I have the money, but at no time is it growing, weakening or hacking anything. Been running for hours.
IsKon  [author] 8 Jan, 2022 @ 10:59pm 
oculomind, i suppose that it is becouse of this string - (ns.getPurchasedServerCost(ramofmyservers * 2) * 250 < ns.getServerMoneyAvailable("home"))
To buy 16gb servers you need money for 250 that servers, then script will buy 25 in one time. You can change number 250(its just to avoid situation when more than 10% of money are spend to servers)
oculomind 8 Jan, 2022 @ 2:13pm 
Awesome script, Thank you

Currently the servers wont be purchased with more than 8 ram even with available money.
IsKon  [author] 2 Jan, 2022 @ 11:51pm 
SirGouki, thanks for comments!
I wiil try to put that it the code, to fix
IsKon  [author] 2 Jan, 2022 @ 11:49pm 
Who are at the begining and dont have HACK
Change 0.01 to the 0.2 at least. But be carefull, all 3 numbers should be 1 together.
For 0.2 hack, it is 0.3 grow and 0.5 weak like example.
0.01 is set only because in late game your hack will be super strong and on home computer you will have thousands of threads.
SirGouki 2 Jan, 2022 @ 8:49pm 
The following works the same way for those who want to use the script above with the vue dashboard:




function concatArray(destination, source)
{
var final = destination;

for(var i = 0; i < source.length; i++)
{
if(!final.includes(source[i]))
{
final.push(source[i]);
}
}

return final;
}

replace var servers = servers.concat(newservers).unique(); with servers = concatArray(servers, newservers); YOU DO NOT EVER NEED TO USE VAR TO REASSIGN EXISTING VARIABLES, THAT CREATES A NEW INSTANCE INSTEAD AND WILL LEAD TO MORE GC CALLS IN LANGUAGES THAT HAVE THEM.
SirGouki 2 Jan, 2022 @ 8:29pm 
Warning: The code at the top of this script (the code regarding merging arrays) will break other scripts, depending on what they do.

see the following: https://github.com/smolgumball/bitburner-vue/issues/10
Pika64 2 Jan, 2022 @ 7:45pm 
same but no grow and it tries to nuke