Bitburner – Script that crawls the network and maps out all the hosts

Bitburner – Script that crawls the network and maps out all the hosts 1 - steamlists.com
Bitburner – Script that crawls the network and maps out all the hosts 1 - steamlists.com

USAGE:
 
 

// <hostname> the name of the host that you want to start the crawl from. you do not need to connect to the host.
crawler.js <hostname>
// example: crawler.js n00dles

 
 
This is a basic depth-first-recursive crawler that maps out all the hosts in the game. It might be missing some connections if the game has host that are: A, are a different number of hops from home, and B, directly connect to each other. I haven’t fully tested that scenario. It currently shows whether or not you have root access to each host (‘Y’ or ‘N’) and can easily be tweaked to show more metadata, but that will likely add more to the RAM cost. Current iteration requires ~3.9GB RAM to execute, so there is room for improvement. Also, you may have some word-wrap issues on the terminal, depending on the starting host.
 
 

Code

/** @param {NS} ns **/export async function main(ns) {
 let home = new Object();
 let nodeMap = new Map();
 home.name - [home.name]  = ns.args[0];
 home.parent = null;
 home.neighbors = ns.scan(home.name); - [] 
 nodeMap.set(home.name - [home.name] , home);
 crawl(ns, home, nodeMap);
 printMap(ns, home, nodeMap, "");
}

/** @param {NS} ns **/function crawl(ns, node, nodeMap) {
 node.neighbors.forEach(nodeName => {
 if (nodeMap.has(nodeName)) {
 return;
 }
 let newNode = new Object();
 newNode.name - [newNode.name]  = nodeName;
 newNode.parent = node;
 newNode.neighbors = ns.scan(newNode.name); - [] 
 nodeMap.set(newNode.name - [newNode.name] , newNode);
 crawl(ns, newNode, nodeMap);
 });
}

/** @param {NS} ns **/function printMap(ns, node, nodeMap, indent) {
 ns.tprintf(indent + "%s%s", node.name - [node.name] , ns.hasRootAccess(node.name) - []  ? "(Y)" : "(N)");
 nodeMap.delete(node.name); - [] 
 indent = adjustIndent(indent, node, nodeMap);
 printIndent(ns, indent, node);
 
 node.neighbors.forEach(adjacentName => {
 if (nodeMap.has(adjacentName)) {
 printMap(ns, nodeMap.get(adjacentName), nodeMap, indent);
 }
 });
}

/** @param {NS} ns **/function printIndent(ns, indent, node) {
 if (node.neighbors.length > 1) {
 ns.tprintf(indent + "|");
 }
}

function adjustIndent(indent, node, nodeMap) {
 const INDENT_SPACE = 6;
 for (let i = 0; i < INDENT_SPACE; i++) {
 if (i === 0 && hasMoreSiblings(node, nodeMap)) {
 indent += "|";
 } else {
 indent += " ";
 }
 }
 return indent;
}

function hasMoreSiblings(node, nodeMap) {
 let hasSiblings = false;
 if (node.parent === null) {
 return hasSiblings;
 }
 node
 .parent
 .neighbors
 .forEach(adjacent => {
 if (nodeMap.has(adjacent)) {
 hasSiblings = true;
 }
 });
 return hasSiblings;
}

 
 

Written by DayTripperID

 
 
I hope you enjoy the Guide we share about Bitburner – Script that crawls the network and maps out all the hosts; if you think we forget to add or we should add more information, please let us know via commenting below! See you soon!
 
 


Be the first to comment

Leave a Reply

Your email address will not be published.


*