You are not logged in.
This script creates a bridge as explained in Network bridge.
The name of the bridge is br0, and it adds the interface provided as a parameter to it.
#!/bin/sh
usage() {
printf '%s\n' "usage: ${0##*/} interface"
}
check_interface() {
[ -e "/sys/class/net/$1" ] || {
print_error "$1: No such interface"
exit 1
}
}
print_error() {
printf '\e[1;38;5;1m%s\e[m\n' "$1" >&2
}
while [ $# -gt 0 ]; do
case "$1" in
-h | --h | --he | --hel | --help)
usage
exit 0
;;
-*)
print_error "Unknown option: $1"
usage
exit 1
;;
*)
break
;;
esac
shift
done
[ -z "$1" ] && { usage; exit 1; }
if [ "$(id -u)" -ne 0 ]; then
print_error "Permission denied"
exit 1
fi
check_interface "$1"
interface="$1"
# Create a new bridge and change its state to up
bridge=br0
[ -e "/sys/class/net/$bridge" ] || {
ip link add name "$bridge" type bridge
ip link set dev "$bridge" up
}
address="$(ip address show "$interface" | awk '/inet/ {print $2}')"
bridge_address="$(ip address show "$bridge" | awk '/inet/ {print $2}')"
route="$(ip route show default dev "$interface" | awk '{print $3}')"
[ -n "$address" ] && {
ip address del "$address" dev "$interface"
[ -z "$bridge_address" ] && ip address add "$address" dev "$bridge"
}
[ -n "$route" ] && ip route append default via "$route" dev "$bridge"
ip link set "$interface" master "$bridge"
Last edited by macromal (2025-01-03 19:31:33)
Offline
and this has any advantage over https://wiki.archlinux.org/title/Network_bridge how exactly?
it would conflict with my setup already using br0 and br1
Offline
These are really the commands from the Wiki in a script. It only creates a single bridge interface br0, although you could modify it to create multiple ones. The advantage is that you don't have to write the commands by hand, which is a tedious task.
Offline
my point goes more in this direction: it's some lines of code without much explanation or reference in a very limited style and hence only suited for not many people
those who don't have experience with how to setup a bridge should consult the wiki anyway because it depends on thier specific setup - and those who do have experience likely have at least one bridge already setup or in way not compatible with your script
don't get me wrong - I appreciate the effort - but just throwing some lines of code in some forum without any further reference isn't much helpful
or to put it this way: I do understand what this code does - because I use a bridge for my VMs and hence have experience with it - although I use systemd-networkd doing all that stuff at boot for me ... but someone else not familiar with using a bridge may take this script - run it - and end up with something broken because they don't understand it and just dumb copy'n'paste'n'execute
may I recommend given at least some explanations for what kind of setups this is intented for and what it does specifically and a hint that it's incompatible with other ways of doing it - this would make your code way more helpful
Offline
You are right, sir.
The script simply sets up the bridge as in section one of the wiki. I didn't provide enough information in the post.
Now I usually connect using NAT network, but some time ago I often used the bridge that I created with that script; maybe it will help someone.
Offline