-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcert_mgmt.py
65 lines (57 loc) · 1.81 KB
/
cert_mgmt.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
import json
import pickle
import sys
import os
class CertificationManagement(object):
def __init__(self, config):
f = open(config['certFilename'], 'rb')
self.certs = pickle.load(f)
f.close()
self.config = config
def listHostnames(self):
for hostname in self.certs:
print(hostname.decode('iso-8859-1'))
def removeHostname(self):
sys.stdout.write('Input the hostname to be deleted: ')
sys.stdout.flush()
hostname = sys.stdin.readline().strip().lower().encode('iso-8859-1')
del self.certs[hostname]
def store(self):
try:
os.remove(self.config['certFilename'] + '.bak')
except Exception:
pass
try:
os.rename(self.config['certFilename'], self.config['certFilename'] + '.bak')
except Exception:
pass
f = open(self.config['certFilename'], 'wb')
try:
pickle.dump(self.certs, f)
f.flush()
finally:
f.close()
sys.exit()
def quit(self):
sys.stdout.write('Quit without Save? [Y/N]: ')
sys.stdout.flush()
if sys.stdin.readline().strip().upper() != 'Y':
return
sys.exit()
def start(self):
functionMap = {
1 : self.listHostnames,
2 : self.removeHostname,
3 : self.store,
4 : self.quit,
}
while True:
print('''1. List certifications hostname
2. Remove a hostname from certifications
3. Save and Quit
4. Quit without Save''')
sys.stdout.write('Select function: ')
sys.stdout.flush()
functionMap[int(sys.stdin.readline().strip())]()
if __name__ == '__main__':
CertificationManagement(json.loads(open(sys.argv[1], 'r').read())).start()