disconnecting/connecting NIC to VM

245 views
Skip to first unread message

shi...@ironsrc.com

unread,
May 9, 2013, 4:40:02 AM5/9/13
to psp...@googlegroups.com
Hi,

I have a VM with 2 NIC's.
i want to change the state of one NIC to Connected/Disconnected

In vm.config.hardware.device, i can see the NIC's but i cannot see/change their status.

What would you recommend to do?

Thank you
-Shimon

Matthew Welch

unread,
May 9, 2013, 5:09:05 AM5/9/13
to psp...@googlegroups.com
Look into the VirtualDeviceConnectInfo object. This is where your going to be able to check if a device can be disabled and then actual disable / enable the device. 

Build your spec from there and apply it to your virtual device under 'connectable'.

If you have some sample code your currently working on or an error, could probably help from there. 

-matt

--
You received this message because you are subscribed to the Google Groups "psphere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psphere+u...@googlegroups.com.
To post to this group, send an email to psp...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msg/psphere/-/57Nuzj9damwJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

shi...@ironsrc.com

unread,
May 9, 2013, 5:43:22 AM5/9/13
to psp...@googlegroups.com
Thanks for your fast response
Here is the code i am trying to use

import psphere
import sys
from psphere.client import Client
from psphere.managedobjects import VirtualMachine
from psphere.soap import VimFault
from psphere.errors import ObjectNotFoundError
import types
import time

client = Client("192.168.10.30", "root", "************")
vm = psphere.managedobjects.VirtualMachine.get(client, name="f-prot3")

vmSpec = client.create("VirtualMachineConfigSpec")

for device in vm.config.hardware.device:
    if device.__class__.__name__ == "VirtualPCNet32":
        if device[2][0] == "NAT":
            print device[2][0]
            nicToRemove = client.create("VirtualDeviceConfigSpec")
            nicToRemove.device = device
            nicToRemove.operation = "disconnect"
            vmSpec.deviceChange.append(nicToRemove)


try:
    task = vm.ReconfigVM_Task(spec=vmSpec)
except VimFault, e:
    print("Failed to reconfigure %s: " % e)
    sys.exit()

while task.info.state in ["queued", "running"]:
    print("Waiting 5 more seconds for VM creation")
    time.sleep(5)
    task.update()

client.logout()

