Created
May 24, 2022 15:12
-
-
Save ormergi/d4c9bb9679bb1264dc7dac0ad411981a to your computer and use it in GitHub Desktop.
Connect network namespaces with linux-bridge and veth's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
set -ex | |
function cleanup() { | |
set +e | |
ip netns exec blue ip link del dev veth-blue | |
ip netns exec red ip link del dev veth-red | |
ip netns del blue | |
ip netns del red | |
ip link del dev br-veth-blue | |
ip link del dev br-veth-red | |
ip link del dev br0 | |
set -e | |
} | |
function main(){ | |
ip netns add blue | |
ip netns add red | |
ip link add veth-blue type veth peer name br-veth-blue | |
ip link add veth-red type veth peer name br-veth-red | |
ip link set veth-blue netns blue | |
ip link set veth-red netns red | |
ip netns exec blue ip addr add 192.168.1.10/24 dev veth-blue | |
ip netns exec red ip addr add 192.168.1.20/24 dev veth-red | |
ip link add name br0 type bridge | |
ip link set br0 up | |
ip link set br-veth-blue up | |
ip link set br-veth-red up | |
ip netns exec blue ip link set veth-blue up | |
ip netns exec red ip link set veth-red up | |
ip link set br-veth-blue master br0 | |
ip link set br-veth-red master br0 | |
bridge link show br0 | |
ip netns exec blue ping 192.168.1.20 -w 2 || true | |
ip netns exec red ping 192.168.1.10 -w 2|| true | |
ip addr add 192.168.1.1/24 dev br0 | |
ip link set up dev br0 | |
ip netns exec blue ping 192.168.1.20 -w 2 || true | |
ip netns exec red ping 192.168.1.10 -w 2 || true | |
} | |
trap "cleanup" EXIT | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment