Skip to content

Instantly share code, notes, and snippets.

@0x3333
Last active October 30, 2022 15:30
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 0x3333/6a59b5f96b81755870267ada2b3721d3 to your computer and use it in GitHub Desktop.
Save 0x3333/6a59b5f96b81755870267ada2b3721d3 to your computer and use it in GitHub Desktop.
List KVM/Virsh Network Interface per VM

List KVM/Virsh Network Interface per VM

This script will list all VMs and the network interfaces attached to that VM.

Sample output:

smb:
	vnet1
system_prod:
	vnet8
system_test:
	vnet9
pabx:
	vnet2
	vnet3
management:
	vnet4
docker:
	vnet5
	vnet6
	vnet7

Added a script to list the network tree including the VM that is attached to the interface(if any):

lo
br0
    bond0.20@bond0
    vnet11 - VM: samba
    vnet16 - VM: docker
    vnet18 - VM: system_prod
br1
    bond0
       eno1
       eno2
       eno3
    vnet6 - VM: unifi
    vnet12 - VM: system_test
    vnet15 - VM: docker
br2
    bond0.30@bond0
    vnet4 - VM: pabx
    vnet17 - VM: docker
br3
    eno4
    vnet5 - VM: pabx
#!/bin/bash
DEFAULT_INDENT=" "
DIRECTION="DOWN"
function usage() {
cat <<USAGEEND
The script prints network devices hierarchy as a tree view.
Possible arguments:
-u prints tree from bottom to up (default). Physical devices are roots of the tree.
-d prints tree from up to bottom. Logica, devices are roots of the tree.
USAGEEND
}
function printdown() {
local indent="$1"
devs="$2"
for indev in $devs; do
echo -n "$indent" "$indev"
if [[ "$indev" == "vnet"* ]]; then
echo -n " - VM: ${vmdev[$indev]}"
fi
echo
printdown "$DEFAULT_INDENT$indent" "${devicesdown[$indev]}"
done
}
function printup() {
local indent="$1"
devs="$2"
for indev in $devs; do
echo "$indent" "$indev"
printup "$DEFAULT_INDENT$indent" "${devicesup[$indev]}"
done
}
while [ ! -z "$1" ]; do
case "$1" in
-d)
DIRECTION=DOWN
;;
-u)
DIRECTION=UP
;;
-h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
shift
done
declare -A vmdev
for vm in $(virsh list | grep running | awk '{print $2}'); do
devs=$(virsh dumpxml $vm | grep "vnet" | sed "s/[^']*'\([^']*\)'[^']*/\1/g")
for dev in $devs; do
vmdev[$dev]=$vm
done
done
declare -A devicesup
declare -A devicesdown
while read line; do
dev=${line#*: }
dev=${dev%%:*}
devicesup[$dev]=""
if [ -z "${devicesdown[$dev]}" ]; then
devicesdown[$dev]=""
fi
if [[ "$line" == *" master "* ]]; then
master=${line#* master *}
master=${master%% *}
devicesup[$dev]="${devicesup[$dev]} $master"
devicesdown[$master]="${devicesdown[$master]} $dev"
fi
done < <(ip -o link)
if [ "$DIRECTION" == "UP" ]; then
for dev in ${!devicesup[@]}; do
if [ -z "${devicesdown[$dev]}" ]; then
echo $dev
printup "$DEFAULT_INDENT" "${devicesup[$dev]}"
fi
done
else
for dev in "${!devicesdown[@]}"; do
if [ -z "${devicesup[$dev]}" ]; then
echo $dev
printdown "$DEFAULT_INDENT" "${devicesdown[$dev]}"
fi
done
fi
#!/bin/bash
for vm in $(virsh list | grep running | awk '{print $2}')
do
echo "$vm: " && virsh dumpxml $vm | grep "vnet" | sed "s/[^']*'\([^']*\)'[^']*/\t\1/g"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment