from flask import Flask
from pyVim import connect
from pyVmomi import vim
import html
import re
# details for your ESX host:
host = "192.168.14.251"
user = "root"
password = "nice_try_fucko"
port = 443
# name of (this) machine to be excluded
control_vm = "vulnlab"
def main():
# ignore invalid cert on ESX box
import ssl
_create_unverified_https_context = ssl._create_unverified_context
ssl._create_default_https_context = _create_unverified_https_context
vm_list_l1 = get_vm_list_l1()
vm_list_l2 = get_vm_list_l2()
app = Flask(__name__)
@app.route("/")
def index():
result = '''
Revert Panel
Level 1
Level 2
Level 3
Level 4
'''
return result, 200
@app.route("/reset1/")
def reset(vm_name):
reset_vm_l1(vm_list_l1, vm_name)
return 'OK\n', 200
@app.route("/reset2/")
def reset2(vm_name):
reset_vm_l2(vm_list_l2, vm_name)
return 'OK\n', 200
# start the web server
app.run(host="127.0.0.2", port=80)
def get_vm_list_l1():
# make connection
service_instance = connect.SmartConnect(host=host, user=user, pwd=password, port=int(port))
# create list of machines
content = service_instance.RetrieveContent()
containerView = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
children = containerView.view
vm_list_l1 = {}
for child in children:
summary = child.summary
if not summary.config.name == control_vm:
if "_l1" in summary.config.name:
vm_list_l1[summary.config.name] = summary.config.uuid
connect.Disconnect(service_instance)
return vm_list_l1
def get_vm_list_l2():
# make connection
service_instance = connect.SmartConnect(host=host, user=user, pwd=password, port=int(port))
# create list of machines
content = service_instance.RetrieveContent()
containerView = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
children = containerView.view
vm_list_l2 = {}
for child in children:
summary = child.summary
if not summary.config.name == control_vm:
if "_l2" in summary.config.name:
vm_list_l2[summary.config.name] = summary.config.uuid
connect.Disconnect(service_instance)
return vm_list_l2
def reset_vm_l1(vm_list_l1, target_name):
# make sure target is in list
target_uuid = vm_list_l1[target_name]
if target_uuid:
# make connection
service_instance = connect.SmartConnect(host=host, user=user, pwd=password, port=int(port))
target_vm = service_instance.content.searchIndex.FindByUuid(None, target_uuid, True)
if target_vm:
# print(target_vm.runtime.powerState)
target_vm.ResetVM_Task()
connect.Disconnect(service_instance)
def reset_vm_l2(vm_list_l2, target_name):
# make sure target is in list
target_uuid = vm_list_l2[target_name]
if target_uuid:
# make connection
service_instance = connect.SmartConnect(host=host, user=user, pwd=password, port=int(port))
target_vm = service_instance.content.searchIndex.FindByUuid(None, target_uuid, True)
if target_vm:
# print(target_vm.runtime.powerState)
target_vm.ResetVM_Task()
connect.Disconnect(service_instance)
if __name__ == "__main__":
main()