-
-
Save pocki80/eeea81945111ac14b937bd46b83412d2 to your computer and use it in GitHub Desktop.
Place "link" shortcut to query olsrd-info plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat >/config/scripts/post-config.d/linkscriptsetup.sh <<'ENDINSTALLCONTENT' | |
#!/bin/bash | |
# install link script file if missing | |
cat >/tmp/linkscriptfile <<'ENDSCRIPTCONTENT' | |
#!/bin/bash | |
# OLSRD Links | |
# /usr/local/bin/links | |
ip=127.0.0.1 | |
timestamp=$(date +%s) | |
function get_name { | |
local age=$(grep -w "$1" /tmp/links_hostnames 2>/dev/null | cut -d" " -f3) | |
[ "$age" ] && [ $(($timestamp - $age)) -le 2592000 ] && echo "$(grep -w "$1" /tmp/links_hostnames 2>/dev/null | cut -d" " -f2)" && return | |
host=$(host -W 1 -R 0 -t PTR $1 2>/dev/null | awk '/wien.funkfeuer.at/&&!/#/{gsub(".$","");print $NF}' | sed -e 's/.wien.funkfeuer.at//g'); | |
[ "$host" ] && (sed -i '/^'"$1"' /d' /tmp/links_hostnames 2>/dev/null; echo "$1 $host $timestamp" >>/tmp/links_hostnames) | |
echo "$host" | |
} | |
if [[ $(pgrep -x olsrd) ]] && [[ "$1" != "6" ]]; then | |
[ -f /etc/default/olsrd ] && conffile=$(awk -F"-f " '/DAEMON_OPTS/{print $2}' /etc/default/olsrd | tr -d \") | |
[ "$conffile" ] || conffile="/etc/olsrd/olsrd.conf" | |
conf=$(sed -n "/^LoadPlugin \"olsrd_jsoninfo.so./,/^}/p" $conffile | cut -d"#" -f1 | sed '/^ *$/d') | |
if [ "$conf" ]; then | |
port=$(echo "$conf" | grep -i "port" | awk '{print $3}' | tr -d '"') | |
[ "$port" ] || port=9090 | |
echo "olsrd links from jsoninfo_plugin" | |
echo "Int Local IP Remote IP LQ NLQ Cost #rts def? Remotehost" && | |
for line in $(for l in $(curl -s $ip:$port/links | jq -r '.["links"][] | "\(.localIP)#\(.remoteIP)#\(.hysteresis)#\(.linkQuality)#\(.neighborLinkQuality)#\(.linkCost)#\(.olsrInterface)"' 2>/dev/null); do echo $l | | |
awk -F"#" '{printf "%s,%s,%s,%7.3f,%7.3f,%7.3f,%s\n", $1,$2,$3,$4,$5,$6,$7}' | sed -e 's/ //ig'; done | sort); do | |
n=$(echo $line | awk -F"," '{print $2}') | |
int=$(echo $line | awk -F"," '{print $7}') | |
(/sbin/ip -4 route | grep -E "default|0\.0\.0\.0/1" | sed -e 's/0\.0\.0\.0\/1/default/g' | awk '!x[$5]++' | grep -q $n) && d="def" || d='-' | |
c=$(/sbin/ip -4 route | grep onlink | grep -wc "via $n dev $int") | |
h=$(get_name "$n") | |
echo $d","$line","$c",$h" | awk -F"," '{printf "%-10s%-17s%-17s%8s%8s%8s %4s %-5s%s\n", $8,$2,$3,$5,$6,($7>99 ? "inf" : $7),$9,$1,$10}' | |
done | |
else | |
conf=$(sed -n "/^LoadPlugin \"olsrd_txtinfo.so./,/^}/p" $conffile | cut -d"#" -f1 | sed '/^ *$/d') | |
if [ "$conf" ]; then | |
port=$(echo "$conf" | grep -i "port" | awk '{print $3}' | tr -d '"') | |
[ "$port" ] || port=2006 | |
echo "olsrd links from txtinfo_plugin" | |
echo "Local IP Remote IP LQ NLQ Cost #rts def? Remotehost" | |
for line in $(curl -s $ip:$port/lin | grep -E "^[0-9]" | awk '{printf "%s,%s,%s,%s,%s,%s\n",$1,$2,$3,$4,$5,$6}' | sed -e 's/INFINITE/INFT/ig' | sort); do | |
n=$(echo $line | awk -F"," '{print $2}') | |
(/sbin/ip -4 route | grep -E "default|0\.0\.0\.0/1" | sed -e 's/0\.0\.0\.0\/1/default/g' | awk '!x[$5]++' | grep -q $n) && d="def" || d='-' | |
c=$(/sbin/ip -4 route | grep onlink | grep -wc "via $n") | |
h=$(get_name "$n") | |
echo $d","$line","$c",$h" | awk -F"," '{printf "%-17s%-17s%8s%8s%8s %4s %-5s%s\n", $2,$3,$5,$6,$7,$8,$1,$9}' | |
done | |
else | |
conf=$(sed -n "/^LoadPlugin \"olsrd_httpinfo.so./,/^}/p" $conffile | cut -d"#" -f1 | sed '/^ *$/d') | |
if [ "$conf" ]; then | |
port=$(echo "$conf" | grep -i "port" | awk '{print $3}' | tr -d '"') | |
[ "$port" ] || port=1978 | |
echo "olsrd links from httpinfo_plugin" | |
echo "Local IP Remote IP LQ NLQ Cost #rts def? Remotehost" | |
for line in $(curl -s $ip:$port/nodes | sed -n "/^<h2>Links/,/^<h2>Neighbors/p" | sed -e 's/[\/)( <>]/#/g' | awk -F"#" '/all/ {printf "%s,%s,%s,%s,%s,%s\n", $11,$25,$33,$39,$40,$42}' | | |
sed -e 's/INFINITE/INFT/ig' | sort); do | |
n=$(echo $line | awk -F"," '{print $2}') | |
(/sbin/ip -4 route | grep -E "default|0\.0\.0\.0/1" | sed -e 's/0\.0\.0\.0\/1/default/g' | awk '!x[$5]++' | grep -q $n) && d="def" || d='-' | |
c=$(/sbin/ip -4 route | grep onlink | grep -wc "via $n") | |
h=$(get_name "$n") | |
echo $d","$line","$c",$h" | awk -F"," '{printf "%-17s%-17s%8s%8s%8s %4s %-5s%s\n", $2,$3,$5,$6,$7,$8,$1,$9}' | |
done | |
else | |
echo "olsrd links recognized from local routing table" | |
echo "Int Remote IP Cnt-Routes def? Remotehost" && | |
for line in $(/sbin/ip -4 route | grep onlink | awk '!x[$3]++ {print $5":"$3}' | sort); do | |
n=$(echo $line | cut -d":" -f2); i=$(echo $line | cut -d":" -f1) | |
(/sbin/ip -4 route | grep -E "default|0\.0\.0\.0/1" | sed -e 's/0\.0\.0\.0\/1/default/g' | awk '!x[$5]++' | grep -q $n) && d="def" || d='-' | |
h=$(get_name "$n") | |
(echo -n $i $n $d" "$(/sbin/ip -4 route | grep onlink | grep -wc "via $n")" $h") | awk '{printf "%-10s%-17s%3s routes %-5s%s\n", $1,$2,$4,$3,$5}' | |
done | |
fi | |
fi | |
fi | |
else | |
echo "olsrd not running" | |
fi | |
# OLSRD2 Links | |
if [[ $(pgrep -x olsrd2) ]] && [[ "$1" != "4" ]]; then | |
[[ $(pgrep -x olsrd) ]] && echo "" | |
echo "olsrd2 links from nhdpinfo" | |
echo "Int Remote IPv6 MAC-Addr MetricIn #rts def? Remotehost" && | |
for line in $(curl -s $ip:8000/telnet/nhdpinfo%20link | awk '{print $1","$14","$2","$10","$18","$8}'); do | |
n=$(echo $line | cut -d"," -f2); i=$(echo $line | cut -d"," -f1); s=$(echo $line | cut -d"," -f6) | |
m=$(echo $line | cut -d"," -f4); x=$(echo $line | cut -d"," -f5 | sed 's/bit\/s//g') | |
[ "$n" == "-" ] && n=$(echo $line | cut -d"," -f3) | |
f=$(ip -6 r | grep "$n dev $i" | head -1 | awk '{print $3}'); [ "$f" ] || f=$(echo $line | cut -d"," -f3) | |
(/sbin/ip -6 route | grep default | grep "$i " | grep -q $f) && d="def" || d='-' | |
h=$(get_name "$n") | |
(echo -n $i $n $d $m $x" "$(/sbin/ip -6 route | grep -wc $f)" ${s:0:3} $h") | awk '{printf "%-10s%-36s%-18s%9s%5s %s %-5s%s\n", $1,$2,$4,$5,$6,$7,$3,$8'} | |
done | |
fi | |
ENDSCRIPTCONTENT | |
if [ ! -f /usr/local/bin/links ]; then | |
#echo "initial install" | |
[ -d /usr/local ] || mkdir /usr/local | |
[ -d /usr/local/bin ] || mkdir /usr/local/bin | |
cp /tmp/linkscriptfile /usr/local/bin/links | |
elif [ $(sha1sum /tmp/linkscriptfile | awk {'print $1'}) != $(sha1sum /usr/local/bin/links | awk {'print $1'}) ]; then | |
#echo "updated file" | |
cp /tmp/linkscriptfile /usr/local/bin/links | |
#else | |
#echo "nothing to do" | |
fi | |
if [ -f /usr/local/bin/links ] && [ ! -x /usr/local/bin/links ]; then | |
echo "chmod 755 file" | |
chmod 755 /usr/local/bin/links | |
fi | |
(grep -q "/usr/local/bin/links" /root/.bashrc) || echo "alias links='/usr/local/bin/links'" >>/root/.bashrc | |
(grep -qE "alias ll=.ls -al." /root/.bashrc) || echo "alias ll='ls -al'" >>/root/.bashrc | |
rm /tmp/linkscriptfile | |
exit 0 | |
ENDINSTALLCONTENT | |
chmod 755 /config/scripts/post-config.d/linkscriptsetup.sh | |
/config/scripts/post-config.d/linkscriptsetup.sh | |
echo "done" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment