forked from tvigneron/vc-script-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable_maclearning.py
96 lines (78 loc) · 4.11 KB
/
enable_maclearning.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
#!/usr/bin/env python3
# Disclaimer: This product is not supported by VMware. It is a sample script put together to give an example on how to enable MAC learning and Forged transmit in Python.
# License: https://github.com/vmware/pyvmomi-community-samples/blob/master/LICENSE
# Purpose: Enables MAC Learning and Forged Transmit on a given PortGroup
# The script should be used with a CLI command like: python3 <pythonfile> -s <vcenter> -u <user> -p <password> -pg <portgroup>
from pyVim.connect import SmartConnect
import ssl
from pyVmomi import vim
import time
import argparse
#Put relevant informations for your environment:
def parseParameters():
parser = argparse.ArgumentParser(
description='Arguments to connect to vCenter')
parser.add_argument('-s', '--sourcevc',
required = True,
action = 'store',
help = 'Vcenter server name or IP')
parser.add_argument('-u', '--user',
required=True,
action='store',
help='User name to connect to vcenter')
parser.add_argument('-p', '--password',
required=True,
action='store',
help = 'Password for connection to vcenter')
parser.add_argument('-pg', '--portgroup',
required=True,
action='store',
help= 'PortGroup (name) on which to set MAC Learning and Forged Transmit')
args = parser.parse_args()
return args
def connect_vcenter(sourcevc, user, password):
context = ssl._create_unverified_context()
si= SmartConnect(host=str(sourcevc), user=str(user), pwd=password,sslContext=context)
return si
def get_all_objs(content, vimtype):
obj = {}
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for managed_object_ref in container.view:
obj.update({managed_object_ref: managed_object_ref.name})
return obj
def wait_for_task(task, actionName='job', hideResult=False):
while task.info.state == vim.TaskInfo.State.running or str(task.info.state) == "queued":
time.sleep(2)
if task.info.state == vim.TaskInfo.State.success:
output = 'Success.\n The task %s completed successfully.' % actionName
print(output)
else:
output = 'The task %s failed and did not complete successfully: %s' % (actionName, task.info.error)
raise task.info.error
print(output)
return task.info.result
def enable_maclearning_forgedtransmit(si, content, portgroup):
for virtualportgroup in get_all_objs(content, [vim.dvs.DistributedVirtualPortgroup]):
if virtualportgroup.name == portgroup:
spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec()
spec.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy()
spec.configVersion = virtualportgroup.config.configVersion
spec.defaultPortConfig.macManagementPolicy= vim.dvs.VmwareDistributedVirtualSwitch.MacManagementPolicy()
spec.defaultPortConfig.macManagementPolicy.macLearningPolicy= virtualportgroup.config.defaultPortConfig.macManagementPolicy.macLearningPolicy
spec.defaultPortConfig.macManagementPolicy.inherited = False
spec.defaultPortConfig.macManagementPolicy.macLearningPolicy.enabled = True
spec.defaultPortConfig.macManagementPolicy.macLearningPolicy.inherited = False
spec.defaultPortConfig.macManagementPolicy.macLearningPolicy.limit = 4096
spec.defaultPortConfig.macManagementPolicy.macLearningPolicy.limitPolicy = "DROP"
spec.defaultPortConfig.macManagementPolicy.forgedTransmits = True
task = virtualportgroup.ReconfigureDVPortgroup_Task(spec = spec)
wait_for_task(task, si)
if __name__ == "__main__":
#Get user input from CLI
args = parseParameters()
print(args.sourcevc, args.user, args.password, args.portgroup)
#Initiate a session to vCenter
si = connect_vcenter(args.sourcevc, args.user, args.password)
content = si.content
#Enable Mac learning and forged transmit
enable_maclearning_forgedtransmit(si, content, args.portgroup)