freifunk-common: neigh.sh - show hostnames
[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)"
70         if json_is_a links array;then
71                 json_select links
72                 for v in ${VARS};do
73                         eval _${v%:*}=0
74                 done
75                 for j in 0 1;do
76                         case ${j} in 1)
77                                 for v in ${VARS};do
78                                         eval printf \"%-\${_${v%:*}}s \" ${v#*:}
79                                 done
80                                 echo
81                         ;;esac
82                         i=1;while json_is_a ${i} object;do
83                                 json_select ${i}
84                                 json_get_vars $(for v in ${VARS};do echo ${v%:*};done)
85                                 case ${j} in 0)
86                                         for v in ${VARS};do
87                                                 eval "test \${_${v%:*}} -lt \${#${v%:*}} && _${v%:*}=\${#${v%:*}}"
88                                         done
89                                 ;;*)
90                                         for v in ${VARS};do
91                                                 eval printf \"%-\${_${v%:*}}s \" \$${v%:*}
92                                                 eval remoteHostname="\$IP_${remoteIP//[.:]/_}"
93                                         done
94                                         echo
95                                 ;;esac
96                                 json_select ..
97                                 i=$(( i + 1 ))
98                         done
99                 done
100         fi
101         echo
102 done