-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifconfig.js
498 lines (441 loc) · 14.4 KB
/
wifconfig.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/****************************************************************************
* like including method: ex. eval(ReadFile(".\\this.js"));
***************************************************************************/
function ReadFile(fname) {
var readonly = 1;
var getFileSystemObject = function() {
return WScript.CreateObject("Scripting.FileSystemObject");
}
file_system_object = getFileSystemObject();
if (!file_system_object.fileExists(fname)) {
WScript.Echo("Cannot read file : ", fname);
return "";
}
var rstream, metaobj;
try {
rstream = file_system_object.OpenTextFile(fname, readonly);
metaobj = rstream.ReadAll();
} finally {
rstream.close();
}
return metaobj;
}
/****************************************************************************/
eval(ReadFile(".\\utils.js"));
/*
* my environment are Windows XP and Vista.
*/
var debug = false;
function IPAddress(addr) {
this.addr = addr;
this.check = function() {
var base_ff = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)';
var period = '\\.';
var hat = '^';
var doller = '$';
var ipaddr_match_str = new RegExp(new Array(hat + base_ff, base_ff, base_ff, base_ff + doller).join(period));
return ipaddr_match_str.test(this.addr);
}
this.toStr = function() {
return this.addr;
}
}
function NetConf(host) {
if (typeof(host) == "undefined") {
host = ".";
}
this.wmisrv = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\" + host + "\\root\\cimv2");
// var locator = WScript.CreateObject("WbemScripting.SWbemLocator");
// this.wmisrv = locator.ConnectServer(host, "\\root\\cimv2", "Administrator");
// this.wmisrv.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy; // ref MSDN
// this.wmisrv.Security_.authenticationLevel = 6;
this.vmnet = /VMnetAdapter/;
this.items = {}; // items[index] = { desc, connect, adapter, config };
this.attachDevices = function () {
var id802_3 = 0x0; // why cannot have const keyword(fuck WSH!)
var adapters = this.wmisrv.InstancesOf("Win32_NetworkAdapter");
for (var adapter = new Enumerator(adapters); !adapter.atEnd(); adapter.moveNext()) {
var adapter_type = adapter.item().AdapterTypeID;
if (adapter_type == id802_3) {
var configs = this.wmisrv.InstancesOf("Win32_NetworkAdapterConfiguration");
for (var config = new Enumerator(configs); !config.atEnd(); config.moveNext()) {
var a = adapter.item();
var c = config.item();
if (
String(a.ServiceName) == String(c.ServiceName)
&& !String(c.ServiceName).match(this.vmnet)
&& c.Index == a.Index
) {
var desc = a.Description;
var index = a.Index;
var is_connected = (0x2 == a.NetConnectionStatus); // connect status = 0x2 ref MSDN
this.items[index] = { "desc" : desc, "connect" : is_connected, "adapter" : a, "config": c };
}
}
}
}
if (debug)
for (i in this.items)
WriteLine(i + " : " + this.items[i]["adapter"].Description);
}
this.printDevices = function() {
WriteLine(fillChars('-', 74));
WriteLine("Index MACAddress Description");
for (dev in this.items) {
var item = this.items[dev];
var mac = item["config"].MACAddress.split(":").join(" ");
var desc = item["adapter"].Description;
var index = item["adapter"].index;
WriteLine("0x" + toHex(index).toUpperCase() + (toHex(index).length == 1 ? " " : "") +
" ..." + mac + " ......" + desc);
}
WriteLine(fillChars('-', 74));
}
this.isIndex = function(asoc_iface) {
if (isFinite(asoc_iface)) {
var index = -1;
var base = "10";
var hexsym = new RegExp("^0x");
if (hexsym.test(asoc_iface))
base = "16";
index = parseInt(asoc_iface, base);
if (this.items[index])
return index;
}
return -1;
}
this.isKeyword = function(asoc_iface) {
if (/^s:/.test(asoc_iface)) {
var ambiguious_word = asoc_iface.substring("s:".length, asoc_iface.length);
var ambiguious_desc = new RegExp(ambiguious_word);
var check_ifaces = new Array();
for (iface_idx in this.items) {
var desc = this.getDescription(iface_idx);
if (ambiguious_desc.test(desc))
check_ifaces.push(this.getItem(iface_idx));
}
if (check_ifaces.length > 1) {
WriteLine("=== These interfaces are ambiguity of information from `" + asoc_iface + "' ===");
for (ambiguious_iface in check_ifaces)
WriteLine(ambiguious_iface + " : " + check_ifaces[ambiguious_iface]["desc"]);
return -1;
} else if (check_ifaces.length == 0) {
WriteLine("no interfaces... from `" + asoc_iface + "'");
return -1;
} else {
var index = check_ifaces[0]["config"].index;
return index;
}
}
return -1;
}
this.getInterfaceIdx = function(asoc_iface) {
var ret = this.isIndex(asoc_iface);
if (ret != -1)
return ret;
ret = this.isKeyword(asoc_iface);
if (ret != -1)
return ret;
// else {
// WriteLine("no matching interface... from `" + asoc_iface + "'");
return ret;
// }
}
this.getItem = function(iface_idx) {
return this.items[iface_idx];
}
this.getDescription = function(iface_idx) {
return this.items[iface_idx]["desc"];
}
this.getConfig = function(iface_idx) {
return this.items[iface_idx]["config"];
}
this.getConfig = function(iface_idx) {
return this.items[iface_idx]["config"];
}
this.getConnected = function(iface_idx) {
return this.items[iface_idx]["connect"];
}
this.getStringOfIface = function(iface_idx, word) {
config = this.getConfig(iface_idx);
return (word + ": (" + this.getDescription(iface_idx) + ") " + config.ServiceName);
}
this.checkDHCP = function(iface_idx, inspectDhcp) {
var config = this.getConfig(iface_idx);
var is_connected = this.getConnected(iface_idx);
if (is_connected == false)
return ("This interface is no-connected. Please check your adapter's connection.");
if (inspectDhcp && config.dhcpEnabled == false) {
return ("This interface is no-dhcp.");
}
return "";
}
this.enableDHCP = function(iface_idx) {
var ret;
var config = this.getConfig(iface_idx);
ret = this.checkDHCP(iface_idx, false);
if (ret != "") {
WriteLine(ret);
return;
}
ret = config.EnableDHCP();
if (ret != 0)
throw ("Cannot enable DHCP : errno(" + String(ret) + ")");
// WScript.Sleep(2000);
ret = config.SetDnsServerSearchOrder();
if (ret != 0 && config.DHCPEnabled == false)
throw ("Cannot enable DNS : errno(" + String(ret) + ")");
WriteLine("Enable " + this.getStringOfIface(iface_idx, "DHCP"));
}
this.releaseDHCP = function(iface_idx) {
var ret;
var config = this.getConfig(iface_idx);
ret = this.checkDHCP(iface_idx, true);
if (ret != "") {
WriteLine(ret);
return;
}
ret = config.ReleaseDHCPLease();
if (ret != 0)
throw ("Cannot release DHCP : errno(" + String(ret) + ")");
WriteLine("Release " + this.getStringOfIface(iface_idx, "DHCP"));
}
this.renewDHCP = function(iface_idx) {
var ret;
var config = this.getConfig(iface_idx);
WScript.Timeout = 5; // care of exceed expected time for RenewDHCPLease() method.
ret = this.checkDHCP(iface_idx, true);
if (ret != "") {
WriteLine(ret);
return;
}
ret = config.RenewDHCPLease();
if (ret != 0)
throw ("Cannot release DHCP : errno(" + String(ret) + ")");
WriteLine("Renew " + this.getStringOfIface(iface_idx, "DHCP"));
}
this.enableStatic = function(iface_idx, ipaddr, subnet) {
var ret;
var config = this.getConfig(iface_idx);
ret = this.checkDHCP(iface_idx, true);
if (ret == "") {
ret = config.ReleaseDHCPLease();
WriteLine("Release DHCP address");
if (ret != 0)
throw ("Cannot release DHCP : errno(" + String(ret) + ")");
}
ret = config.EnableStatic(ipaddr, subnet);
if (ret != 0)
throw ("Cannot assign IPaddress and SubnetMask: " + ret);
WriteLine("Static " + this.getStringOfIface(iface_idx, ""));
}
this.setGateways = function(iface_idx, gateways, metric) {
// WriteLine("@@@ setGateways " + typeof(gateways));
var ret;
var config = this.getConfig(iface_idx);
/*
var gate = function () {
var h = new Array(gateways.length);
for (x = 0; x < gateways.length; ++x) {
h[x] = gateways[x];
}
return h;
}
*/
ret = config.SetGateways(gateways, metric);
if (ret != 0) {
throw ("Cannot setup Gateways: " + ret);
}
}
this.setDNSServer = function(iface_idx, dnss) {
var ret;
var config = this.getConfig(iface_idx);
ret = config.SetDnsServerSearchOrder(dnss);
if (ret != 0) {
trheow ("Cannot setup DNS: " + ret);
}
}
}
function Main() {
this.opt_dev = false;
this.opt_help = true;
this.opt_config = false;
this.opt_dhcp = false;
this.opt_static = false;
this.opt_gateways = false;
this.opt_dnss = false;
this.fconfig = "";
this.iface_idx = -1;
this.callbackDHCP = null;
this.default_metric = 100;
this.metric_ary = null;
this.callbackStatic = null;
this.callbackGateway = null;
this.callbackDns = null;
this.devices = null;
this.usage = function() {
WriteLine("Usage: ");
WriteLine("\twifconfig.js interface_index|associated_description dhcp enable|release|renew");
WriteLine("\twifconfig.js interface_index|associated_description static ipaddr netmask addr [gateway addr,...] [dns addr,...] [metric value]");
WriteLine("\twifconfig.js [-h] [-D] [-f configfile]");
}
this.parseOpts = function () {
var args = WScript.Arguments;
if (debug)
WriteLine("@@@ args.len = " + args.length + " @@@");
if (args.length == 3 && args.Item(1) == 'dhcp') { // for checking DHCP method
this.iface_idx = this.devices.getInterfaceIdx(args.Item(0));
if (this.iface_idx == -1)
return -1;
var role = args.Item(2);
this.callbackDHCP = function() { // ugly ........
if (role == 'enable')
this.devices.enableDHCP(this.iface_idx);
else if (role == 'release')
this.devices.releaseDHCP(this.iface_idx);
else if (role == 'renew')
this.devices.renewDHCP(this.iface_idx);
else {
WriteErr('Expceted keywords are enable, release or renew');
}
}
this.opt_dhcp = true;
return 0;
} else if (args.length >= 5 && args.Item(1) == 'static') {
this.iface_idx = this.devices.getInterfaceIdx(args.Item(0));
if (this.iface_idx == -1)
return -1;
var ipaddr = new IPAddress(args.Item(2));
if (ipaddr.check() == false) {
WriteErr('This address is no valid : ' + ipaddr.toStr() );
return -2;
}
if (args.Item(3) != 'netmask') {
WriteErr('Expected keyword is netmask addr');
return -3;
}
var subnet = new IPAddress(args.Item(4));
if (subnet.check() == false) {
WriteErr('This subnet is no valid : ' + subnet.toStr() );
return -4;
}
this.callbackStatic = function() {
this.devices.enableStatic(this.iface_idx, toSafeArray(new Array(ipaddr.toStr())), toSafeArray(new Array(subnet.toStr()))) ;
}
this.opt_static = true;
for (var i = 5; i < args.length; ++i) {
if (args.Item(i) == 'gateway' && args.Item(i+1)) {
var gateways = args.Item(i+1).split(",");
this.metric_ary = new Array();
i++;
for (var j = 0; j < gateways.length; ++j) {
var a_gateway = new IPAddress(gateways[j]);
if (a_gateway.check() == false) {
WriteErr('This gateway address is no valid : ' + a_gateway.toStr());
return -5;
}
this.metric_ary[j] = this.default_metric;
}
this.callbackGateway = function() {
this.devices.setGateways(this.iface_idx, toSafeArray(gateways), toSafeArray(this.metric_ary));
}
this.opt_gateways = true;
} else if (args.Item(i) == 'dns' && args.Item(i+1)) {
var dnss = args.Item(i+1).split(",");
i++;
for (var j = 0; j < dnss.length; ++j) {
dns = new IPAddress(dnss[j]);
if (dns.check() == false) {
WriteErr('This dns address is no valid : ' + dns.toStr());
return -6;
}
}
this.callbackDns = function() {
this.devices.setDNSServer(this.iface_idx, toSafeArray(dnss));
}
this.opt_dnss = true;
} else if (args.Item(i) == 'metric' && args.Item(i+1)) {
var new_metric = args.Item(i+1);
i++;
if (isFinite(new_metric)) {
for ( var j = 0; j < this.metric_ary.length; ++j)
this.metric_ary[j] = parseInt(new_metric, "10");
} else {
WriteErr('This metric is no valid : ' + new_metric);
return -7;
}
} else {
WriteErr("Unknown keyword : " + args.Item(i));
}
}
return 0;
}
for (i = 0; i < args.length; ++i) { // for checking etc...
if (debug)
WriteLine(args.Item(i));
if (args.Item(i) == '-D') {
this.opt_dev = true;
} else if (args.Item(i) == '-h') {
this.opt_help = true;
} else if (args.Item(i) == '-f') {
this.opt_config = true;
this.fconfig = args.Item(++i);
WriteLine(args.Item(i));
} else
return -3;
}
}
this.run = function () {
this.devices = new NetConf();
this.devices.attachDevices();
var ret = this.parseOpts();
if (ret < 0) {
this.usage();
return -1;
}
try {
if (this.opt_dhcp) {
var desc = this.devices.getDescription(this.iface_idx);
WriteLine("--- " + desc + " ----");
this.callbackDHCP();
WriteLine("----" + fillChars('-', desc.length) + "-----");
} else if (this.opt_static) {
var desc = this.devices.getDescription(this.iface_idx);
WriteLine("--- " + desc + " ----");
this.callbackStatic();
if (this.opt_gateways)
this.callbackGateway();
if (this.opt_dnss)
this.callbackDns();
WriteLine("----" + fillChars('-', desc.length) + "-----");
} else if (this.opt_dev) {
this.devices.printDevices();
} else if (this.opt_config) {
// eval
} else if (this.opt_help) {
this.usage();
}
} catch (err) {
WriteLine(err);
}
return 0;
}
}
(new Main()).run();
function test_wifconfig() {
var wifconfig = new NetConf(".");
wifconfig.attachDevices();
wifconfig.getInterfaceIdx("0xb");
wifconfig.getInterfaceIdx("11");
wifconfig.getInterfaceIdx("s:R");
wifconfig.getInterfaceIdx("s:gahahahahahah");
wifconfig.printDevices();
var index = wifconfig.getInterfaceIdx("0x1b");
try {
wifconfig.enableDHCP(index);
// wifconfig.releaseDHCP(index);
// wifconfig.renewDHCP(index);
} catch (err) {
WriteErr(err);
}
}