2

I am looking for the shell script to find the IP address of a virtual machine created by using KVM/VIRSH.

I used the following steps to get it so, but couldn´t able to find it.

  1. Ping the IP addresses in range

2.Use Virsh list command to list all the active VM

3.use Virsh dumpxml domainname and project the xml of vm

  1. use grep command and fetch the Hardware address of vm

5.Display the hardware address of each vm

Now I would like to add one more step like fetching the IP address for that particular Hardware address using ¨arp -ne¨

I couldn´t able to figure out how to add the part.

could any one help me on this.

for i in {1..150}
do
  ping -c 1 -n -q -r  -t 1 -s 1  -W 1   192.168.1.$i > /dev/null &
done


 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 "\n" }'
done

current output:

$ ./virshshell.sh
  vm2 52:54:00:4b:7f:41
  vm3 52:54:00:0e:4c:42

The output I am expecting is

 $ ./virshshell.sh
     vm2 52:54:00:4b:7f:41  192.*.*.*
     vm3 52:54:00:0e:4c:42  192.*.*.*
Harry
  • 253
  • 1
  • 6
  • 19
  • 1
    Not related to your question, but you might consider using a tool such as XMLStarlet rather than trying to use grep and sed to operate on XML (which is inherently prone to breaking should the details of how that content is pretty-printed change). – Charles Duffy May 04 '15 at 15:42
  • 1
    Beyond that, I'd strongly consider using `set -x` to trace your script's execution and determine exactly where and how it deviates from what you expect. – Charles Duffy May 04 '15 at 15:43
  • 1
    ...as it is, though, this isn't a great fit for StackOverflow, since your question is somewhat the opposite of a MCVE (minimal, complete, verifiable example): It can't be run without a bunch of tools and environment not everyone else will have (even folks who use libvirt may have differently-configured networking to the point where they can't reproduce your problem / couldn't use this tool either). If you narrow down where it breaks, then you could ask a question that didn't require libvirt / virsh / etc. to reproduce or test solutions. – Charles Duffy May 04 '15 at 15:45
  • Your command contains rather complex nested quotes. Try using temp variable. For example: `mac_addr=$( virsh dumpxml $name | grep "mac address" | sed "s/.*'\(.*\)'.*/\1/g" )` . – tivn May 04 '15 at 15:53
  • Now i am getting the MAC address, how can i get the IP address from the arp table? – Harry May 04 '15 at 20:50

1 Answers1

0

Use nmap to do network discovery instead of ping. It can do what ping does but also much more, plus it runs way faster and takes care of the network-scope scanning that you're doing in your for loop.

$ nmap -T5 -n -PE 192.168.4.0/24 > /dev/null
$ ip neigh show | grep 192.168.4 | grep -v FAILED
192.168.4.92 dev eth0 lladdr 54:52:00:90:90:92 REACHABLE
192.168.4.11 dev eth0 lladdr fa:16:3e:fa:ac:07 REACHABLE
192.168.4.91 dev eth0 lladdr 54:52:00:90:90:91 REACHABLE
192.168.4.90 dev eth0 lladdr 54:52:00:90:90:90 REACHABLE
RSD3
  • 26
  • 3