-
Notifications
You must be signed in to change notification settings - Fork 3
/
y-localhost
executable file
·38 lines (30 loc) · 1 KB
/
y-localhost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env bash
[ -z "$DEBUG" ] || set -x
set -eo pipefail
[ "$1" = "help" ] && echo '
Tries to bring up a loopback interface so port bindings can be done without conflicts
' && exit 0
hostname=$1
[ -z "$hostname" ] && echo "First arg must be a hostname" && exit 1
platform="$(uname -s)"
ip=$(cat /etc/hosts | grep -e "[[:blank:]]\<$hostname\>" | awk '{ print $1 }')
[ -z "$ip" ] && >&2 echo "Host '$hostname' not found in /etc/hosts" && exit 1
[ "$2" = "show" ] && echo -n "$ip" && exit 0
num=$(echo -n $ip | cut -d '.' -f 4)
case "$platform" in
Darwin)
ifconfig lo0 | grep $ip > /dev/null && exit 0
echo "Loopback alias for $ip $hostname not found. Will try to create ..."
[ $(id -u) -eq 0 ] || exec sudo -E $0 "$@"
ifconfig lo0 alias $ip up
;;
Linux)
if [ -z "$(ip addr show dev lo label lo:$num)" ]; then
echo "Adding missing interface lo:$num ..."
ip addr add $ip/32 dev lo label lo:$num
fi
;;
*)
echo "Unsupported platform $platform" && exit 1
;;
esac