Skip to content

Instantly share code, notes, and snippets.

@nyrahul
Created April 13, 2018 07:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyrahul/e16632c5667cc4d1858e35956c925dc5 to your computer and use it in GitHub Desktop.
Save nyrahul/e16632c5667cc4d1858e35956c925dc5 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -E
trap 'echo Failed line: $LINENO command: $BASH_COMMAND && exit $?' ERR
lcnt=1
nw_pref="192.168.10"
create_node()
{
ns="$1"; shift;
ip netns add $ns
ip netns exec $ns ip link set lo up
while [ "$1" != "" ]; do
hostlink="hostlink$lcnt";
ip link add $hostlink type veth peer name $1
ip link set $1 netns $ns
ip addr add $nw_pref${lcnt}.1/24 dev $hostlink
ip link set $hostlink up
ip netns exec $ns ip addr add $nw_pref${lcnt}.2/24 dev $1
ip netns exec $ns ip link set $1 up
ip netns exec $ns echo "2$lcnt out$lcnt" >> /etc/iproute2/rt_tables
ip netns exec $ns ip route add default via $nw_pref${lcnt}.1 table out$lcnt
ip netns exec $ns ip rule add from $nw_pref${lcnt}.2 table out$lcnt
ip netns exec $ns route add default gateway $nw_pref${lcnt}.1
((lcnt++)); shift;
done
}
main()
{
ip -all netns delete
[[ "$(ip netns)" != "" ]] && echo "could not delete all existing namespaces..." && exit 1
sysctl net.ipv4.ip_forward=1 >/dev/null
create_node "client" "wifi" "lte"
create_node "tgw" "eth0" "eth1"
create_node "srvpxy" "eth0"
create_node "server" "eth0"
}
main
@nyrahul
Copy link
Author

nyrahul commented Apr 13, 2018

create_node "client" "wifi" "lte" creates virtual namespace based node with two interface wifi and lte and assigns IP addresses, net-prefixes internally and makes sure that routing rules are setup correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment