Add NIC properties to VM

2,021 views
Skip to first unread message

maumercado

unread,
Apr 10, 2012, 5:12:48 PM4/10/12
to pysp...@googlegroups.com
Hello Seba,

How do I add the network configuration to a NIC, for example make a NIC to be added to a VM, be Natted, bridged, host only etc?


Thank you

Seba

unread,
Apr 27, 2012, 8:57:54 AM4/27/12
to pysp...@googlegroups.com, maumercado
Hi!

   AFAIK, those NIC connection settings (natted, bridged, host only) are available for VMWare workstation VMs, not ESX/vCenter. In ESX you configure separetely different network objects, and then associate VMs to those networks.

I've written two very similar code snippets, the first reconfigures an existing NIC to use a different networw, the second add a new NIC to the VM

Cheers,

Seba.

##
## SWITCH EXISTING NIC TO A DIFFERENT NETWORK
## (THE ONE IN THE 'label' VARIABLE)
##

from pysphere import *
from pysphere.resources import VimService_services as VI

server = VIServer()
server.connect("server", "user", "pwd")

vm = "Windows XP Home Edition - SP3"

#THE NAME OF THE NWTWORK TO SWITCH THIS VM TO.
#You can get them with the VI Client, from the VM Settings window
#Hardware->Network adapter->Network connection(network label)
label = "networkSystem"


vm_obj = server.get_vm_by_name(vm)
if not vm_obj:
    raise Exception("VM %s not found" % vm)

#Find Virtual Nic device
net_device = None
for dev in vm_obj.properties.config.hardware.device:
    if dev._type in ["VirtualE1000", "VirtualE1000e",
                     "VirtualPCNet32", "VirtualVmxnet"]:
        net_device = dev._obj
        break
if not net_device:
    raise Exception("The vm seems to lack a Virtual Nic")
net_device.Backing.set_element_deviceName(label)

#Invoke ReconfigVM_Task
request = VI.ReconfigVM_TaskRequestMsg()
_this = request.new__this(vm_obj._mor)
_this.set_attribute_type(vm_obj._mor.get_attribute_type())
request.set_element__this(_this)
spec = request.new_spec()
dev_change = spec.new_deviceChange()
dev_change.set_element_device(net_device)
dev_change.set_element_operation("edit")
spec.set_element_deviceChange([dev_change])
request.set_element_spec(spec)
ret = server._proxy.ReconfigVM_Task(request)._returnval

#Wait for the task to finish
task = VITask(ret, server)
status = task.wait_for_state([task.STATE_SUCCESS, task.STATE_ERROR])
if status == task.STATE_SUCCESS:
    print "VM %s successfully reconfigured" % vm

elif status == task.STATE_ERROR:
    print "Error reconfiguring vm: %s" % vm, task.get_error_message()



##
## ADD A NEW NIC TO THE VM 
## (USING THE NETWORK DEFINED IN THE 'label' VARIABLE)
##


from pysphere import *
from pysphere.resources import VimService_services as VI

server = VIServer()
server.connect("server", "user", "pwd")

vm = "Windows XP Home Edition - SP3"

#THE NAME OF THE NWTWORK TO SWITCH THIS VM TO.
#You can get them with the VI Client, from the VM Settings window
#Hardware->Network adapter->Network connection(network label)
label = "VM Network"


vm_obj = server.get_vm_by_name(vm)
if not vm_obj:
    raise Exception("VM %s not found" % vm)


#Invoke ReconfigVM_Task
request = VI.ReconfigVM_TaskRequestMsg()
_this = request.new__this(vm_obj._mor)
_this.set_attribute_type(vm_obj._mor.get_attribute_type())
request.set_element__this(_this)
spec = request.new_spec()
    #add a NIC. the network Name must be set as the device name.
dev_change = spec.new_deviceChange()
dev_change.set_element_operation("add")
nic_ctlr = VI.ns0.VirtualPCNet32_Def("nic_ctlr").pyclass()
nic_backing = VI.ns0.VirtualEthernetCardNetworkBackingInfo_Def("nic_backing").pyclass()
nic_backing.set_element_deviceName(label)
nic_ctlr.set_element_addressType("generated")
nic_ctlr.set_element_backing(nic_backing)
nic_ctlr.set_element_key(4)
dev_change.set_element_device(nic_ctlr)

spec.set_element_deviceChange([dev_change])
request.set_element_spec(spec)
ret = server._proxy.ReconfigVM_Task(request)._returnval

#Wait for the task to finish
task = VITask(ret, server)
status = task.wait_for_state([task.STATE_SUCCESS, task.STATE_ERROR])
if status == task.STATE_SUCCESS:
    print "VM %s successfully reconfigured" % vm

elif status == task.STATE_ERROR:
    print "Error reconfiguring vm: %s" % vm, task.get_error_message()



2012/4/10 maumercado

Adhar Jain

unread,
Oct 5, 2013, 2:09:50 AM10/5/13
to pysp...@googlegroups.com, maumercado
Hi Seba ,

           How do i add a virtual switch to an ESXI Box . Can you share some code snippets for geeting this done ?

Regards,
Adhar
Reply all
Reply to author
Forward
0 new messages