我遵循了基本说明/阅读了示例
- https://code.vmware.com/docs/12016/vmware-vcenter-server-management-programming-guide/GUID-EE3B04C5-3968-41FB-82BB-0908FE08781D.html
- https://github.com/vmware/vsphere-automation-sdk-python
- https://communities.vmware.com/thread/608909
- https://vthinkbeyondvm.com/getting-started-with-vcenter-server-rest-apis-using-python/
我读过openstack rest API 启动/停止服务器
但我不知道如何查询 API。我在笔记本电脑上的本地 VMWare VM 上运行 ESXi,Web GUI 运行正常,但 https://server/rest 只是一个空白站点,我也无法通过 Python SDK 查询任何内容(使用 Github 页面上的示例并为其提供 IP 和凭据)
我刚刚开始使用 VMWare API,我真的不明白哪里出了问题。
编辑:使用 Python SDK 时出现以下错误:
com.vmware.vapi.std.errors_client.OperationNotFound: {messages : [LocalizableMessage(id='vapi.provider.interface.unknown', default_message='Unknown interface: com.vmware.cis.session', args=['com.vmware.cis.session'], params=None, localized=None)], data : None, error_type : None}
EDIT2:通过我从 vmware github 获得的这个示例脚本从服务器调用信息也可以正常工作。
#!/usr/bin/env python
"""
Python program for flat text listing the VMs on an
ESX / vCenter, host one per line.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import print_function
import sys
sys.path.insert(0, 'C:/Users/lpeschko/Documents/Python Projects/VMWareSDK/venv/Lib/site-packages')
import atexit
from pyVim.connect import SmartConnectNoSSL, Disconnect
from pyVmomi import vim
from tools import cli
MAX_DEPTH = 10
def setup_args():
"""
Get standard connection arguments
"""
parser = cli.build_arg_parser()
my_args = parser.parse_args()
return cli.prompt_for_password(my_args)
def printvminfo(vm, depth=1):
"""
Print information for a particular virtual machine or recurse into a folder
with depth protection
"""
# if this is a group it will have children. if it does, recurse into them
# and then return
if hasattr(vm, 'childEntity'):
if depth > MAX_DEPTH:
return
vmlist = vm.childEntity
for child in vmlist:
printvminfo(child, depth+1)
return
summary = vm.summary
print(summary.config.name)
def main():
"""
Simple command-line program for listing the virtual machines on a host.
"""
args = setup_args()
si = None
try:
si = SmartConnectNoSSL(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))
atexit.register(Disconnect, si)
except vim.fault.InvalidLogin:
raise SystemExit("Unable to connect to host "
"with supplied credentials.")
content = si.RetrieveContent()
for child in content.rootFolder.childEntity:
if hasattr(child, 'vmFolder'):
datacenter = child
vmfolder = datacenter.vmFolder
vmlist = vmfolder.childEntity
for vm in vmlist:
printvminfo(vm)
# Start program
if __name__ == "__main__":
main()
答案1
您需要查询 vcenter api,据我所知 exsi 不托管 api 端点。
答案2
您真的需要 REST API 吗?我找到了另一个官方库,它不需要 vCenter,而且对我来说很有用:https://github.com/vmware/pyvmomi