-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpnmagic-as.py
executable file
·746 lines (650 loc) · 30.7 KB
/
vpnmagic-as.py
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#!/usr/bin/env python
#Bugs
#--create group doesn't work
import struct, socket
import getopt, sys, os
import subprocess
import ast
import re
def ip2int(ip):
val = lambda ipstr: struct.unpack('!I', socket.inet_aton(ipstr))[0]
return val(ip)
def int2ip(num):
val = lambda n: socket.inet_ntoa(struct.pack('!I', n))
return val(num)
def valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError:
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
except socket.error: # not a valid address
return False
return True
def valid_patt(patt,item):
match = patt.search(item)
if match:
return True
else:
return False
def checkip(seqip, iplist):
for ip in iplist:
if ip == seqip:
return True
def checknetmask(netmask):
if netmask >= 1 and netmask <= 32:
return True
else:
return False
def incrementasset(platform):
assets = listasset(platform)
if assets:
assetlist = []
for i in assets:
assetlist.append(i.split('-')[2].zfill(6))
assettag = int(max(assetlist)) + 1
else:
if platform in 'SC':
assettag = 100 #SC (Server Class G1) devices already exist in the 001 -006 space
elif platform in 'LH':
assettag = 2500 #LH (Lighthouse G1) devices already exist in the 002000 - 002300 space
elif platform in 'VLH':
assettag = 2500 #LH (Virtualized G2 Lighthouse hypervisor)
elif platform in 'BE':
assettag = 10000 #G2 Base Engine (G2 Lighthouse unconfigured)
elif platform in 'CE':
assettag = 10000 #Cloud Engine (G1 Cloud device)
elif platform in 'PE':
assettag = 30000 #Physical Engine (G2 Lightouse)
elif platform in 'VE':
assettag = 40000 #Virtual Engines (G2 Virtual device on Xenserver, ESXi, Hyper-V)
elif platform in 'ADM':
assettag = 9000 #Admin accounts
return str(assettag).zfill(6)
def incrementclientip():
netparts = []
iplist = []
startip = 2
#p = subprocess.Popen(['/usr/local/openvpn_as/scripts/confdba', '-s'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#tmpdba, err = p.communicate()
#confdba = ast.literal_eval(tmpdba)
p = subprocess.Popen(['cat', '/root/bin/confdbas.txt'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
tmpdba, err = p.communicate()
confdba = ast.literal_eval(tmpdba)
for key1,val1 in confdba.items():
if key1 == 'Default':
if isinstance(val1, dict):
for key2,val2 in val1.items():
if key2 == 'vpn.server.static.0.network':
network = val1.get('vpn.server.static.0.network', None)
netmask = val1.get('vpn.server.static.0.netmask_bits', None)
startipdec = ip2int(network) #decimal form of the network address
maxclients = pow(2,(32 - int(netmask))) - startip #decimal form of the number of hosts with given mask
print maxclients
minipdec = startipdec + startip + 8192 #lowest ip in decimal format
maxipdec = startipdec + maxclients + startip - 2 + 8192 #highest ip in decimal format
minip = int2ip(minipdec) #lowest ip in dotted notation
maxip = int2ip(maxipdec) #highest ip in dotted notation
#p = subprocess.Popen(['/usr/local/openvpn_as/scripts/confdba', '-us'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#tmpdba, err = p.communicate()
#userdba = ast.literal_eval(tmpdba)
p = subprocess.Popen(['cat', '/root/bin/confdbaus.txt'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
tmpdba, err = p.communicate()
userdba = ast.literal_eval(tmpdba)
for key1,val1 in userdba.items():
if isinstance(val1, dict):
for key2,val2 in val1.items():
usertype = val1.get(key2, val2)
if usertype == 'user_connect' or usertype == 'user_compile':
userip = val1.get('conn_ip', None)
if userip:
try:
socket.inet_aton(userip) #Makes sure the groups are in IPv4 network form for sorted below
iplist.append(userip)
except:
pass
for seqdec in range(minipdec, maxipdec):
seqip = int2ip(seqdec)
if checkip(seqip, iplist):
pass
else:
newclientip = seqip
break
print newclientip
return newclientip
def incrementipgroup(group):
p = subprocess.Popen(['/usr/local/openvpn_as/scripts/confdba', '-us'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
confdba, err = p.communicate()
userdba = ast.literal_eval(confdba)
#p = subprocess.Popen(['cat', '/root/bin/confdbaus.txt'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#tmpdba, err = p.communicate()
#userdba = ast.literal_eval(tmpdba)
netparts = []
iplist = []
startip = 2
# Assumes all subnets are assigned by group. Could foul it up if assigned to user.
for key1,val1 in userdba.items():
if key1 == group:
if isinstance(val1, dict):
for key2,val2 in val1.items():
usertype = val1.get(key2, val2)
if usertype == 'group':
usernet = val1.get('group_subnets.0', None)
netparts = usernet.split('/')
if not netparts:
return False
startipdec = ip2int(netparts[0]) #decimal form of the network address
maxclients = pow(2,(32 - int(netparts[1]))) - startip #decimal form of the number of hosts with given mask
minipdec = startipdec + startip #lowest ip in decimal format
maxipdec = startipdec + maxclients + startip - 2 #highest ip in decimal format
minip = int2ip(minipdec) #lowest ip in dotted notation
maxip = int2ip(maxipdec) #highest ip in dotted notation
for key1,val1 in userdba.items():
if isinstance(val1, dict):
for key2,val2 in val1.items():
usertype = val1.get(key2, val2)
if usertype == 'user_connect' or usertype == 'user_compile':
userip = val1.get('conn_ip', None)
if userip:
try:
socket.inet_aton(userip) #Makes sure the groups are in IPv4 network form for sorted below
iplist.append(userip)
except:
pass
for seqdec in range(minipdec, maxipdec):
seqip = int2ip(seqdec)
if checkip(seqip, iplist):
pass
else:
newclientip = seqip
break
return newclientip
def listgroup(verbose):
p = subprocess.Popen(['/usr/local/openvpn_as/scripts/confdba', '-us'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
confdba, err = p.communicate()
userdba = ast.literal_eval(confdba)
itemlist = []
netlist = []
alllist = []
for key1,val1 in userdba.items():
if isinstance(val1, dict):
groupname = key1
if groupname is not 'Dev':
for key2,val2 in val1.items():
usertype = val1.get(key2, val2)
if usertype == 'group':
usernet = val1.get('group_subnets.0', None)
try:
socket.inet_aton(groupname) #Makes sure the groups are in IPv4 network form for sorted below
netlist.append(groupname)
except:
itemlist.append(groupname)
alllist = sorted(netlist, key=lambda x:tuple(map(int, x.split('.')))) + sorted(itemlist)
return alllist
def listclient(option, showitem):
p = subprocess.Popen(['/usr/local/openvpn_as/scripts/confdba', '-us'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
confdba, err = p.communicate()
userdba = ast.literal_eval(confdba)
clientlist = []
for key1,val1 in userdba.items():
if isinstance(val1, dict):
username = key1
#"group_subnets.0": "172.17.0.0/16",
for key2,val2 in val1.items():
usertype = val1.get(key2, val2)
if usertype == 'user_connect' or usertype == 'user_compile':
patt = re.compile(r'CFS-%s-[0-9]{4,6}' % showitem)
match = patt.search(username)
if option == 'all':
if showitem:
patt2 = re.compile(r'.*%s.*' % showitem)
match = patt2.search(username)
if match:
usergroup = val1.get('conn_group', None)
if(str(usergroup) == "None"):
usergroup = "--"
userip = val1.get('conn_ip', None)
if(str(userip) == "None"):
userip = "--"
usernet = val1.get('group_subnets.0', None)
if(str(usernet) == "None"):
usernet = "--"
for key3,val3 in userdba.items():
if key3 == usergroup:
usernet = val3.get('group_subnets.0', None)
if(str(usernet) == "None"):
usernet = "--"
superuser = val1.get('prop_superuser', None)
if not (str(superuser) == "true"):
line = username + "," + usergroup + "," + usernet + "," + userip
clientlist.append(line)
else:
usergroup = val1.get('conn_group', None)
if(str(usergroup) == "None"):
usergroup = "--"
userip = val1.get('conn_ip', None)
if(str(userip) == "None"):
userip = "--"
usernet = val1.get('group_subnets.0', None)
if(str(usernet) == "None"):
usernet = "--"
for key3,val3 in userdba.items():
if key3 == usergroup:
usernet = val3.get('group_subnets.0', None)
if(str(usernet) == "None"):
usernet = "--"
superuser = val1.get('prop_superuser', None)
if not (str(superuser) == "true"):
line = username + "," + usergroup + "," + usernet + "," + userip
clientlist.append(line)
elif option == 'name':
superuser = val1.get('prop_superuser', None)
if not (str(superuser) == "true"):
if showitem:
patt = re.compile(r'.*%s.*' % showitem)
match = patt.search(username)
if match:
clientlist.append(username)
else:
clientlist.append(username)
elif option == 'ips':
superuser = val1.get('prop_superuser', None)
if not (str(superuser) == "true"):
if showitem:
patt = re.compile(r'.*%s.*' % showitem)
match = patt.search(username)
if match:
userip = val1.get('conn_ip', None)
if userip:
try:
socket.inet_aton(userip) #Makes sure the groups are in IPv4 network form for sorted below
clientlist.append(userip)
except:
pass
else:
userip = val1.get('conn_ip', None)
if userip:
try:
socket.inet_aton(userip) #Makes sure the groups are in IPv4 network form for sorted below
clientlist.append(userip)
except:
pass
elif option == 'groups':
if showitem:
if username == showitem:
usergroup = val1.get('conn_group', None)
clientlist.append(usergroup)
else:
pass
else:
userip = val1.get('conn_ip', None)
usergroup = val1.get('conn_group', None)
clientlist.append(usergroup)
elif option == 'networks':
if showitem:
patt = re.compile(r'.*%s.*' % showitem)
match = patt.search(username)
if match:
usergroup = val1.get('conn_group', None)
usernet = val1.get('group_subnets.0', None)
if(str(usernet) == "None"):
for key3,val3 in userdba.items():
if key3 == usergroup:
usernet = val3.get('group_subnets.0', None)
if usernet:
clientlist.append(usernet)
else:
usergroup = val1.get('conn_group', None)
usernet = val1.get('group_subnets.0', None)
if(str(usernet) == "None"):
for key3,val3 in userdba.items():
if key3 == usergroup:
usernet = val3.get('group_subnets.0', None)
if usernet:
clientlist.append(usernet)
elif match:
userip = val1.get('conn_ip', None)
if(str(userip) == "None"):
userip = "--"
line = userip
clientlist.append(line)
if option == 'ips':
return sorted(clientlist, key=lambda x:tuple(map(int, x.split('.'))))
else:
return sorted(set(clientlist))
def listasset(platform):
users = listclient('name', platform)
matchlist = []
patt = re.compile(r'CFS-%s-[0-9]{4,6}' % platform)
for user in users:
match = patt.search(user)
if match:
matchline = match.group()
matchlist.append(matchline)
return sorted(matchlist)
def moveclient(client, group):
patt = re.compile(r'CFS-(LH|VLH|SC|CE|PE|VE|BE)-[0-9]{4,6}')
if valid_patt(patt,client):
name = client
grouplist = listgroup('verbose')
if not searchlist(group, grouplist):
print "Please enter a valid Group"
sys.exit(2)
clientip = incrementipgroup(group)
if not clientip:
print "Error: Unable to increment IP"
sys.exit(2)
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key conn_group --value ' + group + ' UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key conn_ip --value ' + clientip +' UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
def searchlist(item, itemlist):
for i in itemlist:
if item == i:
return True
return False
def greplist(patt,itemlist):
""" finds patt in itemlist - patt is a compiled regex
returns all lines that match patt """
for i in itemlist:
match = patt.search(i)
if match:
return True
return False
def migrateclient(name, group):
clientip = incrementipgroup(group)
if not clientip:
print "Error: Unable to increment IP"
sys.exit(2)
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key prop_autologin --value true UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key conn_group --value ' + group +' UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key pvt_password_digest --value 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key conn_ip --value ' + clientip +' UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key access_from.0 --value' +'+ALL_S2C_SUBNETS'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
return name
def createclient(name, group):
patt = re.compile(r'CFS-(LH|VLH|SC|CE|PE|VE|BE|ADM)-[0-9]{4,6}')
if valid_patt(patt,name):
name = name
else:
print "Please enter a valid Asset Name"
sys.exit(2)
patt = re.compile(r'^(LH|VLH|SC|CE|PE|VE|BE|ADM)$')
if valid_patt(patt,name):
name = "CFS-%s-%s" % (name,incrementasset(name))
grouplist = listgroup('verbose')
if not searchlist(group, grouplist):
print "Please enter a valid Group"
sys.exit(2)
clientip = incrementipgroup(group)
if not clientip:
print "Error: Unable to increment IP"
sys.exit(2)
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key prop_autologin --value true UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key conn_group --value ' + group +' UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key pvt_password_digest --value 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + name + ' --key conn_ip --value ' + clientip +' UserPropPut'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
return name
def creategroup(name,network):
grouplist = listgroup('verbose')
if searchlist(name, grouplist):
print "Group %s already exists" % name
sys.exit(2)
if checknetmask(network):
print "Please enter valid network"
sys.exit(2)
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s --key group_declare --value true UserPropPut' % name
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s --key c2s_dest_s --value false UserPropPut' % name
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s --key c2s_dest_v --value false UserPropPut' % name
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s --key prop_autologin --value true UserPropPut' % name
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s --key prop_deny --value false UserPropPut' % name
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s --key prop_superuser --value false UserPropPut' % name
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
count = 0
netstartip = ip2int(network)
for hostip in serverips:
sip = netstartip + hostip
natip = int2ip(sip)
srvip = str(servernet) + str(hostip)
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s --key access_to.%s --value "+SUBNET:%s(%s)" UserPropPut' % (name,count,srvip,natip)
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
#cmd = '/sbin/iptables -A PREROUTING -d %s/32 -p tcp -j DNAT --to-destination %s' % (natip,srvip)
#p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#output, err = p.communicate()
count += 1
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s --key group_subnets.0 --value %s/%s UserPropPut' % (name,network,subnet)
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
def deleteclient(name):
cmd = '/usr/local/openvpn_as/scripts/sacli --user %s UserPropDelAll' % name
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
def outputclient(client, attribute):
if attribute in 'config':
cmd = '/usr/local/openvpn_as/scripts/sacli --user ' + client + ' GetAutoLogin'
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()
return output
elif attribute in 'ip':
tmpout = listclient('ips', client)
return '\n'.join(map(str, tmpout))
elif attribute in 'group':
tmpout = listclient('groups', client)
return '\n'.join(map(str, tmpout))
elif attribute in 'network':
tmpout = listclient('networks', client)
return '\n'.join(map(str, tmpout))
def usage():
progname = os.path.basename(sys.argv[0])
print ""
print "%s arguments:" % progname
print "-h, --help Show this help message and exit"
print "-l, --list clients List clients"
print "-l, --list clients -p <platform> List clients for <platform>"
print "-l, --list groups List groups"
print "-l, --list all List all client data in CSV format"
print "-l, --list all -p <platform> List all client data in CSV format for <platform>"
print "-l, --list ips List client IP addresses"
print "-l, --list ips -p <platform> List client IP addresses for <platform>"
print "-l, --list networks List network addresses"
print "-l, --list networks -p <platform> List network addresses for <platform>"
print "-o, --output <client> -a <attribute> Output client attribute - config, ip, group"
print "-m, --migrate <client> -g <group> Migrate client to OpenVPN AS"
print "-c --create client -n <name> -g <group> Create new client"
print "-c --create client -p <platform> -g <group> Create new client with auto created name"
print "-c --create group -n <name> -s <network/bits> Create new group"
print "-d, --delete <client|group> -n <name> Delete client or group"
print ""
print "Filters:"
print "-s --subnet <network/bits> Network address for client (eg. 24)"
print "-n, --name <client|group> Name of client or group"
print "-g, --group <group name> Name of group"
print "-p, --platform <platform name> Name of the client platform (eg. CE, PE, SC)"
print ""
print "Examples:"
print "Create a new client:"
print " %s --create=client --platform=CE --group=10.0.50.0" % progname
print "Create a new client (short format):"
print " %s -c client -p CE -g 10.0.50.0" % progname
print "Create a new group:"
print " %s --create=group --name=10.0.50.0 --subnet=24" % progname
print "Delete a client:"
print " %s --delete=CFS-CE-123456" % progname
print "Delete a group:"
print " %s --delete=10.0.50.0" % progname
print "Move a client:"
print " %s --move=CFS-CE-123456 --group=10.0.50.0" % progname
print ""
def main():
network = None
platform = None
name = None
attribute = None
verbose = False
group = None
global servernet
servernet = '192.168.1.'
global serverips
serverips = [3,20,25,26]
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "hl:c:s:n:g:d:p:o:m:a:", ["help","list=","create=","subnet=","name=","group=","delete=","platform=","output=","migrate=","attribute="])
if not opts:
usage()
sys.exit(2)
except getopt.GetoptError as err:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-m", "--migrate"):
client = arg
operation = 'migrate'
elif opt in ("-l","--list"):
option = arg
operation = 'list'
elif opt in ("-c","--create"):
itemtype = arg
operation = 'create'
elif opt in ("-s","--subnet"):
network = arg
elif opt in ("-n","--name"):
name = arg
elif opt in ("-p","--platform"):
platform = arg
elif opt in ("-g","--group"):
group = arg
elif opt in ("-a","--attribute"):
attribute = arg
elif opt in ("-d","--delete"):
itemtype = arg
operation = 'delete'
elif opt in ("-o","--output"):
option = arg
operation = 'output'
else:
operation = 'usage'
usage()
sys.exit()
if operation in 'list':
if option in 'ips':
if platform:
itemlist = listclient('ips', platform)
else:
itemlist = listclient('ips', None)
for item in itemlist:
print item
elif option in 'groups':
itemlist = listgroup('verbose')
for item in itemlist:
print item
elif option in 'networks':
itemlist = listclient('networks', None)
for item in itemlist:
print item
elif option in 'clients':
if platform:
itemlist = listclient('name', platform)
else:
itemlist = listclient('name', None)
for item in itemlist:
print item
elif option in 'all':
if platform:
itemlist = listclient('all', platform)
else:
itemlist = listclient('all', None)
for item in itemlist:
print item
else:
print "Unknown Option - %s" % option
sys.exit(2)
elif operation in 'create':
if itemtype in 'client':
if not name or platform:
print "Please provide asset tag or platform identifier"
sys.exit(2)
if not group:
print "Please provide group name"
sys.exit(2)
else:
createclient(name, group)
if platform:
createclient(name, platform)
elif itemtype in 'group':
if not name:
print "Please provide group name"
sys.exit(2)
if not network:
print "Please provide group network"
sys.exit(2)
#creategroup(name,network)
print "Group creation not functional"
else:
print "Please provide item to create (client,group)"
sys.exit(2)
elif operation in 'delete':
if itemtype in 'client':
if not name:
print "Please provide name"
sys.exit(2)
else:
deleteclient(name)
elif itemtype in 'group':
print "Group deletion not functional"
elif operation in 'migrate':
if group:
migrateclient(client, group)
else:
print "Please provide group"
elif operation in 'output':
if option:
client = option
if attribute:
output = outputclient(client,attribute)
print output
else:
print "Please enter an attribute to output - config, ip or group"
sys.exit(2)
else:
print "Please provide client"
sys.exit(1)
if __name__ == "__main__":
main()