在debian中自动设置VM的主机名作为VM的名称

Kum*_*elJ 1 vmware debian

我想将VM的主机名(它是一个安装了open-vm-tools的debian squeeze系统)自动设置为我在VM Client中设置和查看的VM名称.

我试过了

    ~# vmtoolsd --cmd "info-get guestinfo.name" 2> /etc/hostname
Run Code Online (Sandbox Code Playgroud)

但该命令返回"No value found"

小智 7

我使用VMware的pyVmomi模块在Linux客户机操作系统上使用Python脚本完成了这项工作.

首先,我通过读取系统文件来检索系统UUID /sys/devices/virtual/dmi/id/product_uuid.然后,我根据pyvmomi-community-samples站点中find_by_uuid.py示例,通过UUID在vCenter服务器中搜索虚拟机.另一种选择是通过IP地址进行搜索,这将更加平台无关.pyVmomi模块提供了一种启用此方法的FindByIp()方法.

#!/usr/bin/env python
import atexit
import pyVmomi
from pyVmomi import vim, vmodl
from pyVim.connect import SmartConnect, Disconnect

si = SmartConnect(host='<host>', port='<port>', user='<user>', pwd='<password>')
atexit.register(Disconnect, si)
file = open('/sys/devices/virtual/dmi/id/product_uuid')
uuid = file.read().strip().lower()
file.close()

search_index = si.content.searchIndex
vm = search_index.FindByUuid(None, uuid, True, False)
#Alternatively: vm = search_index.FindByIp(None, <ip_address>, True)
print vm.summary.config.name
Run Code Online (Sandbox Code Playgroud)

获得虚拟机的名称后,可以使用客户机操作系统的命令(例如hostname)执行重命名.