Kesseleth

check_and_hack.ns

Nov 2nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function isMyServer(ns, server) {
  2.     let myServersList = ["home", "darkweb, server_1-0"];
  3.  
  4.     let arr = ns.read("rooted.txt").split(":");
  5.     //ns.tprint(myServersList);
  6.     if (myServersList.indexOf(server) >= 0) ||{
  7.         //ns.tprint(server + " is my server. Don't hack");
  8.         return true;
  9.     } else {
  10.         //ns.tprint(server + " is not my server. Hack.");
  11.         return false;
  12.     }
  13. }
  14.  
  15.  
  16.  
  17. export async function main(ns) {
  18.     let port = ns.getPortHandle(2);
  19.     port.empty();
  20.     ns.disableLog("sleep");
  21.     ns.disableLog("getServerRam");
  22.     while (true) {
  23.         //If port 2 isn't empty
  24.         if (ns.peek(2) !="NULL PORT DATA") {
  25.             //Server = port 2-0, hackable = port 2-1
  26.             let listInfo = ns.read(2);
  27.             let server = listInfo[0];
  28.             let hackable = listInfo[1];
  29.             //ns.tprint (server + "is "+ hackable + " on hacking");
  30.             //if server is hackable, and hacking level is high enough,
  31.             //and there's enough RAM to run infect at least once, and the server is not mine
  32.             let ram = ns.getServerRam(ns.getHostname());
  33.             while ((ram[0] - ram[1] <= ns.getScriptRam("infect.ns"))) {
  34.                 await ns.sleep(1000);
  35.                 ram = ns.getServerRam(ns.getHostname());
  36.             }
  37.             //If I have root access but the server is not in my list of servers, add it
  38.             if (ns.hasRootAccess(server) && !(ismyServer(ns, server))) {
  39.                 ns.tprint("Already root access on " + server);
  40.                 ns.write("rooted", server+":", "a");
  41.             }
  42.            
  43.             //check for the following conditions:
  44.             //I do not already have root access.
  45.             //The server is not in my list of servers.
  46.             //I have enough RAM to run infect.ns.
  47.             //I have a high enough hacking level to hack the server.
  48.             //There are enough open ports to hack the server (shown by if hackable is true)
  49.             //If all of that is true...
  50.             if (!(ns.hasRootAccess(server)) &&
  51.                 !(isMyServer(ns, server)) &&
  52.                 (ram[0] - ram[1]) >= ns.getScriptRam("infect.ns") &&
  53.                 (ns.getHackingLevel() >= ns.getServerRequiredHackingLevel(server)) &&
  54.                 (hackable === true)
  55.                 ) {
  56.                 ns.print(server + " meets every requirement - hacking");
  57.                     //run infect.ns
  58.                     await ns.run("infect.ns", 1, server);
  59.                     ns.tprint("Rooting" + server);
  60.                     ns.write ("rooted", server + ":", "a");
  61.             } else {
  62.                 ns.print(server + " is not hackable currently");
  63.                
  64.             }
  65.         }
  66.     await ns.sleep(10);
  67.     ns.tprint("I'm working now!");
  68.        
  69.     }
  70. // wait one second
  71. }
Add Comment
Please, Sign In to add comment