【问题标题】:python to display all the VMs and the ip address on the hostpython显示主机上的所有虚拟机和IP地址
【发布时间】:2014-11-18 01:43:46
【问题描述】:

有没有办法显示在特定主机上运行的虚拟机的 IP 地址。如何使用 qemu 钩子查看主机中所有已注册的虚拟机。一种可能的方法是嗅探进出主机 NIC 的数据包。但是如何从源IP地址和目的IP地址中过滤出广播IP地址。任何人都可以提出一种可能的方法来实现这一目标。我没有为虚拟机使用静态 IP 地址。 python中的脚本将有很大帮助。甚至一个想法都会受到赞赏..

【问题讨论】:

    标签: python localhost virtual-machine hook qemu


    【解决方案1】:

    嗯,有几种方法可以做到这一点。不过最简单的还是使用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 使用 arpgrep 的组合。

    也有一些方法可以使用libvirt-python 获取这些信息,但它的代码要多得多。 Here's an example of using libvirt to connect to your hypervisor.

    编辑:这里有一些真正未经测试的 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...
    

    【讨论】:

    • 非常感谢用户无法控制他们创建虚拟机的方式。虚拟机可以使用 libvirt 创建,那么 virsh 将是最好的解决方案。但是,如果虚拟机是通过 qemu 或 libvirt 创建的,有没有办法找到一种方法。。libvert 挂钩监视 qemu 中的事件 ..所以如果我想办法通过 qemu 列出所有虚拟机,它将覆盖 libvirt也是。
    • 如何创建 VM 并不重要。 libvirt 将直接连接到虚拟机管理程序,并且可以内省当前定义的 VM,以提取您要查找的数据。
    • 太棒了..!!那么我可以使用 libvirt 列出 VMS 的 IP 地址吗?因为这是我需要做的……
    • 是的,见编辑。它非常原始,需要你稍微玩一下才能让它正确,但要点就在那里。我没有虚拟机主机来测试它,所以上面的代码现在可以工作了。
    • 非常感谢..!!将尝试使用上述方法。将向您更新结果。 :)
    猜你喜欢
    • 2021-10-26
    • 2018-07-25
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    相关资源
    最近更新 更多