From 1459693650a3a1512aea60367e807fdc4dfb1246 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 26 Jun 2016 09:55:31 +0100 Subject: [PATCH] Verify that batman is running after start --- src/freedombone-mesh-batman | 265 +++++++++++++++++++----------------- 1 file changed, 140 insertions(+), 125 deletions(-) diff --git a/src/freedombone-mesh-batman b/src/freedombone-mesh-batman index 96f12409..019149fc 100755 --- a/src/freedombone-mesh-batman +++ b/src/freedombone-mesh-batman @@ -32,21 +32,21 @@ PROJECT_NAME='freedombone' COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt if [[ $1 == "start" ]]; then - # install avahi - sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf - sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf - sed -i "s|use-ipv4=.*|use-ipv4=yes|g" /etc/avahi/avahi-daemon.conf - sed -i "s|use-ipv6=.*|use-ipv6=no|g" /etc/avahi/avahi-daemon.conf - sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" /etc/avahi/avahi-daemon.conf - sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf + # install avahi + sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf + sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf + sed -i "s|use-ipv4=.*|use-ipv4=yes|g" /etc/avahi/avahi-daemon.conf + sed -i "s|use-ipv6=.*|use-ipv6=no|g" /etc/avahi/avahi-daemon.conf + sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" /etc/avahi/avahi-daemon.conf + sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf fi # Mesh definition WIFI_SSID= if ! grep -q "WIFI_SSID:" $COMPLETION_FILE; then - WIFI_SSID='mesh' + WIFI_SSID='mesh' else - WIFI_SSID=$(cat $COMPLETION_FILE | grep "WIFI_SSID:" | awk -F ':' '{print $2}') + WIFI_SSID=$(cat $COMPLETION_FILE | grep "WIFI_SSID:" | awk -F ':' '{print $2}') fi sed -i "s|WIFI_SSID:.*|WIFI_SSID:${WIFI_SSID}|g" $COMPLETION_FILE @@ -54,9 +54,9 @@ CELLID='any' CHANNEL= if ! grep -q "Wifi channel:" $COMPLETION_FILE; then - CHANNEL=2 + CHANNEL=2 else - CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}') + CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}') fi sed -i "s|Wifi channel:.*|Wifi channel:${CHANNEL}|g" $COMPLETION_FILE @@ -69,151 +69,166 @@ IFACE='wlan0' EIFACE=eth0 if [[ $IFACE == "wlan0" ]]; then - if grep -q "wlan1" /proc/net/dev; then - IFACE=wlan1 - fi + if grep -q "wlan1" /proc/net/dev; then + IFACE=wlan1 + fi fi if [[ $IFACE == "wlan0" ]]; then - if grep -q "wlan2" /proc/net/dev; then - IFACE=wlan2 - fi + if grep -q "wlan2" /proc/net/dev; then + IFACE=wlan2 + fi fi if [[ $IFACE == "wlan0" ]]; then - if grep -q "wlan3" /proc/net/dev; then - IFACE=wlan3 - fi + if grep -q "wlan3" /proc/net/dev; then + IFACE=wlan3 + fi fi if [ -e /etc/default/batctl ]; then - . /etc/default/batctl + . /etc/default/batctl fi -start() { - if [ -z "$IFACE" ] ; then - echo 'error: unable to find wifi interface, not enabling batman-adv mesh' - return - fi - echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE" +function status { + batctl o +} - systemctl stop network-manager - sleep 5 +function verify { + tempfile="$(mktemp)" + batctl o > $tempfile + if grep -q "disabled" $tempfile; then + echo $'B.A.T.M.A.N. not enabled' + rm $tempfile + exit 726835 + fi + echo $'B.A.T.M.A.N. is running' + rm $tempfile +} - # remove an avahi service which isn't used - if [ -f /etc/avahi/services/udisks.service ]; then - sudo rm /etc/avahi/services/udisks.service - fi +function start { + if [ -z "$IFACE" ] ; then + echo 'error: unable to find wifi interface, not enabling batman-adv mesh' + exit 723657 + fi + echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE" - # Might have to re-enable wifi - rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true + systemctl stop network-manager + sleep 5 - ifconfig $IFACE down - ifconfig $IFACE mtu 1532 - iwconfig $IFACE enc off - iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL - sleep 1 - iwconfig $IFACE ap $CELLID + # remove an avahi service which isn't used + if [ -f /etc/avahi/services/udisks.service ]; then + sudo rm /etc/avahi/services/udisks.service + fi - modprobe batman-adv - batctl if add $IFACE - ifconfig $IFACE up - avahi-autoipd --force-bind --daemonize --wait $BRIDGE - avahi-autoipd --force-bind --daemonize --wait $IFACE - ifconfig bat0 up promisc + # Might have to re-enable wifi + rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true - #Use persistent HWAddr - ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //") - if [ ! -f /var/lib/mesh-node/bat0 ]; then - mkdir /var/lib/mesh-node - echo "${ether_new}" > /var/lib/mesh-node/bat0 - else - ether=$(cat /var/lib/mesh-node/bat0) - ifconfig bat0 hw ether ${ether} - fi + ifconfig $IFACE down + ifconfig $IFACE mtu 1532 + iwconfig $IFACE enc off + iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL + sleep 1 + iwconfig $IFACE ap $CELLID - if [ "$EIFACE" ] ; then - brctl addbr $BRIDGE - brctl addif $BRIDGE bat0 - brctl addif $BRIDGE $EIFACE - ifconfig bat0 0.0.0.0 - ifconfig $EIFACE 0.0.0.0 - ifconfig $EIFACE up promisc - ifconfig $BRIDGE up - fi + modprobe batman-adv + batctl if add $IFACE + ifconfig $IFACE up + avahi-autoipd --force-bind --daemonize --wait $BRIDGE + avahi-autoipd --force-bind --daemonize --wait $IFACE + ifconfig bat0 up promisc - iptables -A INPUT -p tcp --dport 548 -j ACCEPT - iptables -A INPUT -p udp --dport 548 -j ACCEPT - iptables -A INPUT -p tcp --dport 5353 -j ACCEPT - iptables -A INPUT -p udp --dport 5353 -j ACCEPT - iptables -A INPUT -p tcp --dport 5354 -j ACCEPT - iptables -A INPUT -p udp --dport 5354 -j ACCEPT - iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT - iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT - iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT + #Use persistent HWAddr + ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //") + if [ ! -f /var/lib/mesh-node/bat0 ]; then + mkdir /var/lib/mesh-node + echo "${ether_new}" > /var/lib/mesh-node/bat0 + else + ether=$(cat /var/lib/mesh-node/bat0) + ifconfig bat0 hw ether ${ether} + fi - systemctl restart avahi-daemon + if [ "$EIFACE" ] ; then + brctl addbr $BRIDGE + brctl addif $BRIDGE bat0 + brctl addif $BRIDGE $EIFACE + ifconfig bat0 0.0.0.0 + ifconfig $EIFACE 0.0.0.0 + ifconfig $EIFACE up promisc + ifconfig $BRIDGE up + fi + + iptables -A INPUT -p tcp --dport 548 -j ACCEPT + iptables -A INPUT -p udp --dport 548 -j ACCEPT + iptables -A INPUT -p tcp --dport 5353 -j ACCEPT + iptables -A INPUT -p udp --dport 5353 -j ACCEPT + iptables -A INPUT -p tcp --dport 5354 -j ACCEPT + iptables -A INPUT -p udp --dport 5354 -j ACCEPT + iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT + iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT + iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT + + systemctl restart avahi-daemon + + verify } stop() { - if [ -z "$IFACE" ]; then - echo 'error: unable to find wifi interface, not enabling batman-adv mesh' - return - fi - if [ "$EIFACE" ]; then - brctl delif $BRIDGE bat0 - brctl delif $BRIDGE $EIFACE - ifconfig $BRIDGE down || true - brctl delbr $BRIDGE - ifconfig $EIFACE down -promisc - fi + if [ -z "$IFACE" ]; then + echo 'error: unable to find wifi interface, not enabling batman-adv mesh' + return + fi + if [ "$EIFACE" ]; then + brctl delif $BRIDGE bat0 + brctl delif $BRIDGE $EIFACE + ifconfig $BRIDGE down || true + brctl delbr $BRIDGE + ifconfig $EIFACE down -promisc + fi - avahi-autoipd -k $BRIDGE - avahi-autoipd -k $IFACE - ifconfig bat0 down -promisc + avahi-autoipd -k $BRIDGE + avahi-autoipd -k $IFACE + ifconfig bat0 down -promisc - batctl if del $IFACE - rmmod batman-adv - ifconfig $IFACE mtu 1500 - ifconfig $IFACE down - iwconfig $IFACE mode managed + batctl if del $IFACE + rmmod batman-adv + ifconfig $IFACE mtu 1500 + ifconfig $IFACE down + iwconfig $IFACE mode managed - iptables -D INPUT -p tcp --dport 548 -j ACCEPT - iptables -D INPUT -p udp --dport 548 -j ACCEPT - iptables -D INPUT -p tcp --dport 5353 -j ACCEPT - iptables -D INPUT -p udp --dport 5353 -j ACCEPT - iptables -D INPUT -p tcp --dport 5354 -j ACCEPT - iptables -D INPUT -p udp --dport 5354 -j ACCEPT - iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT - iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT - iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT + iptables -D INPUT -p tcp --dport 548 -j ACCEPT + iptables -D INPUT -p udp --dport 548 -j ACCEPT + iptables -D INPUT -p tcp --dport 5353 -j ACCEPT + iptables -D INPUT -p udp --dport 5353 -j ACCEPT + iptables -D INPUT -p tcp --dport 5354 -j ACCEPT + iptables -D INPUT -p udp --dport 5354 -j ACCEPT + iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT + iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT + iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT - systemctl restart network-manager + systemctl restart network-manager } if ! grep -q "$IFACE" /proc/net/dev; then - echo 'Interface $IFACE was not found' - stop - exit 1 + echo 'Interface $IFACE was not found' + stop + exit 1 fi case "$1" in - start|stop) - $1 - ;; - restart) - stop - sleep 10 - start - ;; - status) - batctl o - ;; - ping) - batctl ping $2 - ;; - ls|list) - avahi-browse -atl - ;; - *) + start|stop|status) + $1 + ;; + restart) + stop + sleep 10 + start + ;; + ping) + batctl ping $2 + ;; + ls|list) + avahi-browse -atl + ;; + *) echo "error: invalid parameter $1" echo 'usage: $0 {start|stop|restart|status|ping|ls|list}' exit 2