Code Examples

Generic hacking script for low-level servers
This is a basic generic hacking script. It can be used for low-level servers that requires at most 1 port opened in order to gain root access.

This script takes in three arguments. The first argument, stored in the  variable, defines the target server that this script is going to hack. The second argument, stored in the  variable (short for "money threshold"), defines the minimum amount of money that must be available on the target server in order for our script to hack it. If there is less money than this "money threshold," then our script will grow the server rather than hacking it. The third argument, stored in the  variable (short for "security level threshold"), defines the maximum security level the target server can have. If the target server's security level is above this threshold, then the script will weaken it before growing/hacking it.

After storing the arguments in variables, this script will call nuke and brutessh (if the player has the BruteSSH.exe program) on the target server in order to gain root access to it. Then, the script will continuously weaken, grow, and hack the target server based on the "threshold" values that were passed in by arguments. //Works for servers that need up to 1 port opened //Requires 3 arguments: target server, money threshold, and security threshold target = args[0]; moneyThresh = args[1]; securityThresh = args[2]; if (fileExists("BruteSSH.exe", "home")) { brutessh(target); } nuke(target); while(true) { if (getServerSecurityLevel(target) > securityThresh) { //If the server's security level is above our threshold, weaken it        weaken(target); } else if (getServerMoneyAvailable(target) < moneyThresh) { //If the server's money is less than our threshold, grow it        grow(target); } else { //Otherwise, hack it        hack(target); } }

Generic smart hacking script for low-level servers
This script is similar to the generic hacking script above except it automatically calculates the money threshold and security threshold rather than taking them in as arguments. I recommend only using this script if you have enough RAM to run it with many threads.

//Works for servers that need up to 1 port opened //Requires 1 argument: the target server. target = args[0]; moneyThresh = getServerMaxMoney(target) * 0.9; securityThresh = Math.round(getServerBaseSecurityLevel(target) / 3) + 2; if (fileExists("BruteSSH.exe", "home")) { brutessh(target); } nuke(target); while(true) { if (getServerSecurityLevel(target) > securityThresh) { //If the server's security level is above our threshold, weaken it        weaken(target); } else if (getServerMoneyAvailable(target) < moneyThresh) { //If the server's money is less than our threshold, grow it        grow(target); } else { //Otherwise, hack it        hack(target); } }

Generic smart hacking script for high-level servers
This script is a generic hacking script similar to the one above except that it will use all available port-opening programs and can thus be used for high level servers.

//Requires 1 argument: The target server target = args[0]; moneyThresh = getServerMaxMoney(target) - 2000000; securityThresh = Math.round(getServerBaseSecurityLevel(target) / 3) + 2; programs = ["BruteSSH.exe", "FTPCrack.exe", "relaySMTP.exe", "HTTPWorm.exe", "SQLInject.exe"]; for (i = 0; i < programs.length; ++i) { if (fileExists(programs[i], "home")) { if (i === 0) {brutessh(target);} if (i === 1) {ftpcrack(target);} if (i === 2) {relaysmtp(target);} if (i === 3) {httpworm(target);} if (i === 4) {sqlinject(target);} } } nuke(target); while(true) { if (getServerSecurityLevel(target) > securityThresh) { //If the server's security level is above our threshold, weaken it        weaken(target); } else if (getServerMoneyAvailable(target) < moneyThresh) { //If the server's money is less than our threshold, grow it        grow(target); } else { //Otherwise, hack it        hack(target); } }

Early game script-distribution template
When starting out, it is advised that you take advantage of the RAM on other servers in order to run your scripts. Many of the servers with a required hacking level up to 50 have 8GB of RAM that you can use to run scripts. This is a template that can be used to automatically copy scripts from our home computer onto these servers and then execute them.

'''NOTE: This is a template, which means it will not work "out of the box". You will need to adapt it to your game and fill in certain values.'''

