libvirt kvm
There are times when we start virtual machine and want to get ip address quickly without open any GUI or connecting virtual machines console. Here is the tip.
有时,我们启动虚拟机并希望快速获取IP地址,而无需打开任何GUI或连接虚拟机控制台。 这里是提示。
virsh domiflist命令 (virsh domiflist Command)
#!/bin/bash
VMNAME=$1
for mac in `sudo virsh domiflist $VMNAME |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"` ; do arp -e | grep $mac | grep -o -P "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ; done
The magic is that virtual machines interfaces are listed than mac address is grepped. After grepping mac address we are looking our ARP table to find corresponding ip address and put it to the screen. This script will list all interfaces of VM if it has more than one.
神奇之处在于,列出了虚拟机接口,而不是grep了mac地址。 grep mac mac address之后,我们正在查看我们的ARP表以找到相应的IP地址并将其显示在屏幕上。 如果该脚本有多个,它将列出VM的所有接口。
As we have provided as script we can use this script like below. We will name script as vm_ip and provide the virtual machine or VM name as parameter. VM name is ubu1 in this example.
正如我们提供的脚本一样,我们可以像下面这样使用该脚本。 我们将脚本命名为vm_ip并提供虚拟机或VM名称作为参数。 在此示例中,虚拟机名称为ubu1。
$ ./vm_ip "ubu1"
翻译自: https://www.poftut.com/how-to-get-runnig-vm-ip-address-in-kvmlibvirt/
libvirt kvm