Wednesday, July 10, 2013

Find KVM Vm Ip address

Let assume we have a Virtual Machine running (started).
The VM is connected to the network thru a bridge ethernet.
We need to know its IP address, how?

Algorithm:
  • Ping all the ip address of the network.
    • If IP exist ARP will add a line to the ARP table with IP and MAC address.
  • Compare the MAC address saved in the KVM XML with all the MAC-IP saved inside ARP table.
    • When Match print the IP address
How to call: script_name <number\name of running VM>

example:

find_ip.sh Windows

#!/bin/bash

#Variables
network="192.168.1."

for end in {1..254}
do

  ping $network$end -c2 >/dev/null 2>/dev/null &
done

# Wait all ping to end
wait

for kvm_addr in `virsh dumpxml $1 | \

                 awk 'BEGIN {FS="\47"} /mac address/ {print $2}'`
do

  for arp_addr in `arp | awk '/ether/ {print $3"-"$1}'`
    do
    if [ "$kvm_addr" == ${arp_addr:0:17} ];
    then
      echo "Match: " $arp_addr
    fi
  done
done