简体   繁体   中英

Get IP address of KVM Guest vm

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

#!/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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM