XenServer5.6测试通过,用于查找所有VM及其对应MAC地址写入output.csv文件,直接下载shell脚本。
#!/bin/bash
#https://www.haiyun.me
if [ $(whoami) != 'root' ];
then
    echo "Must be root to run $0"
    exit 1;
fi
if [[ ! -e output.csv ]];
then
    touch output.csv
else
    > output.csv
fi
for vmuuid in `xe vm-list | awk -F ':'  '/^uuid/ {print $2}'` ;
do
    namelabel=`xe vm-param-get uuid=$vmuuid param-name=name-label`
    case $namelabel in
        *"Control domain"* ) continue ;;
    esac
    i=`xe vif-list vm-name-label="$namelabel" | grep device | wc -l`
    writeline="\"$namelabel\""
    writeline+=","
    for ((j=0;j<$i;j++))
    do
        mac=`xe vif-list vm-name-label="$namelabel" params=MAC device=$j | awk -F': ' '/^MAC/ {print $2}'`
        if [ -z "$mac" ]
        then
            i=`expr $i + 1`
            continue
        else
            writeline+=$mac
        fi
        if [ $j -lt `expr $i - 1` ]
        then
            writeline+=","
        fi
    done
    echo "$writeline" >> output.csv
done
echo "VMs and their respective MAC addresses have been written to output.csv in the current directory."
exit 0