1

Is there a way to display the ip address of the VMs running on the particular host. how do I use qemu hooks to see all the registered vm in the host. One possible way is to sniff the packets to and from the NIC of the host . But how to filter the broadcast ip address from the source and destination ip address. Can any one suggest a possible way to achieve this. I am not using static ip address for the VMs. A script in python will be of great help. Or even a idea will be appreciated..

Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
Prat_yow
  • 23
  • 1
  • 8

1 Answers1

1

Well, there's a couple ways you can do this. However the easiest is to use the virsh command line tool

This is system specific, but on Redhat you can install the libvirt-client package to get /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.

There are ways to get some of this information with libvirt-python as well, but it's much more code. Here's an example of using libvirt to connect to your hypervisor.

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)

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...
Community
  • 1
  • 1
VooDooNOFX
  • 4,674
  • 2
  • 23
  • 22
  • much appreciated the there is no control on the user the way they will create the VMs..The VMs can be possibly be created using libvirt then the virsh ll be the best solution . But is there a way to to find a way if the vms are created through qemu or libvirt..the libvert hooks monitors the events in the qemu ..so if i figure a way to list all the vms through qemu it will cover the libvirt as well. – Prat_yow Nov 18 '14 at 19:11
  • It doesn't matter how the VM is created. `libvirt` will connect directly to the hypervisor, and can introspect the VM's that are currently defined to pull the data you're looking for. – VooDooNOFX Nov 18 '14 at 23:11
  • Great ..!! SO can I list the ip addresses of the VMS using libvirt...?because this is what I need to do.. – Prat_yow Nov 19 '14 at 16:44
  • Yes, see edit. It's pretty raw, and will require you to play around slightly with it to get it right, but the gist is there. I don't have a VM Host to test it on, so the code above might now work. – VooDooNOFX Nov 20 '14 at 02:41
  • Thanks a ton..!! Will give it a try with the above. Will update you on the outcome. :) – Prat_yow Nov 20 '14 at 16:47
  • Traceback (most recent call last): File "libvirt-findvm.py", line 28, in mac_addresses = re.search(r" – Prat_yow Nov 21 '14 at 18:07
  • The above is able to list the domains of the vms sucessfully but somwer I am getting this error to list the ip address. – Prat_yow Nov 21 '14 at 18:08
  • So, that error seems to not be able to find a ` – VooDooNOFX Nov 21 '14 at 22:34
  • Yeah this solution perfectly fine. Now i want to list the vms without using any network . For that i am trying to find a way to create a hook that will monitor the events in qemu. SO the 1st step for me will be to find out how many qemu sessions are running in the host. – Prat_yow Nov 27 '14 at 19:53