------------
ERROR:
No handlers could be found for logger "psphere.config"
NAT
Traceback (most recent call last):
  File "/home/shimon/dev/av/av_update/daily_update.py", line 26, in <module>
    task = vm.ReconfigVM_Task(spec=vmSpec)
  File "/usr/local/lib/python2.7/dist-packages/psphere/__init__.py", line 355, in func
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/psphere/client.py", line 173, in invoke
    result = getattr(self.service, method)(_this=_this, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/suds/client.py", line 542, in __call__
    return client.invoke(args, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/suds/client.py", line 595, in invoke
    soapenv = binding.get_message(self.method, args, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/suds/bindings/binding.py", line 120, in get_message
    content = self.bodycontent(method, args, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/suds/bindings/document.py", line 63, in bodycontent
    p = self.mkparam(method, pd, value)
  File "/usr/local/lib/python2.7/dist-packages/suds/bindings/document.py", line 105, in mkparam
    return Binding.mkparam(self, method, pdef, object)
  File "/usr/local/lib/python2.7/dist-packages/suds/bindings/binding.py", line 287, in mkparam
    return marshaller.process(content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/core.py", line 62, in process
    self.append(document, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/core.py", line 75, in append
    self.appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 102, in append
    appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 243, in append
    Appender.append(self, child, cont)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 182, in append
    self.marshaller.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/core.py", line 75, in append
    self.appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 102, in append
    appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 298, in append
    Appender.append(self, parent, cont)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 182, in append
    self.marshaller.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/core.py", line 75, in append
    self.appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 102, in append
    appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 243, in append
    Appender.append(self, child, cont)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 182, in append
    self.marshaller.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/core.py", line 75, in append
    self.appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 102, in append
    appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 243, in append
    Appender.append(self, child, cont)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 182, in append
    self.marshaller.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/core.py", line 75, in append
    self.appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 102, in append
    appender.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 243, in append
    Appender.append(self, child, cont)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/appender.py", line 182, in append
    self.marshaller.append(parent, content)
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/core.py", line 74, in append
    if self.start(content):
  File "/usr/local/lib/python2.7/dist-packages/suds/mx/literal.py", line 87, in start
    raise TypeNotFound(content.tag)
suds.TypeNotFound: Type not found: 'deviceName'
[Finished in 3.3s with exit code 1]

shi...@ironsrc.com

unread,
May 9, 2013, 7:26:11 AM5/9/13
to psp...@googlegroups.com
i modified the code

but i still get an error on commit

        CODE:
            nicToDisable = client.create("VirtualDeviceConfigSpec")
            nicToDisable.device = device
            nicToDisable.device.connectable.connected = False
            vmSpec.deviceChange.append(nicToDisable)

    Error
Traceback (most recent call last):
  File "/home/shimon/dev/av/av_update/daily_update.py", line 28, in <module>
Process finished with exit code 1

shi...@ironsrc.com

unread,
May 9, 2013, 7:31:34 AM5/9/13
to psp...@googlegroups.com
import psphere
import sys
from psphere.client import Client
from psphere.managedobjects import VirtualMachine
from psphere.soap import VimFault
from psphere.errors import ObjectNotFoundError
import types
import time

client = Client("192.168.10.30", "root", "*******")
vm = psphere.managedobjects.VirtualMachine.get(client, name="f-prot3")

vmSpec = client.create("VirtualMachineConfigSpec")

for device in vm.config.hardware.device:
    if device.__class__.__name__ == "VirtualPCNet32":
        if device[2][0] == "NAT":
            print device[2][0]
            the_nic = device

            nicToDisable = client.create("VirtualDeviceConfigSpec")
            nicToDisable.device = device
            nicToDisable.device.connectable.connected = False
            vmSpec.deviceChange.append(nicToDisable)

print "-" * 60
print "NIC to Disable"
print nicToDisable
print "-" * 60
print "vmSpec"
print vmSpec


try:
    task = vm.ReconfigVM_Task(spec=vmSpec)
except VimFault, e:
    print("Failed to reconfigure %s: " % e)
    sys.exit()

while task.info.state in ["queued", "running"]:
    print("Waiting 5 more seconds for VM creation")
    time.sleep(5)
    task.update()

client.logout()



/usr/bin/python2.7 /home/shimon/dev/av/av_update/daily_update.py
No handlers could be found for logger "psphere.config"
NAT
------------------------------------------------------------
NIC to Disable
(VirtualDeviceConfigSpec){
   dynamicType = None
   dynamicProperty[] = <empty>
   operation = 
      (VirtualDeviceConfigSpecOperation){
         value = None
      }
   fileOperation = 
      (VirtualDeviceConfigSpecFileOperation){
         value = None
      }
   device = 
      (VirtualPCNet32){
         key = 4001
         deviceInfo = 
            (Description){
               label = "Network adapter 2"
               summary = "NAT"
            }
         backing = 
            (VirtualEthernetCardNetworkBackingInfo){
               deviceName = "NAT"
               useAutoDetect = False
               network = <psphere.managedobjects.Network object at 0xba8afd0>
            }
         connectable = 
            (VirtualDeviceConnectInfo){
               startConnected = True
               allowGuestControl = True
               connected = False
               status = "ok"
            }
         controllerKey = 100
         unitNumber = 8
         addressType = "assigned"
         macAddress = "00:50:56:85:e2:64"
         wakeOnLanEnabled = True
      }
 }
------------------------------------------------------------
vmSpec
(VirtualMachineConfigSpec){
   dynamicType = None
   dynamicProperty[] = <empty>
   changeVersion = None
   name = None
   version = None
   uuid = None
   instanceUuid = None
   npivNodeWorldWideName[] = <empty>
   npivPortWorldWideName[] = <empty>
   npivWorldWideNameType = None
   npivDesiredNodeWwns = None
   npivDesiredPortWwns = None
   npivTemporaryDisabled = None
   npivOnNonRdmDisks = None
   npivWorldWideNameOp = None
   locationId = None
   guestId = None
   alternateGuestName = None
   annotation = None
   files = 
      (VirtualMachineFileInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         vmPathName = None
         snapshotDirectory = None
         suspendDirectory = None
         logDirectory = None
      }
   tools = 
      (ToolsConfigInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         toolsVersion = None
         afterPowerOn = None
         afterResume = None
         beforeGuestStandby = None
         beforeGuestShutdown = None
         beforeGuestReboot = None
         toolsUpgradePolicy = None
         pendingCustomization = None
         syncTimeWithHost = None
      }
   flags = 
      (VirtualMachineFlagInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         disableAcceleration = None
         enableLogging = None
         useToe = None
         runWithDebugInfo = None
         monitorType = None
         htSharing = None
         snapshotDisabled = None
         snapshotLocked = None
         diskUuidEnabled = None
         virtualMmuUsage = None
         virtualExecUsage = None
         snapshotPowerOffBehavior = None
         recordReplayEnabled = None
      }
   consolePreferences = 
      (VirtualMachineConsolePreferences){
         dynamicType = None
         dynamicProperty[] = <empty>
         powerOnWhenOpened = None
         enterFullScreenOnPowerOn = None
         closeOnPowerOffOrSuspend = None
      }
   powerOpInfo = 
      (VirtualMachineDefaultPowerOpInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         powerOffType = None
         suspendType = None
         resetType = None
         defaultPowerOffType = None
         defaultSuspendType = None
         defaultResetType = None
         standbyAction = None
      }
   numCPUs = None
   memoryMB = None
   memoryHotAddEnabled = None
   cpuHotAddEnabled = None
   cpuHotRemoveEnabled = None
   deviceChange[] = 
      (VirtualDeviceConfigSpec){
         dynamicType = None
         dynamicProperty[] = <empty>
         operation = 
            (VirtualDeviceConfigSpecOperation){
               value = None
            }
         fileOperation = 
            (VirtualDeviceConfigSpecFileOperation){
               value = None
            }
         device = 
            (VirtualPCNet32){
               key = 4001
               deviceInfo = 
                  (Description){
                     label = "Network adapter 2"
                     summary = "NAT"
                  }
               backing = 
                  (VirtualEthernetCardNetworkBackingInfo){
                     deviceName = "NAT"
                     useAutoDetect = False
                     network = <psphere.managedobjects.Network object at 0xba8afd0>
                  }
               connectable = 
                  (VirtualDeviceConnectInfo){
                     startConnected = True
                     allowGuestControl = True
                     connected = False
                     status = "ok"
                  }
               controllerKey = 100
               unitNumber = 8
               addressType = "assigned"
               macAddress = "00:50:56:85:e2:64"
               wakeOnLanEnabled = True
            }
      },
   cpuAllocation = 
      (ResourceAllocationInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         reservation = None
         expandableReservation = None
         limit = None
         shares = 
            (SharesInfo){
               dynamicType = None
               dynamicProperty[] = <empty>
               shares = None
               level = 
                  (SharesLevel){
                     value = None
                  }
            }
         overheadLimit = None
      }
   memoryAllocation = 
      (ResourceAllocationInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         reservation = None
         expandableReservation = None
         limit = None
         shares = 
            (SharesInfo){
               dynamicType = None
               dynamicProperty[] = <empty>
               shares = None
               level = 
                  (SharesLevel){
                     value = None
                  }
            }
         overheadLimit = None
      }
   cpuAffinity = 
      (VirtualMachineAffinityInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         affinitySet[] = <empty>
      }
   memoryAffinity = 
      (VirtualMachineAffinityInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         affinitySet[] = <empty>
      }
   networkShaper = 
      (VirtualMachineNetworkShaperInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         enabled = None
         peakBps = None
         averageBps = None
         burstSize = None
      }
   cpuFeatureMask[] = <empty>
   extraConfig[] = <empty>
   swapPlacement = None
   bootOptions = 
      (VirtualMachineBootOptions){
         dynamicType = None
         dynamicProperty[] = <empty>
         bootDelay = None
         enterBIOSSetup = None
         bootRetryEnabled = None
         bootRetryDelay = None
      }
   vAppConfig = 
      (VmConfigSpec){
         dynamicType = None
         dynamicProperty[] = <empty>
         product[] = <empty>
         property[] = <empty>
         ipAssignment = 
            (VAppIPAssignmentInfo){
               dynamicType = None
               dynamicProperty[] = <empty>
               supportedAllocationScheme[] = <empty>
               ipAllocationPolicy = None
               supportedIpProtocol[] = <empty>
               ipProtocol = None
            }
         eula[] = <empty>
         ovfSection[] = <empty>
         ovfEnvironmentTransport[] = <empty>
         installBootRequired = None
         installBootStopDelay = None
      }
   ftInfo = 
      (FaultToleranceConfigInfo){
         dynamicType = None
         dynamicProperty[] = <empty>
         role = None
         instanceUuids[] = <empty>
         configPaths[] = <empty>
      }
   vAppConfigRemoved = None
   vAssertsEnabled = None
   changeTrackingEnabled = None
 }
Traceback (most recent call last):
  File "/home/shimon/dev/av/av_update/daily_update.py", line 35, in <module>

Matthew Welch

unread,
May 9, 2013, 2:43:31 PM5/9/13
to psp...@googlegroups.com
I'll have to get a local lab built tonight to test. I'm not able to debug this from memory, but I will try and find a solution. 

-matt

To view this discussion on the web, visit https://groups.google.com/d/msg/psphere/-/NNtATA4qm1oJ.

Poulp Atine

unread,
May 22, 2013, 3:00:10 AM5/22/13
to psp...@googlegroups.com
Hi,

have you found a solution to this problem ?

I've tried with last psphere release but error is still happenning.

Many thanks.

Shimon Tolts

unread,
May 22, 2013, 4:01:35 AM5/22/13
to psp...@googlegroups.com

No, i insted used a devcon.exe from inside the vm, to disable the network localy

--
You received this message because you are subscribed to a topic in the Google Groups "psphere" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psphere/f8EsWpOMXog/unsubscribe?hl=en-GB.
To unsubscribe from this group and all of its topics, send an email to psphere+u...@googlegroups.com.

To post to this group, send an email to psp...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages