we only need to change the valuse here. do not chagne other code.
NET_NAMES=( ‘ansible_edge: asedgebr0‘ ‘smartcity_edge: scedgebr0‘ ‘smartcity_cloud: sccloud0‘ ‘ansible_cloud: asedgebr0‘) declare -A NET_MACS NET_MACS=( [ansible_edge]=‘EDGE1:1 EDGE2:1 EDGE3:1 HUB:1‘ [smartcity_edge]=‘EDGE1:2 EDGE2:2 EDGE3:2 HUB:2‘ [smartcity_cloud]=‘CLOUD:2 HUB:3‘ [ansible_cloud]=‘CLOUD:1‘) IP=192.168.124.2 MODE=route # route or nat declare -A MACS=([EDGE1]="" [EDGE2]="" [EDGE3]="" [HUB]="" [CLOUD]="")
REF:
# 33.9. Generating a new unique MAC address Red Hat Enterprise Linux 5 | Red Hat Customer Portal
function genmac(){ cat <<EOF | python3 import random # def randomMAC(): mac = [ 0x00, 0x16, 0x3e, random.randint(0x00, 0x7f), random.randint(0x00, 0xff), random.randint(0x00, 0xff) ] return ‘:‘.join(map(lambda x: "%02x" % x, mac)) print(randomMAC()) EOF }
# declare -A EDGE1_MACS EDGE2_MACS EDGE3_MACS HUB_MACS CLOUD_MACS # MACS=(EDGE1_MACS EDGE2_MACS EDGE3_MACS HUB_MACS CLOUD_MACS) # above 4.3 alpha version support nameref # declare -A MACS=([EDGE1]="" [EDGE2]="" [EDGE3]="" [HUB]="" [CLOUD]="") END=5 for i in "${!MACS[@]}"; do for j in $(seq 1 $END); do MACS[$i]=$(genmac)" "${MACS[$i]}; done echo "export ${i}_MACS=\"${MACS[$i]}\"" >> ~/.bashrc done
# declare -p NET_MACS # get_net_macs NET_MACS ansible_edge function get_net_macs(){ # $1 net macs map NET_MACS, $2 net mames, $3 macs pool mac_idxs=$(eval "echo \${$1[$2]}") # IFS=‘, ‘ read -r -a array <<< "$string" # array=(${string//:/ }) macstr=($mac_idxs) # return value ret=() for v in "${macstr[@]}"; do host=${v%%:*} idx=${v##*:} macs=${v%%:*}_MACS string=$(eval "echo \${$macs}") # IFS=‘, ‘ array=($countries) array=($string) # echo "\${array[${!idx}]}" mac=$(eval "echo \${array[${idx}]}") ret+=($mac) done echo ${ret[@]} }
# gen_host_array NET_MACS ansible_edge function gen_host_array(){ # $1 net macs map NET_MACS, $2 net mames, $3 base ip addr mac_idxs=$(eval "echo \${$1[$2]}") HOST=${mac_idxs//:/.} echo ${HOST,,} }
# inc_subnet 192.168.124.1 3 function inc_subnet(){ SUBN=${1#*.*.} SUBN=${SUBN%.*} SUFFIX=${1##*.} echo ${1%.*.*}.$((${SUBN} + ${2})).${SUFFIX} } # inc_ipaddr 192.168.124.1 3 function inc_ipaddr(){ echo ${1%.*}.$((${1##*.} + $2)) } # gen_ip_array NET_MACS smartcity_edge 192.168.127.1 function gen_ip_array(){ # $1 net macs map NET_MACS, $2 net mames, $3 base ip addr mac_idxs=$(eval "echo \${$1[$2]}") macstr=($mac_idxs) len=${#macstr[@]} ret=() for (( i=0; i<$len; i++ )); do ip=$(inc_ipaddr $3 $i); ret+=($ip) done echo ${ret[@]} }
function get_dhcp_item(){ # $1 MAC, $2 HOST, $3 DOMAIN, $4 IP DOM=${3/_/.} # (IFS=$‘;‘; echo " <host mac=\"$1\" name=\"${2,,}.${DOM,,}.com\" ip=\"$4\"/>") echo " <host mac=\"$1\" name=\"${2,,}.${DOM,,}.com\" ip=\"$4\"/>" } # gen_dhcp_items NET_MACS ansible_edge 192.168.124.192 function gen_dhcp_items(){ # $1 net macs map NET_MACS, $2 net mames, $3 base ip addr hosts=$(gen_host_array $1 $2) macs=$(get_net_macs $1 $2) ips=$(gen_ip_array $1 $2 $3) ha=($hosts) hm=($macs) hi=($ips) ret=() for i in ${!ha[@]}; do itm=$(get_dhcp_item ${hm[$i]} ${ha[$i]} $2 ${hi[$i]}) # declare -p itm ret+=("$itm") # echo ${ha[$i]} ${hm[$i]} ${hi[$i]} done # echo ${ret[@]} # declare -p ret ( IFS=$‘\n‘; echo "${ret[*]}" ) }
# gen_dhcp_items NET_MACS ansible_edge 192.168.124.192 function gen_dhcp_items(){ # $1 net macs map NET_MACS, $2 net mames, $3 base ip addr hosts=$(gen_host_array $1 $2) macs=$(get_net_macs $1 $2) ips=$(gen_ip_array $1 $2 $3) ha=($hosts) hm=($macs) hi=($ips) ret=() for i in ${!ha[@]}; do itm=$(get_dhcp_item ${hm[$i]} ${ha[$i]} $2 ${hi[$i]}) # declare -p itm ret+=("$itm") # echo ${ha[$i]} ${hm[$i]} ${hi[$i]} done # echo ${ret[@]} # declare -p ret ( IFS=$‘\n‘; echo "${ret[*]}" ) } # gen_networt_xml ansible_edge asedgebr0 192.168.124.1 $dhcp nat function gen_networt_xml(){ # $1 NET_NAME $2 BR NAME $3 IP $4 DHCP $5 MODE str="<network> <name>$1</name> <bridge name=\"$2\"/> <forward mode=\"${5}\"/> <ip address=\"${3%.*}.1\" netmask=\"255.255.255.0\"> <dhcp> <range start=\"${3%.*}.2\" end=\"${3%.*}.192\"/> $4 </dhcp> </ip> </network>" cat | tee /tmp/vir_network.xml << EOF <network> <name>$1</name> <bridge name="$2"/> <forward mode="${5}"/> <ip address="${3%.*}.1" netmask="255.255.255.0"> <dhcp> <range start="${3%.*}.2" end="${3%.*}.192"/> $4 </dhcp> </ip> </network> EOF # declare -p str } # gen_networt_xmls NET_NAMES $NET_MACS 192.168.124.1 nat # gen_networt_xmls NET_NAMES $NET_MACS function gen_networt_xmls(){ # $1 NET_NAMES $2 NET_MACS $3 IP $4 MODE: nat or route IP=${3:-192.168.124.2} for i in $(eval "echo \${!$1[@]}"); do v=$(eval "echo \${$1[i]}") NM=${v%:*} BR=${v#*: } ip=$(inc_subnet $IP $i) dhcp=$(gen_dhcp_items $2 $NM $ip) gen_networt_xml $NM $BR $IP "$dhcp" ${4:-nat} done }
原文:https://www.cnblogs.com/shaohef/p/14799860.html