Skip to content

Instantly share code, notes, and snippets.

@luginbash
Last active January 10, 2019 07:12
Show Gist options
  • Select an option

  • Save luginbash/7851d165f14e87626f744ba5c34f39bd to your computer and use it in GitHub Desktop.

Select an option

Save luginbash/7851d165f14e87626f744ba5c34f39bd to your computer and use it in GitHub Desktop.
create a pair of veth for use with netns
#!/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
@luginbash
Copy link
Copy Markdown
Author

sudo is unnecessary in the last line.

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