Last active
May 20, 2022 07:36
-
-
Save cl4u2/eb155519d9fbda8c5dd87177fd8cb64f to your computer and use it in GitHub Desktop.
cheap network emulation with Linux network namespaces
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 | |
# emulate a 3 nodes network using Linux network namespaces | |
# n1 -- r0 -- n2 | |
set -x | |
# cleanup | |
ip netns del n1 | |
ip netns del r0 | |
ip netns del n2 | |
ip link del veth1 | |
ip link del veth2 | |
ip link del veth3 | |
ip link del veth4 | |
set -e | |
# create 3 namespaces corresponding to the three nodes | |
ip netns add n1 | |
ip netns add r0 | |
ip netns add n2 | |
# create a veth between n1 and r0 | |
ip link add veth1 type veth peer name veth2 | |
ip link set veth1 netns n1 | |
ip link set veth2 netns r0 | |
# create a veth between r0 and n2 | |
ip link add veth3 type veth peer name veth4 | |
ip link set veth3 netns r0 | |
ip link set veth4 netns n2 | |
# configure n1 | |
ip netns exec n1 ip link set lo up | |
ip netns exec n1 ip addr add 10.0.0.1/24 dev veth1 | |
ip netns exec n1 ip link set veth1 up | |
ip netns exec n1 ip route add default via 10.0.0.2 | |
# configure r0 | |
ip netns exec r0 ip link set lo up | |
ip netns exec r0 ip addr add 10.0.0.2/24 dev veth2 | |
ip netns exec r0 ip link set veth2 up | |
ip netns exec r0 ip addr add 10.0.1.2/24 dev veth3 | |
ip netns exec r0 ip link set veth3 up | |
# configure n2 | |
ip netns exec n2 ip link set lo up | |
ip netns exec n2 ip addr add 10.0.1.1/24 dev veth4 | |
ip netns exec n2 ip link set veth4 up | |
ip netns exec n2 ip route add default via 10.0.1.2 | |
# ip netns list | |
# ip netns exec <node name> bash | |
# ping from n1 to r0 | |
ip netns exec n1 ping -c 3 10.0.0.2 | |
# ping from r0 to n1 | |
ip netns exec r0 ping -c 3 10.0.0.1 | |
# ping from r0 to n2 | |
ip netns exec r0 ping -c 3 10.0.1.1 | |
# ping from n2 to r0 | |
ip netns exec n2 ping -c 3 10.0.1.2 | |
# enable forwarding on r0 | |
echo 1 | ip netns exec r0 tee /proc/sys/net/ipv4/ip_forward | |
# ping from n1 to n2 | |
ip netns exec n1 ping -c 3 10.0.1.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The same with single subnet and bridge: https://gist.github.com/kotovalexarian/0194cb801ca4fa9ab77c9f4a2104f4ef