2.0 KiB
NS.scan() method
Get the list of servers connected to a server.
Signature:
scan(host?: string): string[];
Parameters
| Parameter | Type | Description |
|---|---|---|
| host | string | (Optional) Optional. Hostname of the server to scan, default to current server. |
Returns:
string[]
Returns an array of hostnames.
Remarks
RAM cost: 0.2 GB
Returns an array containing the hostnames of all servers that are one node way from the specified target server. The hostnames in the returned array are strings.
The server network is a tree graph with the home server at the root. The parent node is always the first item of the returned array.
For example, let's say the network looks like this:
home
--n00dles
--joesguns
----CSEC
------omega-net
ns.scan("home"): ["n00dles", "joesguns"]: "home" is the root, so it does not have a parent node.
ns.scan("n00dles"): ["home"]: "home" is the parent node of "n00dles".
ns.scan("joesguns"): ["home", "CSEC"]: "home" is the parent node of "joesguns".
ns.scan("CSEC"): ["joesguns", "omega-net"]: "joesguns" is the parent node of "CSEC".
ns.scan("omega-net"): ["CSEC"]: "CSEC" is the parent node of "omega-net".
If you run the "scan-analyze" command at home, it won't show all servers due to its limited maximum depth. You can use this function with BFS (Breadth-first search) or DFS (Depth-first search) to traverse the network and discover all servers.
Example
// All servers that are one hop from the current server.
ns.tprint("Neighbors of current server.");
let neighbor = ns.scan();
for (let i = 0; i < neighbor.length; i++) {
ns.tprint(neighbor[i]);
}
// All neighbors of n00dles.
const target = "n00dles";
neighbor = ns.scan(target);
ns.tprintf("Neighbors of %s.", target);
for (let i = 0; i < neighbor.length; i++) {
ns.tprint(neighbor[i]);
}