Created
May 23, 2022 17:13
-
-
Save steven-r/ba9c670efb9e51964ca70950616c7f45 to your computer and use it in GitHub Desktop.
Simple restarter script which runs all found scripts on all hosts. Does not run scripts twice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @param {NS} ns */ | |
export async function main(ns) { | |
ns.disableLog("scan"); // ignore scan log messages | |
const servers = scanAllServers(ns); | |
for (const hostname of servers) { | |
rerunScriptsOnHost(ns, hostname); | |
} | |
} | |
/** Helper to get a list of all hostnames on the network | |
* @param {NS} ns | |
*/ | |
function scanAllServers(ns) { | |
let discoveredHosts = []; // Hosts (a.k.a. servers) we have scanned | |
let hostsToScan = ["home"]; // Hosts we know about, but have no yet scanned | |
while (hostsToScan.length > 0) { // Loop until the list of hosts to scan is empty | |
let hostName = hostsToScan.pop(); // Get the next host to be scanned | |
for (const connectedHost of ns.scan(hostName)) // "scan" (list all hosts connected to this one) | |
if (!discoveredHosts.includes(connectedHost)) // If we haven't already scanned this host | |
hostsToScan.push(connectedHost); // Add it to the queue of hosts to be scanned | |
discoveredHosts.push(hostName); // Mark this host as "scanned" | |
} | |
return discoveredHosts; // The list of scanned hosts should now be the set of all hosts in the game! | |
} | |
/** | |
* re-run all found scripts on a particular host | |
* @param {NS} ns | |
* @oaran {string} hostname | |
*/ | |
function rerunScriptsOnHost(ns, hostname) { | |
for (const file of ns.ls(hostname, ".script")) { // NS1 | |
runFile(ns, hostname, file); | |
} | |
for (const file of ns.ls(hostname, ".js")) { // NS2 | |
runFile(ns, hostname, file); | |
} | |
} | |
/** | |
* Execute a script on a host | |
* @param {NS} ns | |
* @param {string} hostname | |
* @param {strinh} scriptname | |
*/ | |
function runFile(ns, hostname, scriptname) { | |
if (ns.getRunningScript(scriptname, hostname)) { | |
ns.print(`Ignore running script ${scriptname} on host ${hostname}`); | |
return; | |
} | |
ns.exec(scriptname, hostname); | |
// ns.print(`Executing ${scriptname} on ${hostname}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment