简体   繁体   English

python以显示主机上的所有VM和IP地址

[英]python to display all the VMs and the ip address on the host

Is there a way to display the ip address of the VMs running on the particular host. 有没有一种方法可以显示在特定主机上运行的VM的IP地址。 how do I use qemu hooks to see all the registered vm in the host. 如何使用qemu挂钩查看主机中所有已注册的虚拟机。 One possible way is to sniff the packets to and from the NIC of the host . 一种可能的方法是嗅探与主机NIC之间的数据包。 But how to filter the broadcast ip address from the source and destination ip address. 但是如何从源IP地址和目标IP地址过滤广播IP地址。 Can any one suggest a possible way to achieve this. 任何人都可以提出实现这一目标的一种可能方法。 I am not using static ip address for the VMs. 我没有为虚拟机使用静态IP地址。 A script in python will be of great help. python中的脚本会很有帮助。 Or even a idea will be appreciated.. 甚至一个想法将不胜感激。

Well, there's a couple ways you can do this. 好吧,有两种方法可以做到这一点。 However the easiest is to use the virsh command line tool 但是,最简单的方法是使用virsh命令行工具

This is system specific, but on Redhat you can install the libvirt-client package to get /usr/bin/virsh . 这是特定于系统的,但是在Redhat上,您可以安装libvirt-client软件包来获取/usr/bin/virsh

Here's a SO article showing how to map the MAC address of a guest to their IP using a combination of arp and grep . 这是一篇SO文章,展示了如何使用arpgrep组合将访客的MAC地址映射到其IP

There are ways to get some of this information with libvirt-python as well, but it's much more code. 也有一些方法可以通过libvirt-python获取这些信息,但是它的代码更多。 Here's an example of using libvirt to connect to your hypervisor . 这是使用libvirt连接到管理程序的示例

EDIT: Here's some really untested Python, which should give you a start, but will need some modification and playing around with to 100% work (probably) 编辑:这是一些未经测试的Python,应该为您提供一个开始,但需要进行一些修改,并进行100%的工作(可能)

import libvirt  # To connect to the hypervisor
import re
import subprocess


# Connect to your local hypervisor. See https://libvirt.org/uri.html
#    for different URI's where you'd replace `None` with your
#    connection URI (like `qemu://system`)
conn = libvirt.openReadOnly(None)  # Open the hypervisor in read-only mode
# conn = libvirt.open(None)  # Open the default hypervisor in read-write mode (require
if conn == None:
    raise Exception('Failed to open connection to the hypervisor')

try:  # getting a list of all domains (by ID) on the host
    domains = conn.listDomainsID()
except:
    raise Exception('Failed to find any domains')

for domain_id in domains:
    # Open that vm
    this_vm = conn.lookupById(domain_id)
    # Grab the MAC Address from the XML definition
    #     using a regex, which may appear multiple times in the XML
    mac_addresses = re.search(r"<mac address='([A-Z0-9:]+)'", vm.XMLDesc(0)).groups()

    for mac_address in mac_addresses:
        # Now, use subprocess to lookup that macaddress in the
        #      ARP tables of the host.
        process = subprocess.Popen(['/sbin/arp', '-a'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        process.wait()  # Wait for it to finish with the command
        for line in process.stdout:
            if mac_address in line:
                ip_address = re.search(r'(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})', line)
                print 'VM {0} with MAC Address {1} is using IP {2}'.format(
                    vm.name(), mac_address, ip_address.groups(0)[0]
                )
             else:
                # Unknown IP Address from the ARP tables! Handle this somehow...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM