xExekut3x

bitburner - find all rooted hosts from home

Jul 6th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | Gaming | 0 0
  1. /** @param {NS} ns **/
  2. function getRootedHomeNeighbors(ns) {
  3. /**
  4. * Return all rooted hosts from 'home'
  5. * Don't return private servers
  6. */
  7. let hosts = [];
  8. ns.scan('home').forEach(e => {
  9. if (e.includes('psrv-')) {
  10. return;
  11. }
  12. if (ns.hasRootAccess(e)) {
  13. hosts.push(e);
  14. }
  15. });
  16. return Object.values(hosts);
  17. }
  18.  
  19. /** @param {NS} ns **/
  20. function getRootedHostNeighbors(ns, host, upstream) {
  21. /**
  22. * Return all rooted hosts from host
  23. */
  24. let hosts = [];
  25. ns.scan(host).forEach(e => {
  26. // Ignore home & upstream neighbor
  27. if ((e === 'home') || (e === upstream)) {
  28. return;
  29. }
  30. if (ns.hasRootAccess(e)) {
  31. hosts.push(e);
  32. }
  33. });
  34. return Object.values(hosts);
  35. }
  36.  
  37. /** @param {NS} ns **/
  38. export function getRootedHosts(ns) {
  39. /**
  40. * Return all rooted hosts
  41. * Checking 5 Depth Levels
  42. */
  43. var hosts = [];
  44. // Depth 1
  45. getRootedHomeNeighbors(ns).forEach(homeNeighbor => {
  46. hosts.push(homeNeighbor);
  47.  
  48. // Depth 2
  49. let hostNeighbors1 = getRootedHostNeighbors(ns, homeNeighbor);
  50. hostNeighbors1.forEach(hostNeighbor1 => {
  51. hosts.push(hostNeighbor1);
  52.  
  53. // Depth 3
  54. let hostNeighbors2 = getRootedHostNeighbors(ns, hostNeighbor1, homeNeighbor);
  55. hostNeighbors2.forEach(hostNeighbor2 => {
  56. hosts.push(hostNeighbor2);
  57.  
  58. // Depth 4
  59. let hostNeighbors3 = getRootedHostNeighbors(ns, hostNeighbor2, hostNeighbor1);
  60. hostNeighbors3.forEach(hostNeighbor3 => {
  61. hosts.push(hostNeighbor3);
  62.  
  63. // Depth 5
  64. let hostNeighbors4 = getRootedHostNeighbors(ns, hostNeighbor3, hostNeighbor2);
  65. hostNeighbors4.forEach(hostNeighbor4 => {
  66. hosts.push(hostNeighbor4);
  67. // Done for now, may get deeper scanning later
  68. })
  69. });
  70. });
  71. });
  72.  
  73. });
  74.  
  75. return Object.values(hosts);
  76. }
Tags: Bitburner
Advertisement
Add Comment
Please, Sign In to add comment