python - 使用 pysphere 将新端口组添加到 vmware 虚拟交换机

标签 python vmware

我正在尝试使用 pysphere 自动将新端口组添加到 ESXi 主机。我正在使用以下代码片段:

from pysphere import MORTypes
from pysphere import VIServer, VIProperty
from pysphere.resources import VimService_services as VI 

s = VIServer()
s.connect(vcenter, user, password)

host_system = s.get_hosts().keys()[17]
prop = VIProperty(s, host_system)
propname = prop.configManager._obj.get_element_networkSystem()
vswitch = prop.configManager.networkSystem.networkInfo.vswitch[0]
network_system = VIMor(propname, MORTypes.HostServiceSystem)

def add_port_group(name, vlan_id, vswitch, network_system):
    request = VI.AddPortGroupRequestMsg()
    _this = request.new__this(network_system)
    _this.set_attribute_type(network_system.get_attribute_type())
    request.set_element__this(_this)
    portgrp = request.new_portgrp()
    portgrp.set_element_name(name)
    portgrp.set_element_vlanId(vlan_id)
    portgrp.set_element_vswitchName(vswitch)
    portgrp.set_element_policy(portgrp.new_policy())
    request.set_element_portgrp(portgrp)
    s._proxy.AddPortGroup(request) 

但是,当我尝试运行它时,出现以下错误:

>>> add_port_group(name, vlan_id, vswitch, network_system)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 12, in add_port_group
  File "/usr/lib/python2.6/site-packages/pysphere-0.1.8-    py2.6.egg/pysphere/resources/VimService_services.py", line 4344, in AddPortGroup
    response = self.binding.Receive(AddPortGroupResponseMsg.typecode)
  File "/usr/lib/python2.6/site-packages/pysphere-0.1.8-    py2.6.egg/pysphere/ZSI/client.py", line 545, in Receive
    return _Binding.Receive(self, replytype, **kw)
  File "/usr/lib/python2.6/site-packages/pysphere-0.1.8-    py2.6.egg/pysphere/ZSI/client.py", line 464, in Receive
    raise FaultException(msg)
pysphere.ZSI.FaultException: The object has already been deleted or has not been completely created

我曾尝试为“vswitch”和“network_system”交换不同的值,但没有成功。有没有人尝试用 pysphere 成功地做类似的事情?

我可以通过 Powershell 完成我需要的,这表明这不是 vmware 问题,但我不想在这种特殊情况下使用 Powershell。

最佳答案

我在我们的一个 vSphere 上试过你的代码。 您似乎将对象传递给 set_element_vswitchName 而不是名称。也许这会有所帮助:

vswitch = prop.configManager.networkSystem.networkInfo.vswitch[0].name

关于python - 使用 pysphere 将新端口组添加到 vmware 虚拟交换机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24661015/

相关文章:

linux - 哪个版本的 Linux 内核开始支持 SPC-4 for VMware 6.5 UNMAP

macos - VMWare融合: Change IP of vmnet8

python - 如何在 C++ 中列出 Python 模块的所有函数名称?

python - 何时使用 subprocess.call() 或 subprocess.Popen(),运行 airodump

python - 按日期时间间隔计算记录数

macos - 在云中运行 VMWare 镜像

tomcat - 如何访问在 VMWare 工作站中运行的 tomcat docker 容器?

mysql - WampServer - mysqld.exe 无法启动,因为缺少 MSVCR120.dll

python - 在 Python 中使用 Pandas 组合三个 DataFrame

python - 在同一张图表上将 Pandas DataFrame 绘制为条形图和折线图