forked from KusakabeShi/ospf-ibgp-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
68 lines (61 loc) · 1.56 KB
/
update.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
#!/bin/bash
resolveip()
{
: ${1:?Usage: resolve name}
(
PATH=$PATH:/usr/bin
lookupresult=$(getent ahostsv4 "$1")
if [ $? -eq 0 ]; then
resultaddr=$(echo $lookupresult | head -n 1 | awk '{print $1}')
echo $resultaddr
return 0
fi
lookupresult=$(getent ahostsv6 $1)
if [ $? -eq 0 ]; then
resultaddr=$(echo $lookupresult | head -n 1 | awk '{print $1}')
echo "[$resultaddr]"
return 0
fi
echo "0.0.0.0"
return 127
)
}
get_wg_peer_down () {
#1:name, 2:peer key, 3:peer endpoint, 4:confpath
current_time=$(date +%s)
last_handshake=$(wg show "$1" latest-handshakes | grep "$2" | awk "{print \$2}")
if [ -z "$last_handshake" ]; then
last_handshake=0
wg setconf "$1" "$4"
fi
last_to_now=$(("$current_time"-"$last_handshake"))
if [ "$last_to_now" -gt "180" ]; then
return 0 #success, means down
fi
return 1 #fail, means up
}
update_wg_peer() {
if get_wg_peer_down "$1" "$2" "$3" "$4"; then
wg set "$1" listen-port "5$(head /dev/urandom | tr -dc "0123456789" | head -c4)"
wg set "$1" peer "$2" endpoint "$3"
fi
}
get_ip_down () {
#1:ip 2:ifname
if ! ping -c 1 -W 3 "$1" -I "$2" >/dev/null
then
return 0 #success, means down
fi
return 1 # fail. means up
}
{% for resolvip in resolvips -%}
{{ resolvip }}
{% endfor %}
{% for con in reconns -%}
if get_ip_down "{{ con["ip"] }}" "{{ con["ifname"] }}"; then
{{ con["script"] }}
fi
{% endfor %}
{% for update in updates -%}
{{ update }}
{% endfor %}