Last active
September 26, 2017 13:22
-
-
Save adler/f979f397cc05d481cb0090e5c7de81b0 to your computer and use it in GitHub Desktop.
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 | |
# runs a command in the same network namespaces as a process pid | |
set -e | |
function usage { | |
test -n "$*" & echo "$*" | |
echo "usage: $0 PID COMMAND [ARG...]" >&2 | |
exit 1 | |
} | |
test $# -lt 2 && usage | |
PID=$1 | |
shift | |
test -e /proc/$PID || usage "invalid PID $PID" | |
NETNS_DIR=/var/run/netns | |
mkdir -p $NETNS_DIR | |
TMP=$(mktemp -u $NETNS_DIR/netns.XXXXXXX) | |
trap 'test -e $TMP && rm -v $TMP' EXIT | |
NAME=$(basename "$TMP") | |
# ip netns expects a file in $NETNS_DIR | |
ln -sf "/proc/$PID/ns/net" "$NETNS_DIR/$NAME" | |
ip netns exec "$NAME" "$@" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment