Script to crawl the network – Bitburner

Script to crawl the network – Bitburner 1 - steamclue.com
Script to crawl the network – Bitburner 1 - steamclue.com

Script that crawls the network and maps out all the hosts. Also shows whether or not you have root access. Can easily be tweaked to show more metadata. It’s depth-first recursive because I was too stupid to figure out breadth-first iterative. Current iteration requires 3.9GB RAM to execute; I’ll try to improve that.
 
 

Code

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

/** @param {NS} ns **/function discover(ns, node, nodeMap) {
 node.adjacents.forEach(nodeName => {
 if (nodeMap.has(nodeName)) {
 return;
 }
 let newNode = new Object();
 newNode.name - [newNode.name]  = nodeName;
 newNode.parent = node;
 newNode.adjacents = ns.scan(newNode.name); - [] 
 nodeMap.set(newNode.name - [newNode.name] , newNode);
 discover(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.adjacents.forEach(adjacentName => {
 if (nodeMap.has(adjacentName)) {
 printMap(ns, nodeMap.get(adjacentName), nodeMap, indent);
 }
 });
}

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

function adjustIndent(indent, node, nodeMap) {
 for (let i = 0; i < node.name.length - [name.length]  - 1; i++) {
 if (i === 0 && hasMoreSiblings(node, nodeMap)) {
 indent += "|";
 } else {
 indent += " ";
 }
 }
 return indent;
}

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

 
 

Written by DayTripperID

 
 
This is all we can share for Script to crawl the network – Bitburner for today. I hope you enjoy the guide! If you have anything to add to this guide or we forget something please let us know via comment! We check each comment! Don’t forget to check SteamClue.com for MORE!
 


Be the first to comment

Leave a Reply

Your email address will not be published.


*