Re: virsh domifaddr domain does not show static IP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



After installing and configuring the qemu-guest-agent in the guest - https://wiki.libvirt.org/page/Qemu_guest_agent
 it should give you the IP with the domifaddr --source flag:

# virsh domifaddr avocado-vt-vm1 --source agent
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 lo         00:00:00:00:00:00    ipv4         127.0.0.1/8
 -          -                    ipv6         ::1/128
 enc1       52:54:00:22:36:1b    N/A          N/A


On Wed, Sep 15, 2021 at 5:50 PM Charles Polisher <chas@xxxxxxxxxx> wrote:
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;
}



[Index of Archives]     [Linux Virtualization]     [KVM Development]     [CentOS Virtualization]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]     [Video 4 Linux]

  Powered by Linux