Created
January 3, 2013 22:02
-
-
Save tensorfields/4447791 to your computer and use it in GitHub Desktop.
Get a KVM guest's IP address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Returns the IP address of a running KVM guest VM | |
# Assumes a working KVM/libvirt environment | |
# | |
# Install: | |
# Add this bash function to your ~/.bashrc and `source ~/.bashrc`. | |
# Usage: | |
# $ virt-addr vm-name | |
# 192.0.2.16 | |
# | |
virt-addr() { | |
VM="$1" | |
arp -an | grep "`virsh dumpxml $VM | grep "mac address" | sed "s/.*'\(.*\)'.*/\1/g"`" | awk '{ gsub(/[\(\)]/,"",$2); print $2 }' | |
} |
@marafa and rest here is an awk only script:
virsh domifaddr $VM | awk -F'[ /]+' '{if (NR>2) print $5}'
virt-info() {
echo "------------------------------------------------------------------------------------------------------------------------";
printf "%-30s%-17s%-12s%-12s%-8s%-40s\n" "VM Name" "IP Address" "Memory" "Current" "VCPUs" "UUID";
virsh -c qemu:///system list --all | grep -o '[0-9]* [a-z]*.*running' | while read -r line;
do
line_cropped=$(echo "$line" | sed 's/[0-9][ ]*\([-._0-9a-zA-Z]*\)[ ]*running/\1/' );
printf "%-30s%-17s%-12s%-12s%-8s%-40s\n" "$line_cropped" $( virt-addr "$line_cropped" ) $( virt-mem $line ) $( virt-currmem $line ) $( virt-vcpu $line ) $( virt-uuid $line );
done;
echo "------------------------------------------------------------------------------------------------------------------------";
}
This breaks if the ID is higher than 9
Change this line
line_cropped=$(echo "$line" | sed 's/[0-9][ ]*\([-._0-9a-zA-Z]*\)[ ]*running/\1/' );
To
line_cropped=$(echo "$line" | sed 's/[0-9]*[ ]*\([-._0-9a-zA-Z]*\)[ ]*running/\1/' );
@mistofvongola and the others.
If you have qmeu-agent installed on the guest machine you can user virsh to fetch the details directly from the VM using virsh.
Example commad: virsh domifaddr Squid-4.1-CentOS-Testing --source agent --full
(the --source agent
flag is important)
[root@kvm4 ~]# virsh list
Id Name State
----------------------------------------------------
1 KVM4-MANAGER running
2 Fast-Windows-10 running
3 CHR-LAB-CLIENT1 running
4 CHR-LAB-PX3 running
5 CHR-LAB-SERVER1 running
6 CHR-R1 running
7 Squid-4.1-CentOS-Testing running
[root@kvm4 ~]# virsh domifaddr Squid-4.1-CentOS-Testing --source agent --full
Name MAC address Protocol Address
-------------------------------------------------------------------------------
lo 00:00:00:00:00:00 ipv4 127.0.0.1/8
lo 00:00:00:00:00:00 ipv6 ::1/128
eth0 52:54:00:7e:7f:29 ipv4 192.168.89.59/24
eth0 52:54:00:7e:7f:29 ipv6 fe80::125d:b868:7bf7:11f0/64
eth1 52:54:00:18:05:6b N/A N/A
eth2 52:54:00:fd:79:85 N/A N/A
eth3 52:54:00:cc:ac:c8 N/A N/A
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am sure a bash expert can make that line even better but it works for me and "if its not broken don't fix it" is a good rule for me