//All of these servers have 8GB of RAM that can be used to run scripts servers = ["foodnstuff", "sigma-cosmetics", "joesguns", "nectar-net", "hong-fang-tea", "harakiri-sushi"]; i = 0; while (i < servers.length) { //OPTIONAL //Wait for player to reach the correct hacking level while (getHackingLevel < getServerRequiredHackingLevel(servers[i])) { sleep(20000); }    //Copy whatever scripts we want onto the target server scp("DESIRED SCRIPT NAME HERE", servers[i]); scp("ANOTHER SCRIPT NAME HERE, IF YOU WANT", servers[i]); //NUKE the target server to gain root access nuke(servers[i]); //Execute our scripts on the target server exec("SCRIPT NAME", servers[i], NUMBER THREADS, SCRIPT ARGUMENTS); exec("ANOTHER SCRIPT NAME", servers[i], NUMBER THREADS, SCRIPT ARGUMENTS); ++i; }

Early game script-distribution with functions
This is a slightly modified version of the above script to demonstrate the use of basic functions for certain tasks. It will also calculate the maximum number of threads for your given script. Optimization is left as an exercise to the reader! // count how many programs we have to open ports function countPrograms { var count = 0; if ( fileExists( "BruteSSH.exe" ) ) count++; if ( fileExists( "FTPCrack.exe" ) ) count++; if ( fileExists( "relaySMTP.exe" ) ) count++; if ( fileExists( "HTTPWorm.exe" ) ) count++; if ( fileExists( "SQLInject.exe" ) ) count++; return count; } // try to open every port we can function breakPorts( hostname) { if ( fileExists( "BruteSSH.exe" ) ) brutessh( hostname ); if ( fileExists( "FTPCrack.exe" ) ) ftpcrack( hostname ); if ( fileExists( "relaySMTP.exe" ) ) relaysmtp( hostname ); if ( fileExists( "HTTPWorm.exe" ) ) httpworm( hostname ); if ( fileExists( "SQLInject.exe" ) ) sqlinject( hostname ); } var servers = [ "foodnstuff", "sigma-cosmetics", "joesguns" ]; var target = "joesguns"; var ram = 0; var cost = 0; var threads = 0; var i = 0; while ( i < servers.length ) { // check if we have the required skill to hack the server if ( getHackingLevel >= getServerRequiredHackingLevel( servers[i] ) ) { // sleep until we have enough port openers to nuke the server while ( countPrograms < getServerNumPortsRequired( servers[i] ) ) { sleep( 20000 ); } 		// we now have enough port openers, so open ports and nuke the server breakPorts( servers[i] ); nuke( servers[i] ); // copy the hacking script over scp( "YOUR SCRIPT HERE", servers[i] ); // figure out how many threads we can run of our script ram = getServerRam( servers[i] ); cost = getScriptRam( "YOUR SCRIPT HERE" ); threads = Math.floor( ( ram[0] - ram[1] ) / cost ); // execute script on the target server exec( "YOUR SCRIPT HERE", servers[i], target, threads, "SCRIPT ARGUMENTS HERE" ); // increment the counter to move to the next server i++; } } '''NOTE: This is a template, which means it will not work "out of the box". You will need to adapt it to your game and fill in certain values.'''

Automatic Server Purchasing
This is a templated script that can be adapted to automatically purchase servers using the purchaseServer Netscript function, and then your scripts on those servers.

'''NOTE: This is a template, which means it will not work "out of the box". You will need to adapt it to your game and fill in certain values.'''

i = 0; /* OPTIONAL: Define a single target and gain root access to it  target = "alpha-ent"; ftpcrack(target); httpworm(target); brutessh(target); relaysmtp(target); sqlinject(target); nuke(target); */  //Amount of RAM on your purchased servers amtRam = 64; //Calculate the number of threads of your desired script you can run numThreads = round(amtRam / getScriptRam("YOUR HACKING SCRIPT HERE", "home")) - 1; //You can have a maximum of 25 purchased servers while(i < 25) { //The price of purchasing a server is the RAM * 50,000 if (getServerMoneyAvailable("home") > (amtRam * 50000)) { hostname = purchaseServer("pserv-" + i, amtRam); scp("YOUR HACKING SCRIPT HERE", hostname); exec("YOUR HACKING SCRIPT HERE", hostname, numThreads, SCRIPT PARAMETERS); ++i; }  }

findServer (find the path through the network to the designated server)
This script is used to find a server in the network. It takes an argument that specifies the target server's hostname and prints the path to that server.

This can be useful for finding the Bitrunners server or the 'w0r1d_d43m0n' server.

Note that the hostname in the argument is case sensitive. Passing in an invalid hostname will cause the script to run forever (I think)

Also note that if you are searching for a server that is deep in the network, this script could take a while to run. //Uses a DFS to find the path to the specified server and then prints the path //to Terminal. //The target server's HOSTNAME must be a string passed in as an argument to the script. //It is CASE-SENSITIVE. //If an invalid hostname is passed the script will probably just run forever. target = args[0]; visited = []; stack = []; parentTracker = []; origin = getHostname; stack.push(origin); while(stack.length > 0) { node = stack.pop; print("DFS processing server " + node); if (visited.includes(node)) { //Do nothing. Essentially a "continue" but that doesn't exist yet } else { if (node == target) {break;} visited.push(node); nextNodes = scan(node); for (i = 0; i < nextNodes.length; ++i) { stack.push(nextNodes[i]); //Keep track of the nodes "parent" so we can re-create the path //Ignore entries that start at the origin if (nextNodes[i] != origin) { pair = [nextNodes[i], node]; parentTracker.push(pair); }        }     } } print("Target found. About to re-create path"); print("parentTracker size: " + parentTracker.length); path = []; i = target; while (i != getHostname) { path.push(i); print("Re-creating path at " + i); //Search through the parentTracker array to find this nodes parent for (j = 0; j < parentTracker.length; ++j) { pair = parentTracker[j]; if (pair[0] == i) { i = pair[1]; break; }    } } path.reverse; tprint(path);

You can then use aliases to automatically run this script for certain special servers:

alias findBitrunners="run findServer.script run4theh111z" alias findFulcrum="run findServer.script fulcrumassets" alias findWorldDaemon="run findServer.script w0r1d_d43m0n"

Download all Literature files to Home Computer
This is a set of two scripts that can be used to download all of the literature files in the game to your home computer. Literature files are simply files containing text that help establish the in-game lore. Literature files can be read using the cat Terminal command

The first script is simple. It should be named "getLit.script": litName = args[0]; scp(litName, "home");

The second script uses a DFS to navigate the entire network and search for literature files. It then uses the getLit.script to download those files. This script requires all 5 port-opening programs in order to run. //Uses a DFS to search through all servers & check if they have any literature (.lit) files. //If they do, it launches the getLit script to copy the literature files onto the home //computer //Requires all 5 port-opening programs visited = []; stack = []; origin = getHostname; stack.push(origin); while(stack.length > 0) { node = stack.pop; print("DFS processing server " + node); if (visited.includes(node)) { //Do nothing. Essentially a "continue" but that doesn't exist yet } else { visited.push(node); //Only check for lit files on servers that aren't home computer if (node != "home") { //Check the files on this server files = ls(node); for (i = 0; i < files.length; ++i) { if (files[i].endsWith(".lit")) { //Get root access to server. brutessh(node); ftpcrack(node); relaysmtp(node); httpworm(node); sqlinject(node); nuke(node); scp("getLit.script", node); while(exec("getLit.script", node, 1, files[i]) === false) { sleep(10000); }                    print("Copying " + files[i] + " to home"); }            }         }         nextNodes = scan(node); for (i = 0; i < nextNodes.length; ++i) { stack.push(nextNodes[i]); }    } }

Get all servers on the network
A relatively simple script to get all servers in the network and print them to the terminal.

foundnodes = []; stack = []; origin = getHostname; stack.push(origin); while(stack.length > 0) { node = stack.pop; if (foundnodes.includes(node)) { //Do nothing => "continue" } else { foundnodes.push(node); nextNodes = scan(node); for (i = 0; i < nextNodes.length; ++i) { stack.push(nextNodes[i]); }    } } for(i = 0; i < foundnodes.length; i++) { tprint(foundnodes[i]); }

Establish a botnet (advanced script distribution)
Can be used to spread your hack scripts to all servers on the network that have enough RAM to run them. // Script to spread to several servers // // Configuration, change this! // expects 1 argument: target; uses programs and NUKE.exe to get root on a target nukescript = "nuke-target.script"; // DEFINE AS ARRAY! expects 1 argument: target (self adjusts money and security thresholds); will be run as many times as the RAM allows hackscript = ['hack-target.script']; // DEFINE AS ARRAY! expects 1 argument: target; will be run once per server weakenscript = ['weaken-target.script']; // target server. Chose a server with good max money and growth target = 'CHANGEME'; // servers to spread the scripts to servers = ["SERVER1", "SERVER2"]; // internal config, don't touch hackscript.push(getScriptRam(hackscript[0], "home")); weakenscript.push(getScriptRam(weakenscript[0], "home")); programs = ["BruteSSH.exe", "FTPCrack.exe", "relaySMTP.exe", "HTTPWorm.exe", "SQLInject.exe"]; // script start tprint("Hack target set to " + target); tprint(" Current target money: " + (getServerMoneyAvailable(target) / 1000000) + "M"); tprint(" Max target money: " + (getServerMaxMoney(target) / 1000000) + "M"); tprint(" Growth factor: " + getServerGrowth(target)); tprint("Got " + servers.length + " servers to take over..."); tprint("Starting..."); // Make sure we the target first target_hacked = false; while(target_hacked === false) { pcount = 0; for(i = 0; i < programs.length; i++) { if(fileExists(programs[i], "home")) { pcount++; }   }    req_ports = getServerNumPortsRequired(target); if(pcount >= req_ports) { if(getHackingLevel >= getServerRequiredHackingLevel(target)) { if(hasRootAccess(target) === false) { exec(nukescript, "home", 1, target); while(hasRootAccess(target) === false) { sleep(1000); }           }            target_hacked = true }       else  { print("Hacking level: " + getHackingLevel + "/" + getServerRequiredHackingLevel(target)); sleep(10000); }   }    else { print("OpenPorts: " + pcount + "/" + req_ports); sleep(10000); } } // now spread our scripts across the network while(servers.length > 0) { // update port programs every time we run the loop pcount = 0; for(i = 0; i < programs.length; i++) { if(fileExists(programs[i], "home")) { pcount++; }   }    // array for next loop run remaining_servers = []; // let's go   for(i = 0; i < servers.length; i++) { current_server = servers[i]; hacked = false; req_ports = getServerNumPortsRequired(current_server); if(pcount >= req_ports) { if(getHackingLevel >= getServerRequiredHackingLevel(current_server)) { scp(hackscript[0], current_server); scp(weakenscript[0], current_server); // get root on the server if(hasRootAccess(current_server) === false) { exec(nukescript, "home", 1, current_server); while(hasRootAccess(current_server) === false) { sleep(1000); }               }                available_ram = getServerRam(current_server)[0] - weakenscript[1]; num_threads = round(available_ram / hackscript[1]) - 1; if(num_threads >= 1) { exec(hackscript[0], current_server, num_threads, target); exec(weakenscript[0], current_server, 1, target); }               else { print("Not enough RAM on " + current_server + ". Not starting any hack scripts..."); }               hacked = true; }       }        if(hacked === false) { remaining_servers.push(current_server); }   }    servers = remaining_servers; tprint("Establishing botnet: " + servers.length + " servers left to take over..."); sleep(10000); }

MercuriusXeno's Smart Trading Scripts
A very advanced script that intelligently and efficiently hacks/grows/weakens servers.

May require slight adjustments for more recent versions of the game

https://www.reddit.com/r/Bitburner/comments/70ikjm/progression_scripts_0286/

stock-sma.script
This is a simple script that calculates the simple moving average (SMA) of a stock and outputs it to a Netscript Port

sym = args[0];      //Symbol of stock window = args[1];   //Number of price changes for the SMA's 'time period' outPort = args[2];  //Port to write SMA to prices = []; sum = 0; lastPrice = -1; while(true) { price = getStockPrice(sym); if (price != lastPrice) { prices.push(price); sum += price; if (prices.length > window) { foo = prices.shift; sum -= foo; write(outPort, sum/window); print(sum/window); }        lastPrice = price; } }