48 lines
1.3 KiB
Bash
48 lines
1.3 KiB
Bash
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
BRANCH=$1
|
|
BRANCH=${BRANCH:-"main"}
|
|
ESXCLI=/usr/bin/esxcli
|
|
VSWITCH="vSwitch2"
|
|
source ~/.pass
|
|
|
|
|
|
# Check if portgroup exists
|
|
function checkIfPortgroupExists () {
|
|
local i=0
|
|
PORTGROUPS=( $($ESXCLI network vswitch standard portgroup list | grep $BRANCH | awk ' { print $1 }') )
|
|
for portgroup in "${PORTGROUPS[@]}"; do
|
|
if [ $portgroup == $BRANCH ] ; then
|
|
echo "Portgroup $BRANCH is not going to be created, already exists"
|
|
echo "Please Check ESXI configuration"
|
|
exit 0
|
|
fi
|
|
done
|
|
}
|
|
function createPortGroup() {
|
|
echo Adding portgroup $BRANCH to $VSWITCH
|
|
$ESXCLI network vswitch standard portgroup add --portgroup-name=$BRANCH --vswitch-name=$VSWITCH
|
|
}
|
|
|
|
function getMaxVlan(){
|
|
PORTGROUPS=( $($ESXCLI --formatter=csv network vswitch standard portgroup list | grep $VSWITCH | cut -d "," -f3 ) )
|
|
IFS=$'\n'
|
|
MAX_VLAN=$(echo "${PORTGROUPS[*]}" | sort -nr | head -n1)
|
|
NEXT_VLAN=$(( MAX_VLAN + 1 ))
|
|
[[ $NEXT_VLAN -lt 3000 ]] && NEXT_VLAN=3000
|
|
echo VLAN assigned is $NEXT_VLAN
|
|
}
|
|
|
|
function setVlan(){
|
|
$ESXCLI network vswitch standard portgroup set -p $BRANCH --vlan-id $1
|
|
}
|
|
|
|
#### SCRIPT
|
|
checkIfPortgroupExists
|
|
createPortGroup
|
|
getMaxVlan
|
|
setVlan $NEXT_VLAN
|
|
echo portgroup $BRANCH created with vlan_id $NEXT_VLAN
|