Advertisement
omuretsu

Simple network scan and print

Oct 1st, 2022 (edited)
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns */
  2. export async function main(ns) {
  3.   // Store padding as a variable so we can keep track of the longest server name encountered.
  4.   let padding = 0;
  5.  
  6.   /** This function constructs the tree */
  7.   function getTreeFromHostname(parent = "", hostname = "home") {
  8.     // get all children of the host
  9.     let children = ns.scan(hostname).filter(child => child !== parent);
  10.     // set padding if this is the longest server name observed
  11.     padding = Math.max(padding, hostname.length + 1);
  12.     // Return hostname, plus recursive trees for all children
  13.     return [hostname, ...children.map(child => getTreeFromHostname(hostname, child))];
  14.   }
  15.  
  16.   /** This function returns a string representation of a tree. If no tree provided, it will make a new one using getTreeFromHostname. */
  17.   function printWithChildren(tree = getTreeFromHostname(), level = 0) {
  18.     //start a new line and add initial padding based on the level
  19.     let output = "\n" + " ".repeat(level * 3);
  20.     //Parse the current level of the tree into a hostname and its child trees
  21.     let [host, ...childTrees] = tree;
  22.     output += `${(host + ":").padEnd(padding, "-")}  Rooted: ${ns.hasRootAccess(host) ? "Yes" : "No "}  Backdoored: ${ns.getServer(host).backdoorInstalled ? "Yes" : "No "}`;
  23.     if (childTrees) childTrees.forEach(childTree => output += printWithChildren(childTree, level + 1));
  24.     return output;
  25.   }
  26.  
  27.   // All main needs to actually do is call the print function, which will grab the whole tree on its own due to default parameters.
  28.   ns.tprint(printWithChildren());
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement