-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_ip_at_dyndns_server
executable file
·66 lines (52 loc) · 1.56 KB
/
update_ip_at_dyndns_server
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
#!/bin/bash
network_prefix=".*"
lan_prefix="131.174"
dyndns_url="http://www.cs.ru.nl/lab/dyndns/"
get_mac_for_interface()
{
# ifconfig $1 | grep HWaddr | sed 's/.*HWaddr //';
ip -4 link show $1 | grep -oP '(?<=link/ether\s)[:0123456789abcdef]*'
}
get_ip_for_interface()
{
#ifconfig $1 | grep 'inet addr:' | sed 's/.*inet addr://'| sed 's/ Bcast:.*//';
ip -4 addr show $1 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
}
getipfrommac ()
{
curl "$dyndns_url?mac=$1&cmdline=1" 2>/dev/null
}
# only allow setting ip within network which starts with network_prefix set
setmac2ip ()
{
if [[ "$2" =~ ^$network_prefix || "$2" =~ ^$lan_prefix ]]
then
curl "$dyndns_url?mac=$1&ip=$2&cmdline=1" 2>/dev/null
fi
}
update_dyndns_for_interface() {
interface=$1
if ! ifconfig $interface >& /dev/null
then
echo "interface:$interface not defined"
return
fi
mac=$(get_mac_for_interface $interface)
ip=$(get_ip_for_interface $interface)
# notes:
# ip=$(hostname -I) => displays all interfaces ip's
# mac=$(cat /sys/class/net/eth0/address) => nothing similar for mac
#echo "dyndns: update interface $interface : $(setmac2ip $mac $ip )"
setmac2ip $mac $ip >/dev/null
echo "interface:$interface mac:$mac ip:$ip"
}
# example
# $ setmac2ip b8:27:eb:fc:c9:ca 131.174.142.31
# $ getipfrommac b8:27:eb:fc:c9:ca
# 131.174.142.31
# $ update_dyndns
# $ getipfrommac b8:27:eb:fc:c9:ca
# 131.174.142.175
echo ""
update_dyndns_for_interface "eth0 "
update_dyndns_for_interface "wlan0"