VMWare REST Api - 使用 PHP 通过 IP 地址获取有关 VM 的信息

问题描述 投票:0回答:2

我正在使用 VMWare REST API (/api/vcenter/host) 查询有关在 vCenter 上注册的 VM 主机的信息。如果我搜索 IP 地址,我想获取虚拟机的名称,反之亦然,通过 IP 地址搜索虚拟机的名称。

https://i.stack.imgur.com/qESNT.png

如果我使用虚拟机名称搜索信息,我已经获得了此信息:

https://i.stack.imgur.com/viXl9.png

但是使用IP地址来获取相同的信息,我无法获取,任何人都可以帮助我吗?

php ip-address
2个回答
0
投票

此信息(纯 IP 地址[es])目前无法从 REST API 获得,尽管它是在不久前计划的。由于当今许多虚拟机都有多个地址(仅考虑 IPv6),并且 API 无法返回混合类型的多个值,因此实现起来并不容易。 您可以使用管理 SDK 或 VMOMI(VMware 托管对象管理接口)之一。 GitHub 上有适用于多种语言的多个 SDK(pyvmomi、govvmomi 和 rbvmomi)来获取地址。

更新:在

https://github.com/vmware-archive/vsphere-automation-sdk-rest/issues/21 找到链接(从 2018 年起)


0
投票

函数 getIpAddress($vm, $server, $id) {

$key = "power_state"; if (isset($vm[$key])) { $vmid = $vm['vm']; // echo $vmid; $url = "https://$server/api/vcenter/vm/$vmid/guest/networking/interfaces"; // echo $url; $ch = curl_init(); // curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $headers = array( 'Accept: application/json', 'Content-Type: application/json', "vmware-api-session-id: $id" ); // echo $id; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); curl_close($ch); if (! $result) { return false; } $teste = json_decode($result, true); echo '<form method="post" action="dvlindex.php">'; echo '<ul>'; echo '<li><h4>Mac Address: ' . $teste[0]['mac_address'] . ' </h4></li>'; echo '<li><h4>IP Address: ' . $teste[0]['ip']['ip_addresses'][0]['ip_address'] . ' </h4></li>'; echo "</form>"; }

}

© www.soinside.com 2019 - 2024. All rights reserved.