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
|
import pycurl
import sys, getopt
from StringIO import StringIO
import json
####################################################################################################################################################################################
# #
# Export all active users to file - for 1602+ #
# #
# activation : #
# python exportUsers.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <output file> | --ofile=<output file> ] #
# #
# shortest activation (be host = localhost, be port = 8080): #
# python exportUsers.py [-f <output file> | --ofile=<output file> ] #
# #
####################################################################################################################################################################################
ALL_USERS_SUFFIX = '/sdc2/rest/v1/user/users'
def errorAndExit(errorCode, errorDesc):
if ( errorCode > 0 ):
print("status=" + str(errorCode) + ". " + errorDesc)
else:
print("status=" + str(errorCode))
sys.exit(errorCode)
def getUsers(scheme, beHost, bePort, adminUser):
try:
buffer = StringIO()
c = pycurl.Curl()
url = scheme + '://' + beHost + ':' + bePort + ALL_USERS_SUFFIX
print(url)
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, buffer.write)
#c.setopt(c.WRITEFUNCTION, lambda x: None)
adminHeader = 'USER_ID: ' + adminUser
c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
if scheme == 'https':
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
res = c.perform()
#print(res)
#print('Status: %d' % c.getinfo(c.RESPONSE_CODE))
c.close()
body = buffer.getvalue()
#print(body)
return (body, None)
except Exception as inst:
print inst
#print type(inst) # the exception instance
#print inst.args # arguments stored in .args
#print inst # __str__ allows args to be printed directly
#x, y = inst.args
#print 'x =', x
return (None, inst)
def usage():
print sys.argv[0], '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <output file> | --ofile=<output file> ]'
def main(argv):
print 'Number of arguments:', len(sys.argv), 'arguments.'
adminHeader = 'jh0003'
beHost = 'localhost'
bePort = '8080'
outputfile = None
scheme = 'http'
try:
opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ofile=","scheme="])
except getopt.GetoptError:
usage()
errorAndExit(2, 'Invalid input')
for opt, arg in opts:
#print opt, arg
if opt == '-h':
usage()
sys.exit(3)
elif opt in ("-i", "--ip"):
beHost = arg
elif opt in ("-p", "--port"):
bePort = arg
elif opt in ("-f", "--ofile"):
outputfile = arg
elif opt in ("-s", "--scheme"):
scheme = arg
print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', output file =',outputfile
if ( outputfile == None ):
usage()
sys.exit(3)
users = getUsers(scheme, beHost, bePort, adminHeader)
error = users[1]
body = users[0]
if ( error != None ):
errorAndExit(5, str(error))
#print body
io = StringIO(body)
usersAsJson = json.load(io)
writeFile = open(outputfile, 'w')
json.dump(usersAsJson, writeFile)
writeFile.close()
print("-------------------------------------------")
errorAndExit(0, None)
if __name__ == "__main__":
main(sys.argv[1:])
|