Skip to content

Instantly share code, notes, and snippets.

@activedecay
Last active January 7, 2021 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save activedecay/42e73907e30f21870ba0c3debaa8a76e to your computer and use it in GitHub Desktop.
Save activedecay/42e73907e30f21870ba0c3debaa8a76e to your computer and use it in GitHub Desktop.
#!/bin/bash
# Based on public domain work by Yury V. Zaytsev <yury@shurup.com>
# Deps: bash4+, xpath, egrep, virsh
set -e
set -u
MACHINES=$( virsh list --name | egrep "\S" )
NETWORK="default"
BRIDGE="bridge0"
DRYRUN="${DRYRUN:-echo}"
$DRYRUN virsh net-destroy $NETWORK
$DRYRUN virsh net-start $NETWORK
declare -A MACS
declare -A TYPES
#Create array
for m in $MACHINES ; do
#MACHINE_INFO=$( virsh dumpxml --inactive "$m" ) is useful if you encounter errors
MACHINE_INFO=$( virsh dumpxml "$m" )
eval $(echo $MACHINE_INFO | xpath "/domain/devices/interface[@type='bridge']/mac/@address")
eval $(echo $MACHINE_INFO | xpath "/domain/devices/interface[@type='bridge']/model/@type")
MACS[$m]=$address
TYPES[$m]=$type
echo "machine $m network bridge $type mac $address"
done
#Could consider running all these in the background/parallely .. not sure how safe that is so not doing so currently
#set +e/-e also copied from previous script -- better error handling would be ideal for a production quality script
for m in $MACHINES ; do
set +e
$DRYRUN virsh detach-interface --domain "$m" --mac "${MACS[$m]}" --type bridge
set -e
done
#Taken from previous script as 3s downtime was not a problem for my scenarios and I did not test anything else
#sleep 3
#Same as previous loop re: background/set
for m in $MACHINES; do
set +e
$DRYRUN virsh attach-interface --domain "$m" --type bridge --source $BRIDGE --mac "${MACS[$m]}" --model "${TYPES[$m]}"
set -e
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment