-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimporter.py
More file actions
64 lines (55 loc) · 2.31 KB
/
importer.py
File metadata and controls
64 lines (55 loc) · 2.31 KB
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
import re
from pyVim import connect
from pyVmomi import vmodl, vim
from tools import pchelper
import time
import requests
import ast
from configparser import ConfigParser
requests.packages.urllib3.disable_warnings() #disable ssl warnings
#load variables.ini
config = ConfigParser()
config.read('variables.ini')
#load arrays
vm_array = ast.literal_eval(config.get("ARRAYS", "vm_array"))
delay30Sec = ast.literal_eval(config.get("ARRAYS", "delay30sec"))
delay60Sec = ast.literal_eval(config.get("ARRAYS", "delay60sec"))
#load the rest of the variables
configDict = config._sections['MAIN']
locals().update(configDict)
print(teststring) #test string, doesn't affect anything, is just to check if the config changed
#Host auth
si = connect.SmartConnectNoSSL(host=esxi_host, user=esxi_user, pwd=esxi_pass, port=443) #connect to host
content = si.RetrieveContent()
for guestvm in vm_array:
FN = f'scripts/{guestvm}' #source path
vm_path = f'/root/{guestvm}' #target path
#VM auth
vm = pchelper.get_obj(content, [vim.VirtualMachine], guestvm)
creds = vim.vm.guest.NamePasswordAuthentication(username=vm_user, password=vm_pass) #VM authentication
data_to_send = open(FN, 'rb').read() #file to send
file_attribute = vim.vm.guest.FileManager.FileAttributes()
url = content.guestOperationsManager.fileManager.InitiateFileTransferToGuest(vm, creds, vm_path, file_attribute, len(data_to_send), True)
url = re.sub(r"^https://\*:", f"https://{esxi_host}:", url)
resp = requests.put(url, data=data_to_send, verify=False) #send data
profile_manager = content.guestOperationsManager.processManager
#exec stuff
program_spec = vim.vm.guest.ProcessManager.ProgramSpec(programPath='/bin/chmod', arguments=f'+x {vm_path}; {vm_path}')
res = profile_manager.StartProgramInGuest(vm, creds, program_spec)
print(guestvm)
if guestvm in delay60Sec:
print("Wait 60sec")
time.sleep(60)
elif guestvm in delay30Sec:
print("Wait 30sec")
time.sleep(30)
else:
print("Skip to next host")
#esxi_host = '192.168.100.146'
#esxi_user = 'root'
#esxi_pass = 'P@ssw0rd'
#vm_user = 'root'
#vm_pass = 'toor'
#vm_array = ["R-FW", "L-FW", "OUT-CLI", "L-RTR-A", "L-RTR-B", "L-CLI-A", "L-CLI-B", "L-SRV", "R-RTR", "R-SRV", "R-CLI"]
#delay30Sec = ["R-FW", "L-RTR-A", "L-RTR-B"]
#delay60Sec = ["L-FW"]