virsh domifaddr domain does not show static IP

Charles Polisher chas at chasmo.org
Wed Sep 15 15:15:15 UTC 2021


On 9/9/21 11:11, Kaushal Shriyan wrote:

> I have assigned static IP for all the below KVM Guest VM's. Is there a 
> way to find out the IP of the below VM's from virsh utility or any 
> other utility? virsh domifaddr testdobssbahrainms does not show the 
> static IP.
>
> # virsh list --all
>  Id   Name                      State
> -----------------------------------------
>  1    testdobssbahrainms         running
<snip>
> # virt-install --version
> 2.2.1
> # cat /etc/redhat-release
> CentOS Stream release 8
>
> #virsh domifaddr testdobssbahrainms
> #Name       MAC address          Protocol     Address
> ------------------------------------------------------------------------------- 
>
>
Kaushal,

Probably the following was gleaned from this very list,
in the last year or so IIRC. Hope this works for you.
As you can see below, the technique is to grep the MAC addresses
from the domain definitions, e.g.:
       # virsh dumpxml <domain> | grep 'mac address'
       <mac address='52:54:00:30:fe:d8'/>
then grep the matching IP address from a listing of the
hypervisor host's ARP table. e.g.:
       # arp -an | fgrep '52:54:00:30:fe:d8'
? (192.168.122.6) at 52:54:00:30:fe:d8 [ether] on virbr0
    ^^^^^^^^^^^^^ here is the IP address.
The script follows my sig.

Best regards,
-- 
Charles Polisher

#!/usr/bin/perl -w

# 
https://rwmj.wordpress.com/2010/10/26/tip-find-the-ip-address-of-a-virtual-machine/
# Invocation: virt-addr.pl <guestname>

use strict;
use XML::XPath;
use XML::XPath::XMLParser;
use Sys::Virt;

# Open libvirt connection and get the domain.
my $conn = Sys::Virt->new (readonly => 1);
my $dom = $conn->get_domain_by_name ($ARGV[0]);

# Get the libvirt XML for the domain.
my $xml = $dom->get_xml_description ();

# Parse out the MAC addresses using an XPath expression.
my $xp = XML::XPath->new (xml => $xml);
my $nodes = $xp->find 
("//devices/interface[\@type='network']/mac/\@address");
my $node;
my @mac_addrs;
foreach $node ($nodes->get_nodelist) {
     push @mac_addrs, lc ($node->getData)
}

# Look up the MAC addresses in the output of 'arp -an'.
my @arp_lines = split /\n/, `arp -an`;
foreach (@arp_lines) {
     if (/\((.*?)\) at (.*?) /) {
         my $this_addr = lc $2;
         if (list_member ($this_addr, @mac_addrs)) {
             print "$1\n";
         }
     }
}

sub list_member
{
     local $_;
     my $item = shift;
     foreach (@_) {
         return 1 if $item eq $_;
     }
     return 0;
}





More information about the virt-tools-list mailing list