Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

migrated from stackoverflow.com Sep 9 '11 at 17:46

This question came from our site for professional and enthusiast programmers.

    
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 '11 at 20:23
    
Two virsh commands: domifaddr and net-dhcp-leases – Nehal J. Wani Mar 18 '15 at 0:04

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
share|improve this answer
up vote 2 down vote accepted

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.
share|improve this answer
    
Could you describe how this solved your problem? What did you do exactly? – Simon Sheehan Sep 9 '11 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 '11 at 20:56
    
Great, I added that to your answer – Simon Sheehan Sep 9 '11 at 21:08

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.