Skip to content

Instantly share code, notes, and snippets.

@kurtis318
Last active May 26, 2020 20:33
Show Gist options
  • Save kurtis318/514890410583eef3ee8cea77f51dfc3e to your computer and use it in GitHub Desktop.
Save kurtis318/514890410583eef3ee8cea77f51dfc3e to your computer and use it in GitHub Desktop.
Finding KVM guest IPs using virsh command

The following works all the time.

REF: https://adam.younglogic.com/2017/10/vm-ip-virsh/

I presume the VM should be up and running for the following commands to work.

HOSTNAME="rhel75"
MAC=$(virsh domiflist $HOSTNAME | awk '{ print $5 }'| tail -2 | head -1)
arp -a | grep $MAC | awk '{ print $2 }' | sed 's/[()]//g'

Here is a script called finddomup.sh I have written:

#!/bin/bash

if (( UID != 0)); then
    echo "This script must be run with root authority."
    exit 100
fi

DOMNAME=$1
LEN=${#DOMNAME}
if (( LEN == 0 )); then
    echo "Specify a domain name."
    exit 101
fi

MAC=$(virsh domiflist "${DOMNAME}" | awk '{ print $5 }'| tail -2 | head -1)
arp -a | grep "${MAC}" | awk '{ print $2 }' | sed 's/[()]//g'

exit 0
## The following does NOT always work.

REF: https://www.systutorials.com/241639/how-to-get-the-ip-addresses-of-vms-in-kvm-with-virsh/

[kurtis@jakedog ~]$ sudo virsh list --all [sudo] password for kurtis: Id Name State

1 win10 running

  • mintxfce                       shut off
    

[kurtis@jakedog ~]$ sudo virsh domifaddr 1 Name MAC address Protocol Address

vnet0 52:54:00:36:e1:c7 ipv4 192.168.122.25/24

[kurtis@jakedog ~]$ ssh 192.168.122.25 [kurtis@jakedog ~]$ sudo virsh domiflist win10 Interface Type Source Model MAC

vnet0 network default rtl8139 52:54:00:36:e1:c7 macvtap0 direct enp1s0 rtl8139 52:54:00:a6:36:b8

[kurtis@jakedog ~]$ arp -e Address HWtype HWaddress Flags Mask Iface 192.168.122.25 ether 52:54:00:36:e1:c7 C virbr0 chloester ether e4:1f:13:ee:53:6a C enp1s0 LGSmartTV.roc.mn.charte ether 9c:80:df:9c:e9:76 C enp1s0 basenet ether 98:fc:11:fa:b8:63 C enp1s0 192.168.0.241 ether 48:f8:b3:bb:d6:b8 C enp1s0

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