mcwhirter.com.au/ craige/ blog/ 2014/ Find the IP Address of a Virtual Machine
Find the IP Address of a Virtual Machine

So, you're running libvirt, you've run up a new VM that's using DHCP and you'd like to know the IP address without logging into the console.

If you haven't already, set LIBVIRT_DEFAULT_URI. The below example assumes it's localhost:

$ export LIBVIRT_DEFAULT_URI=qemu:///system

Next you need to obtain the virtual machine's MAC address:

$ virsh dumpxml vm-name | grep "mac address"

Then you can use the MAC address to get the running VM's IP address:

$ /usr/sbin/arp -an | grep mac-address

So an example of this would look like this:

$ export LIBVIRT_DEFAULT_URI=qemu:///system
$ virsh dumpxml debian-7 | grep "mac address" 
      <mac address='52:54:00:ec:fe:cd'/>
$ /usr/sbin/arp -an | grep 52:54:00:ec:fe:cd
? (192.168.122.197) at 52:54:00:ec:fe:cd [ether] on virbr0

You can also write a nice little bash script to do this for you:

#!/bin/bash
#
# This script uses virsh and arp to determine the IP address of a VM

# Where are your tools?
virsh=/usr/bin/virsh
arp=/usr/sbin/arp
grep=/bin/grep
sed=/bin/sed

# Get the VM name as an argument:
VM_NAME=$1

# Ensure the default URI is set for your environment:
export LIBVIRT_DEFAULT_URI=qemu:///system

# Obtain the MAC address from libvirt:
MAC_ADDRESS=`$virsh dumpxml $VM_NAME | $grep "mac address" | $sed "s/.*'\(.*\)'.*/\1/g"`

# Use arp to find the IP address you're looking for via it's MAC address:
$arp -an | $grep $MAC_ADDRESS

exit 0

An example output of that script looks likes this:

$ ./vm_ip.sh debian-7
? (192.168.122.197) at 52:54:00:ec:fe:cd [ether] on virbr0

You can get a copy of that script here.

This site is powered by ikiwiki.