468ed7a3e07b532725b78ead574336f525ceeb33
[project/luci.git] / contrib / package / freifunk-common / files / usr / bin / neigh.sh
1 #!/bin/sh
2
3 . /usr/share/libubox/jshn.sh
4
5 hostsfile_getname()
6 {
7         local config="$1"
8         local i=0
9         local value file
10
11         while value="$( uci -q get $config.@LoadPlugin[$i].library )"; do {
12                 case "$value" in
13                         'olsrd_nameservice.so.'*)
14                                 file="$( uci -q get $config.@LoadPlugin[$i].hosts_file )"
15                                 break
16                         ;;
17                 esac
18
19                 i=$(( i + 1 ))
20         } done
21
22         echo "${file:-/var/run/hosts_olsr}"
23 }
24
25 read_hostnames()
26 {
27         local file_list=" $( hostsfile_getname 'olsrd' ) $(hostsfile_getname 'olsrd6' ) "
28         local line ip hostname file file_list_uniq
29
30         for file in $file_list; do {
31                 case " $file_list_uniq " in
32                         *" $file "*)
33                         ;;
34                         *)
35                                 file_list_uniq="$file_list_uniq $file"
36                         ;;
37                 esac
38         } done
39
40         for file in $file_list_uniq; do {
41                 [ -e "$file" ] || continue
42
43                 while read -r line; do {
44                         case "$line" in
45                                 [0-9]*)
46                                         # 2001:bf7:820:901::1 stuttgarter-core.olsr   # myself
47                                         # 10.63.160.161  AlexLaterne    # 10.63.160.161
48                                         set -f
49                                         set +f -- $line
50                                         ip="$1"
51                                         hostname="$2"
52
53                                         # global vars, e.g.
54                                         # IP_1_2_3_4='foo' or IP_2001_bf7_820_901__1='bar'
55                                         eval IP_${ip//[.:]/_}="$hostname"
56                                 ;;
57                         esac
58                 } done <"$file"
59         } done
60 }
61
62 read_hostnames
63
64 VARS='localIP:Local remoteIP:Remote validityTime:vTime linkQuality:LQ'
65 VARS="$VARS neighborLinkQuality:NLQ linkCost:Cost remoteHostname:Host"
66
67 for HOST in '127.0.0.1' '::1';do
68         json_init
69         json_load "$( echo /links | nc $HOST 9090 | sed -n '/^[}{ ]/p' )"       # remove header/non-json
70
71         if json_is_a links array;then
72                 json_select links
73                 for v in ${VARS};do
74                         eval _${v%:*}=0
75                 done
76                 for j in 0 1;do
77                         case ${j} in 1)
78                                 for v in ${VARS};do
79                                         eval printf \"%-\${_${v%:*}}s \" ${v#*:}
80                                 done
81                                 echo
82                         ;;esac
83                         i=1;while json_is_a ${i} object;do
84                                 json_select ${i}
85                                 json_get_vars $(for v in ${VARS};do echo ${v%:*};done)
86                                 case ${j} in 0)
87                                         for v in ${VARS};do
88                                                 eval "test \${_${v%:*}} -lt \${#${v%:*}} && _${v%:*}=\${#${v%:*}}"
89                                         done
90                                 ;;*)
91                                         for v in ${VARS};do
92                                                 eval printf \"%-\${_${v%:*}}s \" \$${v%:*}
93                                                 eval remoteHostname="\$IP_${remoteIP//[.:]/_}"
94                                         done
95                                         echo
96                                 ;;esac
97                                 json_select ..
98                                 i=$(( i + 1 ))
99                         done
100                 done
101         fi
102         echo
103 done