4 查看KVM虚拟机IP.

4 查看KVM虚拟机IP.

不能直接查看KVM虚拟机IP,只能通过mac地址,再查IP

1. 查看kvm虚拟机

1
virsh list --all

输出如下信息:
Id Name State
—————————————————-
1 kvm22 running
2 kvm31 running
3 kvm11 running
4 kvm12 running

2. 查kvm11的MAC地址:

1
virsh dumpxml kvm11 | grep mac

输出如下信息:

/machine hvm

3. 查看 mac address=’52:54:00:ad:fa:f7 对应的ip

1
arp -a | grep 52:54:00:ad:fa:f7

输出如下信息

? (192.168.122.159) at 52:54:00:ad:fa:f7 [ether] on virbr0

4. 写一个shell文件,全部列出来.

1
vi virsh.sh

增加如下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
#ping当前网段内在线的主机,以便产生arp记录.
for ip in 10.2.2.{1..253};do
{
ping -c1 $ip >/dev/null 2>&1
}&
done
#依次查找arp记录.
running_vms=`virsh list |grep running`
echo -ne "共有`echo "$running_vms"|wc -l`个虚拟机在运行.\n"
for i in `echo "$running_vms" | awk '{ print $2 }'`;do
mac=`virsh dumpxml $i |grep "mac address"|sed "s/.*'\(.*\)'.*/\1/g"`
ip=`arp -ne |grep "$mac" |awk '{printf $1}'`
printf "%-30s %-30s\n" $i $ip
done

给showip.sh 增加执行权限

1
chmod a+x virsh.sh

5. 执行showip.sh 得到ip地址

1
./virsh.sh

输出如下信息:
共有4个虚拟机在运行.
kvm22 192.168.122.173
kvm31 192.168.122.108
kvm11 192.168.122.159
kvm12 192.168.122.213

Leave a Reply

Your email address will not be published. Required fields are marked *