-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipup2d8.sh
executable file
·90 lines (69 loc) · 2.33 KB
/
ipup2d8.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
86
87
88
89
90
#!/usr/bin/env bash
# Usage: ipup2d8
# ipup2d8 1
# ipup2d8 0 en1
# $1: flag (0 or 1) indicating whether we are behind a NAT firewall (1) or not (0)
# $2: name of local network interface to get WAN IP address from. $1 must be 0.
# This script will periodically check your IP address and pass it to each script
# you place in the push-ip.d directory.
################################
# User configuration section
################################
# If you are usually behind a NAT router:
IS_NAT="${1:-1}"
# Otherwise:
#IS_NAT="${1:-0}"
# The name of the local network interface you want to obtain your WAN IP address
# from:
# Be sure to change the name of the network interface to the one that
# YOU are using. It's usually wlan0 or eth0. Use ifconfig to find out.
INTERFACE="${2:-eth0}"
# TODO Randomize
WAN_IP_RESOLVER=ifconfig.me
# WAN_IP_RESOLVER=ifconfig.me
# WAN_IP_RESOLVER=icanhazip.com
# WAN_IP_RESOLVER=ident.me
# WAN_IP_RESOLVER=ipecho.net/plain
# WAN_IP_RESOLVER=whatismyip.akamai.com
# WAN_IP_RESOLVER=tnx.nl/ip
# WAN_IP_RESOLVER=myip.dnsomatic.com
# WAN_IP_RESOLVER=ip.appspot.com
################################
# End user configuration section
################################
die () {
echo $@ 1>&2
exit 1
}
IPUP2D8_DIR="${HOME}/.ipup2d8"
IPUP2D8_FILE="${IPUP2D8_DIR}/ip"
TASKS_DIR="${IPUP2D8_DIR}/push-ip.d"
[ -d "$IPUP2D8_DIR" ] || die "No directory named $IPUP2D8_DIR found"
[ -d "$TASKS_DIR" ] || die "No directory named $TASKS_DIR found"
cd "$IPUP2D8_DIR"
touch "$IPUP2D8_FILE"
OLD_IP=`cat "$IPUP2D8_FILE"`
IP="" # store IP
if [ "$IS_NAT" = 0 ]; then
case `uname` in
Linux)
IP=`ifconfig -a "$INTERFACE" | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
;;
FreeBSD|OpenBSD)
IP=`ifconfig "$INTERFACE" | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'`
;;
SunOS)
IP=`ifconfig -a "$INTERFACE" | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '`
;;
Darwin)
IP=`ifconfig -u "$INTERFACE" | grep -v 'inet 127.0.0.1' | sed -n 's/\s*inet \(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\) .*/\1/p'`
;;
esac
else
IP=`curl "$WAN_IP_RESOLVER"`
fi
[ "$IP" = "$OLD_IP" ] && exit 0
echo "$IP" > "$IPUP2D8_FILE"
for TASK in `ls "$TASKS_DIR"`; do
"$TASK" "$IP"
done