-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.sh
executable file
·85 lines (72 loc) · 1.5 KB
/
ip.sh
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
#!/usr/bin/env bash
version="1.0.1"
# Verbose by default
if [ -t 1 ]; then
verbose=1
else
verbose=
fi
# Usage information
usage() {
cat <<EOF
Usage: ip [options]
Options:
-q, --quiet only output results
-V, --version output version number
-h, --help output usage information
EOF
}
# Parse options
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
case $1 in
-V | --version )
echo $version
exit
;;
-q | --quiet )
verbose=
;;
-h | --help )
usage
exit
;;
esac
shift
done
if [[ "$1" == "--" ]]; then shift; fi
# Check operating system
if [ "$(uname)" == "Darwin" ]; then
macOS=1
linux=
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
macOS=
linux=1
else
echo -e "\033[91m ✗ Unsupported operating system! \033[0m"
exit 1
fi
# Check internet connectivity
if ! ping -c 1 google.com >> /dev/null 2>&1; then
echo -e "\033[91m ✗ You're offline! \033[0m"
exit 1
fi
# Print loading message
if [ $verbose ]; then
echo ""
echo -e "\033[90m Getting internal & external IP addresses… \033[0m"
echo ""
fi
if [ $macOS ]; then
internal=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}')
elif [ $linux ]; then
internal=$(hostname -I)
fi
external=$(dig +short myip.opendns.com @resolver1.opendns.com)
# Print results
if [ $verbose ]; then
echo -e "\033[96m ✓ Internal IP: $internal \033[0m"
echo -e "\033[96m ✓ External IP: $external \033[0m"
else
echo $internal
echo $external
fi