-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicDeploy.js
More file actions
70 lines (54 loc) · 2.35 KB
/
basicDeploy.js
File metadata and controls
70 lines (54 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @param {NS} ns
**/
export async function main(ns) {
const args = ns.flags([['help', false]]);
const serverToHack = args._[0];
const serverRunningScript = ns.getHostname();
const nukeScript = "nukeServer.js";
const hackScript = "basicHack.js"
let rootAccess = ns.hasRootAccess(serverToHack);
// If missing argument or help
if (args.help || !serverToHack) {
ns.tprint("This script will generate money by hacking a target server.");
ns.tprint(`USAGE: run ${ns.getScriptName()} SERVER_NAME`);
ns.tprint("Example:");
ns.tprint(`> run ${ns.getScriptName} n00dles`);
return;
}
// Check that target server exists
if (!ns.serverExists(serverToHack)) {
ns.tprint("Target Server does not exist. Aborting basicHack.js");
return;
}
// Check if the script for gaining access exists on the current server, copy it to it if it does not.
if (!ns.fileExists(nukeScript)) {
await ns.scp(nukeScript, "home", serverRunningScript);
}
// Try to gain root access
// TODO: Change this to import instead, threading issues currently
if (!rootAccess) {
ns.exec(nukeScript, serverRunningScript, 1, serverToHack);
ns.sleep(100000)
let rootAccess = ns.hasRootAccess(serverToHack);
if (!rootAccess) {
ns.tprint(`Aborting deploy to ${serverToHack} because of no root access.`);
return;
}
}
// "deploy" a hacking script, with max threads
// Check if the hacking script exists on the target server, copy it to it if it does not.
if (!ns.fileExists(hackScript, serverToHack)) {
await ns.scp(hackScript, "home", serverToHack);
}
const targetServerMaxRam = ns.getServerMaxRam(serverToHack);
const targetServerUsedRam = ns.getServerUsedRam(serverToHack);
const scriptRamUsage = ns.getScriptRam(hackScript, serverToHack);
const threads = Math.floor((targetServerMaxRam - targetServerUsedRam) / scriptRamUsage);
if (threads > 0) {
ns.tprint(`Launching script ${hackScript} on ${serverToHack} with ${threads} and the following arguments: ${serverToHack}`);
ns.exec(hackScript, serverToHack, threads, serverToHack);
return;
}
ns.tprint(`The target server (${serverToHack} can not support any threads of the script (${hackScript}))`);
}