我在域名(libvirt中的“虚拟机”)配置文件中设置autoport=yes
,因此在运行时自动分配VNC端口。
我需要获取此端口,以便我可以从外部连接到vm,但我找不到合适的API来执行此操作。更好的python因为我正在使用libvirt-python绑定。
答案 0 :(得分:20)
我还没有为VNC端口找到任何API,不确定更新版本的libvirt是否有这个接口?
但是,您可以使用命令virsh vncdisplay $domainName
来显示端口。 注意:您必须修改/etc/libvirt/qemu.conf
启用vnc_listen='0.0.0.0'
。
答案 1 :(得分:6)
没有API来获取VNC端口。您必须获取并解析XML文件以找出该端口。当然,如果客户端被销毁(断电/离线),该端口的值将为-1。
char * virDomainGetXMLDesc (virDomainPtr domain, unsigned int flags)
<domain>
<devices>
<graphics type='vnc' port='5900' autoport='yes'/>
</devices>
</domain>
答案 2 :(得分:3)
以下是你在python中的表现,万一有人需要这个。
另存为vncport.py
from xml.etree import ElementTree as ET
import sys
import libvirt
conn = libvirt.open()
domain = conn.lookupByName(sys.argv[1])
#get the XML description of the VM
vmXml = domain.XMLDesc(0)
root = ET.fromstring(vmXml)
#get the VNC port
graphics = root.find('./devices/graphics')
port = graphics.get('port')
print port
运行命令
python vncport.py <domain name>
答案 3 :(得分:0)
这是一个PHP版本,如果有人需要这个:
$res = libvirt_domain_lookup_by_name($conn, $domname);
$xmlString = libvirt_domain_get_xml_desc($res, '');
$xml = simplexml_load_string($xmlString);
$json = json_encode($xml);
$data = json_decode($json,TRUE);
$port = intval($data["devices"]["graphics"]["@attributes"]["port"]);