Created
November 7, 2016 19:20
-
-
Save alandipert/6bea29ea46087e27468cd8e070b708c7 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env bash | |
| # with_host.sh 54.90.136.89 foo.bar.com chromium-browser | |
| # Runs a program with a one-off /etc/hosts via unshare(1) | |
| # http://unix.stackexchange.com/a/242830 | |
| if [[ $1 == "--" ]]; then | |
| mount $2 /etc/hosts --bind | |
| USER=$3 | |
| PROGNAME=$4 | |
| shift 4 | |
| exec sudo -u $USER -- "$PROGNAME" "$@" | |
| else | |
| set -e | |
| HOSTIP=$1 | |
| HOSTNAME=$2 | |
| PROGNAME="$3" | |
| TMPHOSTS=$(mktemp) | |
| cp /etc/hosts $TMPHOSTS | |
| echo "$HOSTIP $HOSTNAME" >> $TMPHOSTS | |
| echo "Temporary hosts file: $TMPHOSTS" | |
| shift 3 | |
| sudo unshare --mount $0 -- $TMPHOSTS $(whoami) "$PROGNAME" "$@" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment