How can I find the IP of the VM on KVM(using Nmap)? If not,are there any functions in Libvirt to do that?
6 Answers
Fast and dirty example for KVM on Centos 63 x86_64. Short ping in background to all possible IP addresses that the VMs may have in this segment
for i in {1..249}
do
ping -c 1 -n -q -r -t 1 -s 1 -W 1 192.168.100.$i > /dev/null &
done
Extract the names and MAC addresses of the VMs via virsh and extract IP from ARP table
for name in `virsh list | grep running | awk '{ print $2 }'`
do
printf "\n$name\n "
arp -e | grep "`virsh dumpxml $name | grep "mac address"|sed "s/.*'\(.*\)'.*/\1/g"`" |
awk '{ printf $1 ; printf " " ; printf $3 }'
done
After long hours of getting my hands dirty, I chanced to stumble upon this Epic link.
- Get the MAC address of the VM from the config file of the VM(since I'm using KVM, I queried the .xml file for the MAC address).
- Then use arp -a and get all the the MAC addresses and the Address of the interfaces and store it in a file
- Query the file for the MAC address of VM from step 1.
-
Could you describe how this solved your problem? What did you do exactly? Sep 9, 2011 at 20:26
-
@Simon Sheehan- Here's how I did it- 1. get the MAC address of the VM from the config file of the VM(since I'm using KVM, I queried the .xml file for the MAC address). 2. Then use arp -a and get all the the MAC addresses and the Address of the interfaces and store it in a file 3.query the file for the MAC address of VM from step 1.– P RameshSep 9, 2011 at 20:56
-
I wrote a bash script to do this but to also filter for machines with the name "swarm" in them. You could remove the filter or change it as needed. (remove | grep "swarm"
) You could use this as the full script or copy paste the one liner from it:
#!/usr/bin/env bash
# we make assumption that the VM's we want have the word swarm in their name. Edit for different name or to get all unfiltered names
for i in $(virsh list | grep "swarm" | awk '{print $2;}' ); do virsh domifaddr $i | sed 3q | sed '$!d' |awk '{print $4}' ; done;
The constituent parts are virsh commands, virsh list
to get a list of all VM's, and then in a for loop calling domifaddr <VM NAME>
to get the ip addr of it. All the rest is to filter out the text to only the IP address one per line. I wrote/ran this in Ubuntu KVM server 15.10
When started with qemu-kvm
and the --nographic
option, you can use ctrl-a
c
to get the qemu monitor (some kind of shell to monitor and control VMs).
The command info network
will give network connection info, including MAC address.
The command info usernet
shows bridge information: e.g. how a guest port is mapped to a host port. This information often includes the guest ip.
Example
Start vm with some tcp port forwarding:
qemu-kvm -cpu host -m 1024 -hda ~/myvm.qcow -net user,hostfwd=tcp::10022-:22 -net nic -nographic
after ctrl-a
c
the commands info network
and info usernet
give
(qemu) info network
hub 0
\ hub0port1: e1000.0: index=0,type=nic,model=e1000,macaddr=58:44:00:22:11:
\ hub0port0: user.0: index=0,type=user,net=10.0.2.0,restrict=off
(qemu) info usernet
VLAN 0 (user.0):
Protocol[State] FD Source Address Port Dest. Address Port RecvQ S
TCP[HOST_FORWARD] 13 * 10022 10.0.2.15 22 0
Reference: QEMU-doc at wiki.qemu.org.
Using virt-cat command, you can query all files on a VM. For example, the dhcp lease file:
virt-cat -d your_domain /var/lib/dhcp/dhclient.eth0.leases
I wrote a get-vm-ip
script (which you can download from https://github.com/earlruby/create-vm/blob/master/get-vm-ip) which uses this to get the IP:
HOSTNAME=[your vm name]
MAC=$(virsh domiflist $HOSTNAME | awk '{ print $5 }' | tail -2 | head -1)
arp -a | grep $MAC | awk '{ print $2 }' | sed 's/[()]//g'
The virsh command gets the MAC address, the last line finds the IP address using arp
.