forked from coolrepo99/unlocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesxi-config.py
executable file
·86 lines (64 loc) · 1.99 KB
/
esxi-config.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
#!/usr/bin/env python
"""
This is a simple method to modify the hostd XML file
Not using XML on ESXi Python as it does not preserve
formatting or comments.
(This could be sed but cannot find a suitable regex.)
"""
from __future__ import print_function
import sys
def testline(line, test):
sline = line.lstrip()
if sline == test:
return True
else:
return False
def main():
vmsvc = '<vmsvc>\n'
starttag = '<useVmxSandbox>'
endtag = '</useVmxSandbox>'
with open('/etc/vmware/hostd/config.xml', 'r+') as f:
data = f.readlines()
# Search for the relevant XML tags
i = 0
vmsvcindex = 0
sandboxindex = 0
for line in data:
if testline(line, vmsvc):
vmsvcindex = i
if testline(line, starttag):
sandboxindex = i
# print(line, end='')
i += 1
# If vmsvc tag not found then file is probably corrupt
if vmsvcindex is None:
print('ESXi Config - config.xml is corrupt')
return False
# Remove the existing line if prsent
del data[sandboxindex]
# Now add line with correct flag
pad = len(data[vmsvcindex + 1]) - len(data[vmsvcindex + 1].lstrip())
if sys.argv[1] in ['on', 'off']:
pass
if sys.argv[1] == 'off':
print('ESXi Config - useVmxSandbox off')
data.insert(vmsvcindex + 1, (" " * pad) + sandboxoff)
elif sys.argv[1] == 'on':
print('ESXi Config - useVmxSandbox on')
data.insert(vmsvcindex + 1, (" " * pad) + sandboxon)
else:
print('ESXi Config - Incorrect paramter passed')
return False
# Rewrite the config.xml file
f.seek(0)
f.write(''.join(data))
f.truncate()
f.close()
return True
if __name__ == '__main__':
if len(sys.argv) == 1:
sys.exit(1)
if main():
sys.exit(0)
else:
sys.exit(1)