Skip to content

Instantly share code, notes, and snippets.

@ccamacho
Forked from tachang/network-restart.sh
Last active October 28, 2020 18:14
Show Gist options
  • Save ccamacho/21d711d67d8d85d000b8682a89a0a9f0 to your computer and use it in GitHub Desktop.
Save ccamacho/21d711d67d8d85d000b8682a89a0a9f0 to your computer and use it in GitHub Desktop.
Cleanly restart the default libvirt network
#!/bin/bash
virsh net-destroy kimgtnet0
virsh net-start kimgtnet0
virsh net-destroy kimgtnet1
virsh net-start kimgtnet1
VMS=$( virsh list | grep '-' | awk '{ print $2; }' )
for guest in $VMS ; do
echo "Bouncing $guest"
MACHINE_INFO=$( virsh dumpxml "$guest" | xpath -e /domain/devices/interface[1] 2> /dev/null )
MACHINE_MAC=$( echo "$MACHINE_INFO" | grep "mac address" | cut -d '"' -f 2 )
MACHINE_MOD=$( echo "$MACHINE_INFO" | grep "model type" | cut -d '"' -f 2 )
MACHINE_SOURCE=$( echo "$MACHINE_INFO" | grep "source network" | cut -d '"' -f 2 )
echo "guest info is $MACHINE_INFO"
echo "guest info is $MACHINE_MAC"
echo "guest info is $MACHINE_MOD"
echo "guest info is $MACHINE_SOURCE"
set +e
virsh detach-interface "$guest" network --mac "$MACHINE_MAC" && sleep 3
virsh attach-interface "$guest" network $MACHINE_SOURCE --mac "$MACHINE_MAC" --model "$MACHINE_MOD" --source "$MACHINE_SOURCE"
set -e
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment