Bitburner Basic Script.ns

Bitburner Basic Script.ns 1 - steamsplay.com
Bitburner Basic Script.ns 1 - steamsplay.com

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 === a[j])
a.splice(j--, 1);
}
}
return a;
};

export async function main(ns) {
//ns.disableLog("ALL"); //Enable 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, will be filled atomaticly

while(true){
//Find servers, nuke and add to list
//It is infinity for, that scan again and again all servers an new ones.
for(var element of servers) {
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 = 256; //Put here 8gb in early game
var myservers = ns.getPurchasedServers();
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);
}

//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 ;
var namekoef = [koef, element];
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, if hack, then use ony 80% of threads and anothers for grow.
//You can change % to experiment, try to do hack often but for small portions.
for(var rs of serversnuked){
var action;
if((ns.getServerMaxRam(rs) - ns.getServerUsedRam(rs)) >= 2) {
if (ns.getServerSecurityLevel(targetserver) > ns.getServerMinSecurityLevel(targetserver) + 5) { action = "weaken" }
else if (ns.getServerMoneyAvailable(targetserver) < ns.getServerMaxMoney(targetserver) * 0.85) { action = "grow" }
else { action = "hack" }
ns.print("We have " + action + " for " + targetserver);

if(action == "hack"){
var threads = Math.floor((ns.getServerMaxRam(rs) - ns.getServerUsedRam(rs)) / 1.75);
if(ns.isRunning("hack.ns", rs, targetserver))
{
ns.exec("grow.ns", rs, threads, targetserver);
ns.print("Grow on " + rs + " for " + targetserver);
}
if(threads >= 3) {
threads = threads * 0.8;
ns.exec("hack.ns", rs, threads, targetserver);
ns.print("Hack on " + rs + " for " + targetserver);
}
else{
ns.exec("hack.ns", rs, threads, targetserver);
ns.print("Hack on " + rs + " for " + targetserver);
}
}
else{
var threads = Math.floor((ns.getServerMaxRam(rs) - ns.getServerUsedRam(rs)) / ns.getScriptRam( action + ".ns"));
ns.exec(action + ".ns", rs, threads, targetserver);
ns.print(action + " on " + rs + " for " + targetserver);
}
}
else{}
}
}
}
}

 
 

 
 

Written by Nyarlathotep

 
 
Hope you enjoy the post for Bitburner Basic Script.ns, If you think we should update the post or something is wrong please let us know via comment and we will fix it how fast as possible! Thank you and have a great day!
 


2 Comments

  1. Yeah for me personally it seams to do an infinite loop of weakening and growing and then once a new server becomes available from an increased hacking level it loops weakening and growing again.. been running for almost 2 hours and has made zero money because of this.. it doesn’t really have time to start the hacking process before it moves to a new server

Leave a Reply

Your email address will not be published.


*