3

How can I find the IP of the VM on KVM(using Nmap)? If not,are there any functions in Libvirt to do that?

2
  • Answer: After a day's worth of trying nmap/ifconfig/iptables, I chanced to stumble upon this - rwmj.wordpress.com/2010/10/26/…. epic link.
    – P Ramesh
    Sep 9, 2011 at 20:23
  • Two virsh commands: domifaddr and net-dhcp-leases Mar 18, 2015 at 0:04

8 Answers 8

4

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
2

After long hours of getting my hands dirty, I chanced to stumble upon this Epic link.

  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.
3
  • 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 Ramesh
    Sep 9, 2011 at 20:56
  • Great, I added that to your answer Sep 9, 2011 at 21:08
1

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

1

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.

0

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

0

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.

0
# iterate over all running kvm vms
for i in $(virsh list | grep running | awk '{print $2}'); do virsh domifaddr  $i; done
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet2      52:54:00:54:XX:XX    ipv4         192.168.122.148/24

if the vms have dhcp assigned addresses of the network "default"

virsh net-start default
virsh net-autostart default

virsh net-dhcp-leases default

 Expiry Time           MAC address         Protocol   IP address           Hostname    Client ID or DUID
-------------------------------------------------------------------------------------------------------------------------------------------------
 2024-01-08 13:08:39   52:54:00:15:XX:XX   ipv4       192.168.122.207/24   ubuntu      ff:56:50:4d:...
 2024-01-08 13:42:21   52:54:00:95:XX:XX   ipv4       192.168.122.162/24   debian12    ff:00:95:9d:6e:...
 2024-01-08 13:40:09   52:54:00:aa:XX:XX   ipv4       192.168.122.249/24   vmname   -

alternatively: arp scan local network

netdiscover
0

If you have the qemu guest agent installed on the VM, you can use virsh's domifaddr command for that:

wouter@hypervisor:~$ virsh -c qemu:///system domifaddr myvmname
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet4      52:54:00:5c:5b:38    ipv4         192.168.122.217/24

wouter@hypervisor:~$ 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .