tapsetup: add capability to list bridges

This commit is contained in:
Martine S. Lenders 2020-09-01 13:22:40 +02:00
parent de36c400d4
commit a9722dda71
No known key found for this signature in database
GPG Key ID: CCD317364F63286F

View File

@ -17,6 +17,9 @@ usage() {
echo "Options:" >&2
echo " -c [<num>], --create [<num>]: Create <num> tap interfaces (default: 2)" >&2
echo " -d, --delete: Delete all interface" >&2
echo " -l <iface>, --list <iface>: If <iface> belongs to a bridge, list the bridge and" >&2
echo " all interfaces that belong to it. If <iface> does " >&2
echo" not belong to a bridge, just print <iface>." >&2
echo " -b <name>, --bridge <name>: Give name for the bridge (default: tapbr)" >&2
echo " -t <name>, --tap <name>: Name base for the tap interfaces; the" >&2
echo " generated names will be <name>x" >&2
@ -148,6 +151,50 @@ create_tap() {
esac
}
get_master() {
IFACE=$1
case "${PLATFORM}" in
Linux)
MASTER=$(ip link show ${IFACE} | grep -o "master \S\+" | cut -d' ' -f2)
;;
FreeBSD|OSX)
for IF in $(ifconfig | grep -oiE "^[a-z0-9_-]+"); do
if ifconfig $IF | grep -q "member: $IFACE"; then
MASTER=${IF}
break
fi
done
;;
*)
;;
esac
if [ -z "$MASTER" ]; then
# IFACE is its own master
echo "$IFACE"
else
echo "$MASTER"
fi
}
list_bridge() {
BRIDGE=$1
echo "$BRIDGE:"
case "${PLATFORM}" in
Linux)
for IF in $(ls /sys/class/net/${BRIDGE}/brif); do
echo "- $IF"
done
;;
FreeBSD|OSX)
for IF in $(ifconfig ${BRIDGE} | grep -oiE "member: .+ " | cut -d' ' -f2); do
echo "- $IF"
done
;;
*)
;;
esac
}
while true ; do
case "$1" in
-6)
@ -185,6 +232,20 @@ while true ; do
fi
COMMAND="delete"
shift ;;
-l|--list)
if [ -n "${COMMAND}" ]; then
usage
exit 2
fi
COMMAND="list"
case "$2" in
"")
usage
exit 2 ;;
*)
BRNAME="$2"
shift 2 ;;
esac ;;
-h|--help)
usage
exit ;;
@ -213,13 +274,13 @@ while true ; do
esac
done
if [ -z "${SUDO_USER}" ]; then
echo 'Environment variable $SUDO_USER required; Please run with `sudo`'
exit 1
fi
if [ -z "${COMMAND}" ]; then
COMMAND="create"
fi
if [ -z "${SUDO_USER}" ] && [ "${COMMAND}" != "list" ]; then
echo 'Environment variable $SUDO_USER required; Please run with `sudo`'
exit 1
fi
case "$(uname -s)" in
Darwin)
PLATFORM="OSX"
@ -251,6 +312,8 @@ if [ "${COMMAND}" = 'create' ]; then
elif [ "${COMMAND}" = 'delete' ]; then
delete_bridge
elif [ "${COMMAND}" = 'list' ]; then
list_bridge $(get_master "$BRNAME")
else
echo 'unknown command'
exit 1