#!/bin/bash
#自动生成keepalived virtual-router-id 不写死。放至同一个网络多个keepalived组之间的id冲突，导致异常状况

function general_vrid() {
    head -n 10 /dev/urandom | cksum | awk -F ' ' '{print $1 " % 255"}' | xargs expr
}


if [ -e /tmp/keepalived.vrid ]
then
    old_vrid=$(cat /tmp/keepalived.vrid)
    new_vrid=$(general_vrid)
    if [ ${new_vrid} == ${old_vrid} ]
    then
        echo $(general_vrid) > /tmp/keepalived.vrid
    else
        echo ${new_vrid} > /tmp/keepalived.vrid
    fi
else
    echo $(general_vrid) > /tmp/keepalived.vrid
fi

