Skip to content

Instantly share code, notes, and snippets.

@sauravrana646
Created April 17, 2023 19:08
Show Gist options
  • Save sauravrana646/f68f9cd8acf05e7994c72c475feca66f to your computer and use it in GitHub Desktop.
Save sauravrana646/f68f9cd8acf05e7994c72c475feca66f to your computer and use it in GitHub Desktop.
Linux network namespace
#!/bin/bash
set -o pipefail
string="$1"
if [ "$string" = "up" ]; then
ip netns add net1
ip netns add net2
ip link add veth1 netns net1 type veth peer name veth2 netns net2
ip netns exec net1 ip addr add 10.100.0.1/16 dev veth1
ip netns exec net2 ip addr add 10.100.0.2/16 dev veth2
ip netns exec net1 ip link set lo up
ip netns exec net2 ip link set lo up
ip netns exec net1 ip link set veth1 up
ip netns exec net2 ip link set veth2 up
ip netns exec net1 ping -c 3 10.100.0.2
ip netns exec net2 ping -c 3 10.100.0.1
fi
if [ "$1" == "down" ]; then
ip netns delete net1
ip netns delete net2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment