Python - VMWare vSphere(WEB SDK) - SUDS

发布于 2024-12-11 23:34:42 字数 5057 浏览 0 评论 0原文

我如何通过 Python 通过某种形式的库或通过 SUDS 联系 vSphere(或 VMWare)以获取 vCPU 或特定主机/来宾/虚拟机的数量?

目前我正在尝试:

from suds.client import Client
from suds.sudsobject import Property

client = Client("https://<server>/sdk/vimService?wsdl")
queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo']
print queryCon

这有效,它给了我某种形式的输出:

(Method){
   name = "QueryConnectionInfo"
   location = "https://localhost/sdk/vimService"
   binding =
      (binding){
         input = <suds.bindings.document.Document instance at 0x0775C080>
         output = <suds.bindings.document.Document instance at 0x0775C080>
      }
   soap =
      (soap){
         action = ""urn:vim25/4.1""
         style = "document"
         input =
            (Input){
               body =
                  (Body){
                     parts[] =
                        (Part){
                           root = <part name="parameters" element="vim25:QueryConnectionInfo"/>
                           name = "parameters"
                           qname[] =
                              "parameters",
                              "urn:vim25",
                           element = "(u'QueryConnectionInfo', u'urn:vim25')"
                           type = "None"
                        },
                     use = "literal"
                     namespace[] =
                        "vim25",
                        "urn:vim25",
                     wrapped = True
                  }
               headers[] = <empty>
            }
         output =
            (Output){
               body =
                  (Body){
                     parts[] =
                        (Part){
                           root = <part name="parameters" element="vim25:QueryConnectionInfoResponse"/>
                           name = "parameters"
                           qname[] =
                              "parameters",
                              "urn:vim25",
                           element = "(u'QueryConnectionInfoResponse', u'urn:vim25')"
                           type = "None"
                        },
                     use = "literal"
                     namespace[] =
                        "vim25",
                        "urn:vim25",
                     wrapped = True
                  }
               headers[] = <empty>
            }
         faults[] =
            (Fault){
               name = "InvalidLoginFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:InvalidLoginFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'InvalidLoginFault', u'urn:vim25')"
                     type = "None"
                  },
            },
            (Fault){
               name = "HostConnectFaultFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:HostConnectFaultFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'HostConnectFaultFault', u'urn:vim25')"
                     type = "None"
                  },
            },
            (Fault){
               name = "RuntimeFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:RuntimeFaultFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'RuntimeFaultFault', u'urn:vim25')"
                     type = "None"
                  },
            },
      }
 }

我尝试遵循以下“指南”:

SUDS - 对方法和类型的编程访问

http://www.vmware.com/支持/开发者/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#field_detail

http://communities.vmware.com/thread/273616

我知道所有信息可能在这里,我只是看不到整个图片:/

尝试了一段时间后,这就是我陷入困境的地方:

client = Client("https://<server>/sdk/vimService?wsdl")
#queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo']
print client.service.QueryConnectionInfo("https://<server>/sdk", None, r'domain\user', 'Password')

输出是:

urllib2.URLError: <urlopen error [Errno 1] _ssl.c:490: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol>

How do i contact the vSphere (or VMWare) form Python either with some form of library or via SUDS to get the number of number of vCPU or a specific host/guest/virtual-machine?

Currently i'm trying:

from suds.client import Client
from suds.sudsobject import Property

client = Client("https://<server>/sdk/vimService?wsdl")
queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo']
print queryCon

And that works, and it gives me some form of output:

