简体   繁体   English

获取 KVM 来宾虚拟机的 IP 地址

[英]Get IP address of KVM Guest vm

Is there a way I can get the IP address of a KVM guest client using bash?有没有办法使用 bash 获取 KVM 来宾客户端的 IP 地址? 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.我需要将它添加到 bash 脚本中以自动化进程,其中一部分需要获取 VM 的 IP 地址并将其传递给变量。

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:我写了一个get-vm-ip脚本(你可以从https://github.com/earlruby/create-vm/blob/master/get-vm-ip下载)它使用它来获取 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 . virsh 命令获取 MAC 地址,最后一行使用arp查找 IP 地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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