-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcpUtils.py
executable file
·168 lines (132 loc) · 4.08 KB
/
pcpUtils.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
#!/usr/bin/python
#
# This library performs actions to generate and parse hardware information
# from hardware.cfg file
import shutil
import ConfigParser
import argparse
from Crypto.Cipher import AES
_USING_RSA_KEYS = False
_COMPUTER_LIST = [
"play1",
"cg",
"teleprompt",
"pvp",
"prereel"
]
_PROJECTOR_LIST = [
"rightProjector",
"centerProjector",
"leftProjector",
]
_DEVICE_LIST = [
"archive",
]
_DDR_LIST = [
"kipro",
]
def getComputerList():
"""Returns list of computers."""
return _COMPUTER_LIST
def getProjectorList():
"""Returns list of projectors."""
return _PROJECTOR_LIST
def getDDRList():
"""Returns list of DDR devices."""
return _DDR_LIST
def getDeviceList():
"""Returns list of generic devices."""
return _DEVICE_LIST
def getAllDeviceNames():
"""Returns list of every device within PCP."""
return getComputerList() + getProjectorList() + getDDRList() + getDeviceList()
def _addComputer(config, section):
config.set(section, "ip")
if _USING_RSA_KEYS: config.set(section, "rsaKey")
config.set(section, "username")
config.set(section, "password")
config.set(section, "_shutdown", "OFF")
config.set(section, "_talk", "OFF")
return config
def _addDDR(config, section):
config.set(section, "ip")
config.set(section, "_record", "OFF")
config.set(section, "_stop", "OFF")
config.set(section, "_unmount", "OFF")
return config
def _addDevice(config, section):
config.set(section, "ip")
config.set(section, "_power", "OFF")
return config
def _initHardwareCFG():
# build new config file (use config.read() to read existing)
config = ConfigParser.ConfigParser()
list = getAllDeviceNames()
for device in list: config.add_section(device)
# Computers
list = getComputerList()
for computer in list: config = _addComputer(config, computer)
# DDRs
list = getDDRList()
for DDR in list: config = _addDDR(config, DDR)
# Devices with only IP addresses and power control
list = getProjectorList() + getDeviceList()
for device in list: config = _addDevice(config, device)
# overwrite new hardware.cfg file
with open("./hardware.cfg", "wb") as hwcfg:
config.write(hwcfg)
def _encryptPasswords():
config = ConfigParser.ConfigParser()
config.read("hardware.cfg")
# make sure they are not already encrypted
passwords = []
sections = config.sections()
for section in sections:
items = config.items(section)
for item in items:
if item[0] == "password":
for char in item[1]:
if char.isalpha() == False:
# TODO: make this a real time password
aes = AES.new("this is the pcp password")
ciphertext = aes.encrypt(item[1] + ((32 - len(item[1])) * " "))
config.set(section, "password", ciphertext)
# overwrite new hardware.cfg file
with open("./hardware.cfg", "wb") as hwcfg:
config.write(hwcfg)
def decryptPassword(section):
"""Decrypts passwords from hardware.cfg."""
encrypted = False
config = ConfigParser.ConfigParser()
config.read("hardware.cfg")
aes = AES.new("this is the pcp password")
for char in config.get(section, "password"):
if (char.isalnum() == False):
encrypted = True
if encrypted:
return aes.decrypt(config.get(section, "password")).strip()
else:
return config.get(section, "password")
def getProperty(name, property):
"""Returns value of property from hardware.cfg."""
config = ConfigParser.ConfigParser()
config.read("hardware.cfg")
return config.get(name, property)
def loadOptions(deviceName):
"""Returns dictionary of device functionality."""
config = ConfigParser.ConfigParser()
config.read("hardware.cfg")
options = {}
for option in filter(lambda x: x[0] == "_", config.options(deviceName)):
options[option] = config.get(deviceName, option)
return options
def _main():
# command line argument parser
parser = argparse.ArgumentParser(description = "Manages hardware network data")
parser.add_argument("-i", "--init", action = "store_true", help = "Initializes .cfg file")
parser.add_argument("-e", "--encrypt", action = "store_true", help = "Encrypts passwords")
args = parser.parse_args()
if args.init: _initHardwareCFG()
if args.encrypt: _encryptPasswords()
if __name__ == "__main__":
_main()