(Method){
   name = "QueryConnectionInfo"
   location = "https://localhost/sdk/vimService"
   binding =
      (binding){
         input = <suds.bindings.document.Document instance at 0x0775C080>
         output = <suds.bindings.document.Document instance at 0x0775C080>
      }
   soap =
      (soap){
         action = ""urn:vim25/4.1""
         style = "document"
         input =
            (Input){
               body =
                  (Body){
                     parts[] =
                        (Part){
                           root = <part name="parameters" element="vim25:QueryConnectionInfo"/>
                           name = "parameters"
                           qname[] =
                              "parameters",
                              "urn:vim25",
                           element = "(u'QueryConnectionInfo', u'urn:vim25')"
                           type = "None"
                        },
                     use = "literal"
                     namespace[] =
                        "vim25",
                        "urn:vim25",
                     wrapped = True
                  }
               headers[] = <empty>
            }
         output =
            (Output){
               body =
                  (Body){
                     parts[] =
                        (Part){
                           root = <part name="parameters" element="vim25:QueryConnectionInfoResponse"/>
                           name = "parameters"
                           qname[] =
                              "parameters",
                              "urn:vim25",
                           element = "(u'QueryConnectionInfoResponse', u'urn:vim25')"
                           type = "None"
                        },
                     use = "literal"
                     namespace[] =
                        "vim25",
                        "urn:vim25",
                     wrapped = True
                  }
               headers[] = <empty>
            }
         faults[] =
            (Fault){
               name = "InvalidLoginFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:InvalidLoginFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'InvalidLoginFault', u'urn:vim25')"
                     type = "None"
                  },
            },
            (Fault){
               name = "HostConnectFaultFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:HostConnectFaultFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'HostConnectFaultFault', u'urn:vim25')"
                     type = "None"
                  },
            },
            (Fault){
               name = "RuntimeFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:RuntimeFaultFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'RuntimeFaultFault', u'urn:vim25')"
                     type = "None"
                  },
            },
      }
 }

I've tried following the following "guides":

SUDS - programmatic access to methods and types

http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#field_detail

http://communities.vmware.com/thread/273616

I know that all the information probably is here, i just can't see the entire picture :/

After a while of trying this is where i get stuck:

client = Client("https://<server>/sdk/vimService?wsdl")
#queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo']
print client.service.QueryConnectionInfo("https://<server>/sdk", None, r'domain\user', 'Password')

And the output is:

urllib2.URLError: <urlopen error [Errno 1] _ssl.c:490: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉墨 2024-12-18 23:34:42

您可能想尝试 psphere,这是一个 Python 库(基于 suds),它可以让您访问整个 vSphere网络服务 SDK。

安装它:

$ pip install psphere

查找虚拟机并打印它拥有的 CPU 数量:

>>> from psphere.client import Client
>>> from psphere.managedobjects import VirtualMachine
>>> client = Client(server="vcenter.mydomain.com", username="Administrator", password="strong")
>>> vm = VirtualMachine.get(client, name="genesis")
>>> vm
<psphere.managedobjects.VirtualMachine object at 0xd3fbccc>
>>> print("%s has %s CPUs" % (vm.name, vm.config.hardware.numCPU))
genesis has 2 CPUs

您可以在文档。

免责声明:我是psphere的作者。

You might want to try psphere, a Python library (based on suds) which will give you access the entire vSphere Web Services SDK.

Install it:

$ pip install psphere

Find a virtual machine and print the number of CPUs it has:

>>> from psphere.client import Client
>>> from psphere.managedobjects import VirtualMachine
>>> client = Client(server="vcenter.mydomain.com", username="Administrator", password="strong")
>>> vm = VirtualMachine.get(client, name="genesis")
>>> vm
<psphere.managedobjects.VirtualMachine object at 0xd3fbccc>
>>> print("%s has %s CPUs" % (vm.name, vm.config.hardware.numCPU))
genesis has 2 CPUs

You can find more examples in the documentation.

Disclaimer: I am the author of psphere.

许仙没带伞 2024-12-18 23:34:42

使用新库解决了这个问题:pysphere
(easy_install pysphere)

from pysphere import VIServer
server = VIServer()
server.connect("server", 'user', "pass")
vm = server.get_vm_by_name("virtual_host_name")
info = vm.get_properties()

它保持 vSphere 变量的命名空间完整,并且它像我需要的那样透明,这意味着它不会改变任何内容,这使得它很容易找到,它还支持批处理信息,所以我没有要在一个缓慢而痛苦的过程中逐一查询所有 1500 台服务器,需要几秒钟才能收集有关任何主机的信息。

Solved it with a new library: pysphere
(easy_install pysphere)

from pysphere import VIServer
server = VIServer()
server.connect("server", 'user', "pass")
vm = server.get_vm_by_name("virtual_host_name")
info = vm.get_properties()

It keeps the namespace intact of the vSphere variables and it's as transparent as i need it to be, meaning it doesn't change anything which makes it easy to find, it also support batching up information so i don't have to query through all 1500 servers one by one in a slow painful process, it takes a few seconds to gather information regarding any host.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文