Skip to content

Instantly share code, notes, and snippets.

@eatnumber1
Last active August 21, 2022 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eatnumber1/7c3208e3ddd0936f54af52cb32d76792 to your computer and use it in GitHub Desktop.
Save eatnumber1/7c3208e3ddd0936f54af52cb32d76792 to your computer and use it in GitHub Desktop.
Bitburner utility function to create a generator that yields all hostnames.
/**
* Create a generator that yields all hostnames.
*
* @param {NS} ns
* @param {String} hostname the host to start traversal at
* @param {Set} seen used for internal recursion
* @yields {String} a hostname
*/
export function* getAllHostnames(ns, hostname = "home", seen = new Set()) {
seen.add(hostname);
yield hostname;
for (const peer of ns.scan(hostname)) {
if (seen.has(peer)) continue;
yield* getAllHostnames(ns, peer, seen);
}
}
// Example use of getAllHostnames
import {getAllHostnames} from "get_all_hostnames.js";
/** @param {NS} ns */
export async function main(ns) {
for (const host of getAllHostnames(ns)) {
ns.tprint(host);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment