-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper
executable file
·101 lines (94 loc) · 2.73 KB
/
helper
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
#
# A helper script to simplify the collection of a few host-specific items...
#
# ./helper -a
# - returns the appropriate hardware architecture for use with open-horizon
# ./helper -g
# - returns the IP address of the first default gateway for this host
# ./helper -i
# - returns one of the IP address on the first default interface for this host
# ./helper -z
# - returns all of the above (really just for development convenience)
#
DEBUG=0
if [ ${DEBUG} -gt 0 ]; then echo "DEBUG is on."; fi
# Return the Horizon architecture string for this platform
show_architecture () {
if [ ${DEBUG} -gt 0 ]; then echo "Architecture."; fi
RAW_ARCH=$(uname -m)
if [[ "$RAW_ARCH" == "x86_64" ]]; then
ARCH=amd64
elif [[ "$RAW_ARCH" == "i386" ]]; then
ARCH=amd64
elif [[ "$RAW_ARCH" == "aarch64" ]]; then
ARCH=arm64
elif [[ "$RAW_ARCH" == "arm"* ]]; then
ARCH=arm
else
# Other. Fail!
ARCH=error
fi
echo "${ARCH}"
}
# Return the default gateway address for this host
show_gateway () {
if [ ${DEBUG} -gt 0 ]; then echo "Gateway Address."; fi
if [[ "$OSTYPE" == "linux-"* ]]; then
# Linux
GATEWAY=$(ip route | grep default | head -1 | cut -d' ' -f3)
elif [[ "$OSTYPE" == "darwin"* ]]; then
# MacOSX
GATEWAY=$(netstat -nr | grep default | head -1 | awk '{print$2}')
else
# Other. Best guess:
GATEWAY=$(ip route | grep default | head -1 | cut -d' ' -f3)
fi
echo "${GATEWAY}"
}
# Return the default interface's IP address for this host
show_ip () {
if [ ${DEBUG} -gt 0 ]; then echo "IP Address."; fi
if [[ "$OSTYPE" == "linux-"* ]]; then
# Linux
CIDR=$(ip route | grep default | head -1 | sed 's/ proto dhcp / /' | cut -d' ' -f3 | cut -d'.' -f1-3)'.0/24'
if [ ${DEBUG} -gt 0 ]; then echo "CIDR=${CIDR}"; fi
HOST_IP=$(ip route | grep "${CIDR}" | head -1 | sed 's/ proto [a-z]*//' | cut -d' ' -f7)
elif [[ "$OSTYPE" == "darwin"* ]]; then
# MacOSX
HOST_IP=$(host `hostname` | head -1 | sed 's/.* //')
else
# Other. Best guess:
CIDR=$(ip route | grep default | head -1 | sed 's/ proto dhcp / /' | cut -d' ' -f3 | cut -d'.' -f1-3)'.0/24'
if [ ${DEBUG} -gt 0 ]; then echo "CIDR=${CIDR}"; fi
HOST_IP=$(ip route | grep "${CIDR}" | head -1 | sed 's/ proto [a-z]*//' | cut -d' ' -f7)
fi
echo "${HOST_IP}"
}
while getopts ":agiz" opt; do
case ${opt} in
a )
show_architecture;
exit 0;
;;
g )
show_gateway;
exit 0;
;;
i )
show_ip;
exit 0;
;;
z )
echo -n "Architecture: ";
show_architecture;
echo -n "Gateway: ";
show_gateway;
echo -n "IP Address: ";
show_ip;
exit 0;
;;
\? ) echo "Usage: $0 [-a|-g|-i|-z]"
;;
esac
done