-1

Is there a way I can get the IP address of a KVM guest client using bash? I need to add this to a bash script to automate a process and part of it is needing to get the IP address of the VM and pass it into a variable.

I've seen multiple things online but none of them seem to work.

Any help or advice will be appreciated

Rob Barnes
  • 23
  • 6

2 Answers2

2

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.

Earl Ruby
  • 1,263
  • 12
  • 14
  • 1
    `virsh -q` gets rid of the header stuff, so you can skip the `tail` and `head` stuff. – NickD Jul 16 '21 at 18:53
1
#!/bin/bash

ip=$(for mac in `sudo virsh domiflist $buildname |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"` ; do sudo arp -e |grep $mac  |grep -o -P "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ; done)
#iphost="$ip   appliance"

echo $ip
Rob Barnes
  • 23
  • 6