-
Notifications
You must be signed in to change notification settings - Fork 4
/
infectUbuntuVM.py
58 lines (51 loc) · 1.81 KB
/
infectUbuntuVM.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
from __future__ import print_function
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import atexit
import sys
def GetObj(content, vimtype, name):
"""
Return an object by name, if name is None the
first found object is returned
"""
obj = None
container = content.viewManager.CreateContainerView(
content.rootFolder, vimtype, True)
for c in container.view:
if name:
if c.name == name:
obj = c
break
else:
obj = c
break
return obj
def main():
if len(sys.argv) != 3:
print("Usage: infectVM.py <VM_name> <infected_state>")
sys.exit(1)
vmName, infectedVal = sys.argv[1:]
serviceInstance = SmartConnect(host="10.48.58.109",
user="root",
pwd="",
port=443)
atexit.register(Disconnect, serviceInstance)
content = serviceInstance.RetrieveContent()
customFieldMgr = content.customFieldsManager
fields = (obj.name for obj in customFieldMgr.field)
if "Infected" in fields:
print("Found custom attribute 'Infected' in attributes")
else:
print("Adding custom field 'Infected'")
customFieldMgr.AddCustomFieldDef(name = "Infected", moType = vim.VirtualMachine)
print("Looking for {} in vCenter inventory - this may take a few seconds ...".format(vmName))
vm = GetObj(content, [vim.VirtualMachine], vmName)
if vm:
print ("\tfound {0}\n\tsetting value of 'Infected' to {1}".format(vm.name,infectedVal))
vm.setCustomValue(key="Infected", value=infectedVal)
print("\tdone.")
else:
print("\tVM not found in vCenter inventory.")
# Main section
if __name__ == "__main__":
sys.exit(main())