-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
120 lines (117 loc) · 2.91 KB
/
main.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var fs = require("fs"),
exec = require("child_process").exec,
platform = process.platform,
interface = process.argv[2];
function interfaceChecker(callback){
if(interface == undefined){
log("auto detecting interface...")
var interfaces = ["wlan0", "eth0", "en0", "wlan1", "en1"];
exec("ifconfig", function(err, stdout, stderr){
var i = 0;
function next(){
if(stdout.match(interfaces[i])){
interface = interfaces[i];
callback()
}else if(i == interfaces.length){
log("auto-detection failed.", "error");
}else{
i += 1;
next()
}
};
next();
})
}else{
callback();
}
}
function log(text, type){
var io = {
log: function(){
console.log("* "+text);
},
error: function(){
console.log("!! "+text);
}
};
if(io[type] == undefined){type = "log"};
io[type]();
}
var commands = {
changeMAC: {
linux: "ifconfig {{interface}} down;"+
"ifconfig {{interface}} hw ether {{MAC}};"+
"ifconfig {{interface}} up;",
darwin: "ifconfig {{interface}} ether {{MAC}}"
}
}
function command(name, vars){
var s = commands[name][platform]
for(i in vars){
s = s.replace("{{"+i+"}}", vars[i])
}
return s;
}
function getNodes(callback){
console.log("* getting all network nodes...")
exec("fing -r 1 -o table,csv,nodes.csv", function(err, stdout, stderr){
if(err || stderr){
console.log("!! error at execution:");
if(err) console.log(err);
if(stderr) console.log(stderr);
}else{
var NumOfNodes = /has ([1-9]*)/g.exec(stdout).index;
console.log("* "+NumOfNodes+" are up");
console.log("* reading output file")
fs.readFile("nodes.csv", "utf8", function(err, data){
if(err) {
console.log("!! error at opening nodes.csv:"+err);
}else{
console.log("* parsing output")
data = data.split("\n");
var output = []
for(var i = 0; i < data.length; i++){
data[i] = data[i].split(";");
output.push(data[i][5])
if(i+1 == data.length){
callback(output);
}
}
}
})
}
});
};
function tryMacs(macs){
var i = 0;
function next(mac){
console.log("* changing MAC-Address...")
exec(command("changeMAC", {interface: interface, MAC: mac}), function(err, stdout, stderr){
console.log("* waiting for connect...")
setTimeout(function(){
console.log("* getting google.com...")
exec("curl google.com", function(err, stdout, stderr){
if(stdout){
console.log("* ,-)")
console.log("* it's working !!!!");
console.log("* using "+mac);
}else{
console.log("* :-(")
console.log("* address not working\n* trying next one")
next(macs[++i]);
}
})
}, 10000)
})
}
next(macs[i])
}
(function(){
if(process.env.SUDO_USER || process.env.USER === "root"){
interfaceChecker(function(){
getNodes(tryMacs);
});
}else{
console.log("* you need to be root to exec this. \n* Try sudo [the command]")
}
})()