Last active
January 10, 2019 07:12
-
-
Save luginbash/7851d165f14e87626f744ba5c34f39bd to your computer and use it in GitHub Desktop.
create a pair of veth for use with netns
This file contains hidden or 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 | |
netns0="{$1}" | |
addr0="{$2}" | |
ip0() { ip -n $netns0 "$@"; } | |
n0() { ip netns exec $netns0 "$@"; } | |
waitiface() { bash -c "while [[ \$(< \"/sys/class/net/$1/operstate\") != up ]]; do read -t .1 -N 0 || true; done;"; } | |
ip netns del $netns0 2>/dev/null || true | |
ip link del vin-"$netns0" | |
ip netns add $netns0 | |
ip link add vout-"$netns0" type veth peer name vin-"$netns0" | |
ip link set vout-"$netns0" netns "$netns0" | |
ip link set up vin-"$netns0" | |
ip0 link set up dev vout-"$netns0" | |
waitiface vin-"$netns0" | |
ip0 addr add "$addr0" dev lo | |
ip0 route add default dev vout-"$netns0" | |
ip route add $addr0 dev vin-"$netns0" | |
n0 sudo -unobody /usr/local/bin/script --options |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sudo is unnecessary in the last line.