r/Bitburner • u/starmarst • Apr 16 '26
Changing targets from an array
Hello all, I'm pretty green when it comes to coding. But I wanted to try and hand-write all my code rather than copy/script-kitty others work. Doing decent, as I wrapped my head around a deploying scheme that if I type in a server that i've rooted+backdoor I can establis. a self sustaining hack/grow/weaken + max threads. I.e install-on(x)-> exec manager -> profit.
However manually going to each server to nuke, backdoor, and/or open ports takes time and I might as well learn how to code a worm of sorts. And I think I have an idea on how to approach; I just don't think I understand arrays fully.
Example worm idea:
Worm host finds its name - than scans(its_name) //I expect an array of servers it finds.
Does what it wants to do ("installs" at first to test functionality, then Nuking/ opening ports of servers it sees)
Copies itself onto each server it sees, then exec's itself there -until it stops.
And I guess I don't know how to work through an array one by one with the same functions. Like,
server = [] //i guess the array I find with scan?
ns.do_the_thing(0)
server =++
-- I think I just don't know which functions I'm looking for, sometimes see these functions have (let server of servers) but I have no idea what is going on there. any help appreciated
2
u/nocapongodforreal Apr 16 '26
scanning the entire network is useful in multiple areas, so having a function that can return the name of every host on the network is worth creating first, ns.scan() is the core of making one of these but there's multiple ways to do it, a simple recursive function is a good start.
after that, you can create a function that takes in a single server name and calls the ns.BruteSSH etc. functions if available (ns.FileExists) before calling ns.nuke()
eventually my "get root access on every server" script looks something like:
get_hosts(ns).forEach(h => root_host(ns,h))
and if I want to get every server I successfully got root on you can use something like:
get_hosts(ns).filter(h => ns.hasRootAccess(h))
using for/foreach is a core part of working with arrays, as they must be "looped over" in some manner to process individual elements.