Skip to content

Instantly share code, notes, and snippets.

@manuthu
Last active October 2, 2017 18:54
Show Gist options
  • Save manuthu/a5bbcfc3a0733c7995d1b15e662ac460 to your computer and use it in GitHub Desktop.
Save manuthu/a5bbcfc3a0733c7995d1b15e662ac460 to your computer and use it in GitHub Desktop.
libvirt fetch IP address from a domain

View running domains

~ virsh list --all
 Id    Name                           State
----------------------------------------------------
 15    compute.local                  running
 16    storage.local                  running

Get mac address

~ virsh domiflist storage.local
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:b5:9b:88

Query the arp table

~ arp -e
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.147.2            ether   00:50:56:ed:ec:9c   C                     br0
192.168.147.132          ether   52:54:00:39:2d:f7   C                     br0
192.168.147.133          ether   52:54:00:b5:9b:88   C                     br0
192.168.147.254          ether   00:50:56:e8:98:2e   C                     br0
192.168.147.1            ether   00:50:56:c0:00:08   C                     br0

Should be note that the ARP table could be stale.

One liner

~ for mac in `virsh domiflist storage.local |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"` ; do arp -e |grep $mac  |grep -o -P "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ; done
192.168.147.133

Borrowed from https://stackoverflow.com/questions/19057915/libvirt-fetch-ipv4-address-from-guest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment