-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.he.net_updater.sh
executable file
·105 lines (90 loc) · 3.13 KB
/
dns.he.net_updater.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
USAGE="Usage:
$(basename $0) -h <fqdn> -p <password> [-l] [i <iface>]
$(basename $0) --help
-h <fqdn> : The hostname to update
-p <password> : The password to use
-l : Set using local interface address
-i <iface> : The interface to use when getting local address. Implies -l
By default, $(basename $0) queries dns.he.net to determine the public IP address.
When called with -l or -i, it uses the address of a local interface instead.
"
# detect sed syntax
sed_ex_sw='-E' #default
for sw in '-r' '-E'; do
if echo 1 | sed "$sw" 's/1/2/' 2>&1 | grep --silent 2; then
sed_ex_sw="$sw"
break
fi
done
url="https://dyn.dns.he.net/nic/update"
previous_file_prefix=~/.dns.he.net
retval=0
hostname=''
password=''
use_local_iface_address='' # set this to 'yes' to use ifconfig to determine local IP addresses.
iface='eth0' # ignored unless $use_local_iface_address = yes
if [ "$1" == "--help" ]; then
printf "%s\n" "$USAGE"
exit 0
fi
while getopts 'h:p:li:h' optname; do
case "$optname" in
'h')
hostname="$OPTARG"
;;
'p')
password="$OPTARG"
;;
'l')
use_local_iface_address='yes'
;;
'i')
use_local_iface_address='yes'
iface="$OPTARG"
;;
':')
echo "No argument value for option $OPTARG"
printf "%s\n" "$USAGE"
exit 1
;;
*)
echo 'Unknown error while processing options'
printf "%s\n" "$USAGE"
exit 1
;;
esac
done
previous_file="${previous_file_prefix}.${hostname}"
previous=$(cat "${previous_file}" 2>/dev/null)
currentip=''
if [ "$use_local_iface_address" == "yes" ]; then
if which ip >/dev/null 2>&1; then
currentip=$(ip addr show dev "$iface" | grep inet\ .*scope\ global | sed "$sed_ex_sw" 's/[^0-9]*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/[0-9]{1,2}.*/\1/g')
elif which ifconfig >/dev/null 2>&1; then
currentip=$(ifconfig "$iface" inet | grep -E '^.*inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.*$' | sed "$sed_ex_sw" 's/^.*inet ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*$/\1/')
else
logger -i -t com.bennettp123.dyndns "${hostname}: could not determine local IP address"
retval=1
break
fi
else
currentip=$(curl -4 -x "${http_proxy}" -s "http://checkip.dns.he.net" | grep -iE "Your ?IP ?address ?is ?: ?" | sed "$sed_ex_sw" 's/.*\s+([[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}).*/\1/')
fi
oldip=$(echo "${previous}" \
| grep "${hostname}" \
| sed "$sed_ex_sw" 's/.*\ ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/\1/')
if [ "_$oldip" = "_" ]; then
oldip="unknown"
fi
if [ "_$currentip" != "_$oldip" ]; then
logger -i -t com.bennettp123.dyndns "${hostname}: old ip: ${oldip}; current ip: ${currentip}; updating..."
result=$(curl -k -4 -x "${http_proxy}" -s "${url}" -d "hostname=${hostname}" -d "password=${password}" -d "myip=${currentip}")
retval=$?
logger -i -t com.bennettp123.dyndns "${hostname}:${result}"
echo "${hostname}:${result}" > "${previous_file}"
else
logger -i -t com.bennettp123.dyndns "${hostname}: old ip: ${oldip}; current ip: ${currentip}; not updating"
retval=0
fi
exit $retval