SSID ändern wenn kein Gateway in Range: Unterschied zwischen den Versionen

Aus Freifunk Franken
Wechseln zu:Navigation, Suche
Keine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
 
(4 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
Es kommt immer mal wieder vor, das Router "in der Luft hängen" und keine Verbindung zum Freifunknetz haben. Bei anfälligen Routern macht es Sinn dieses Script zu installieren, dann wird die ausgestrahlte SSID geändert sobald kein Gateway (=DHCP Server) mehr in Reichweite ist. Man kann sich immernoch zum internen Freifunknetz verbinden, wird aber keine IP bekommen. Batman bleibt davon unberührt.
Es kommt immer mal wieder vor, dass Router "in der Luft hängen" und keine Verbindung zum Freifunknetz haben. Bei anfälligen Routern macht es Sinn dieses Script zu installieren, dann wird die ausgestrahlte SSID geändert sobald kein Gateway (=DHCP Server) mehr in Reichweite ist. Man kann sich immer noch zum internen Freifunknetz verbinden, wird aber keine IP bekommen. Batman bleibt davon unberührt. Daher funktioniert auch weiterhin der ssh Zugriff oder web-login - sofern Verbindung besteht.
 
Das unten beschriebene Skript basiert auf folgender Vorlage und wurde für Freifunk-franken angepasst: https://github.com/eulenfunk/packages/blob/master/gluon-ssid-changer/files/lib/gluon/ssid-changer/ssid-changer.sh
 
Mit diesem Script wird die SSID geändert in "FF_OFFLINE_"ROUTERNAME"".


<pre>
<pre>
vi /usr/sbin/ssidoffline  
vi /usr/sbin/ssidoffline  
</pre>
</pre>
 
i drücken zum Bearbeiten
<pre>
<pre>
#!/bin/sh
#!/bin/sh
if [ "$(batctl gwl | grep "No gateways in range")" != "" ] && [ "$(uci get wireless.w2ap.ssid)" = "franken.freifunk.net" ]
 
# At first some Definitions:
 
ONLINE_SSID=$(uci get wireless.w2ap.ssid -q)
: ${ONLINE_SSID:=franken.freifunk.net}  # if for whatever reason ONLINE_SSID is NULL
OFFLINE_PREFIX='FF_OFFLINE_' # Use something short to leave space for the nodename
 
UPPER_LIMIT='75' #Above this limit the online SSID will be used
LOWER_LIMIT='30' #Below this limit the offline SSID will be used
# In-between these two values the SSID will never be changed to prevent it from toggeling every Minute.
 
# Generate an Offline SSID with the first and last Part of the nodename to allow owner to recognise wich node is down
NODENAME=`uname -n`
if [ ${#NODENAME} -gt $((30 - ${#OFFLINE_PREFIX})) ] ; then #32 would be possible as well
HALF=$(( (28 - ${#OFFLINE_PREFIX} ) / 2 )) #calculate the length of the first part of the node identifier in the offline-ssid
SKIP=$(( ${#NODENAME} - $HALF )) #jump to this charakter for the last part of the name
OFFLINE_SSID=$OFFLINE_PREFIX${NODENAME:0:$HALF}...${NODENAME:$SKIP:${#NODENAME}} # use the first and last part of the nodename for nodes with long name
else
OFFLINE_SSID="$OFFLINE_PREFIX$NODENAME" #greate we are able to use the full nodename in the offline ssid
fi
 
#Is there an active Gateway?
GATEWAY_TQ=`batctl gwl | grep "^=>" | awk -F'[()]' '{print $2}'| tr -d " "` #Grep the Connection Quality of the Gateway which is currently used
if [ ! $GATEWAY_TQ ]; #If there is no gateway there will be errors in the following if clauses
then
GATEWAY_TQ=0 #Just an easy way to get an valid value if there is no gateway
fi
if [ $GATEWAY_TQ -gt $UPPER_LIMIT ];
then
echo "Gateway TQ is $GATEWAY_TQ node is online"
for HOSTAPD in $(ls /var/run/hostapd-phy*); do #Check status for all physical devices
CURRENT_SSID=`grep "^ssid=$ONLINE_SSID" $HOSTAPD | cut -d"=" -f2`
if [ $CURRENT_SSID == $ONLINE_SSID ]
then
echo "SSID $CURRENT_SSID is correct, nothing to do"
HUP_NEEDED=0
break
fi
CURRENT_SSID=`grep "^ssid=$OFFLINE_SSID" $HOSTAPD | cut -d"=" -f2`
if [ $CURRENT_SSID == $OFFLINE_SSID ]
then
logger -s -t "gluon-offline-ssid" -p 5 "TQ is $GATEWAY_TQ, SSID is $CURRENT_SSID, change to $ONLINE_SSID" #Write Info to Syslog
sed -i s/^ssid=$CURRENT_SSID/ssid=$ONLINE_SSID/ $HOSTAPD
HUP_NEEDED=1 # HUP here would be to early for dualband devices
else
echo "There is something wrong, did not find SSID $ONLINE_SSID or $OFFLINE_SSID"
fi
done
fi
if [ $GATEWAY_TQ -lt $LOWER_LIMIT ];
then
then
uci set wireless.w2ap.ssid='FFF_OFFLINE'
echo "Gateway TQ is $GATEWAY_TQ node is considered offline"
uci commit
for HOSTAPD in $(ls /var/run/hostapd-phy*); do #Check status for all physical devices
/etc/init.d/network restart
CURRENT_SSID=`grep "^ssid=$OFFLINE_SSID" $HOSTAPD | cut -d"=" -f2`
if [ $CURRENT_SSID == $OFFLINE_SSID ]
then
echo "SSID $CURRENT_SSID is correct, noting to do"
HUP_NEEDED=0
break
fi
CURRENT_SSID=`grep "^ssid=$ONLINE_SSID" $HOSTAPD | cut -d"=" -f2`
if [ $CURRENT_SSID == $ONLINE_SSID ]
then
logger -s -t "gluon-offline-ssid" -p 5 "TQ is $GATEWAY_TQ, SSID is $CURRENT_SSID, change to $OFFLINE_SSID" #Write Info to Syslog
sed -i s/^ssid=$ONLINE_SSID/ssid=$OFFLINE_SSID/ $HOSTAPD
HUP_NEEDED=1 # HUP here would be to early for dualband devices
else
echo "There is something wrong, did not find SSID $ONLINE_SSID or $OFFLINE_SSID"
fi
done
fi
fi
 
if [ $GATEWAY_TQ -ge $LOWER_LIMIT -a $GATEWAY_TQ -le $UPPER_LIMIT ]; #This is just to get a clean run if we are in-between the grace periode
if [ "$(batctl gwl | grep "No gateways in range")" = "" ] && [ "$(uci get wireless.w2ap.ssid)" = "FFF_OFFLINE" ]
then
then
uci set wireless.w2ap.ssid='franken.freifunk.net'
echo "TQ is $GATEWAY_TQ, do nothing"
uci commit
HUP_NEEDED=0
/etc/init.d/network restart
fi
fi  
if [ $HUP_NEEDED == 1 ]; then
killall -HUP hostapd # Send HUP to all hostapd um die neue SSID zu laden
HUP_NEEDED=0
echo "HUP!"
fi
</pre>
</pre>


Zeile 30: Zeile 102:
noch schnell nen cronjob anlegen:
noch schnell nen cronjob anlegen:
<pre>
<pre>
cd /usr/lib/micron.d
vi /usr/lib/micron.d/fff-offline
</pre>
</pre>
i drücken zum Bearbeiten
<pre>
<pre>
ls
*/1 * * * * /usr/sbin/ssidoffline
</pre>
irgendeine von den Dateien aufmachen (oder ne neue anlegen):
<pre>
vi DATEINAME
</pre>
</pre>
ESC und :wq zum speichern


o drücken für neue Zeile
<pre>
*/1 * * * * /usr/sbin/ssidoffline
</pre>
micrond neu starten
micrond neu starten
<pre>
<pre>
Zeile 50: Zeile 116:
Achtung beim Test:
Achtung beim Test:
Nach Abbruch der Verbindung dauert es u.U. ein paar Minuten bis batctl gwl wirklich ein "No gateways in range" liefert, diese Zeit muss abgewartet werden es kann daher nach ziehen der Uplinkverbindung durchaus bis zu 5 Minuten dauern bis die SSID geändert wurde.
Nach Abbruch der Verbindung dauert es u.U. ein paar Minuten bis batctl gwl wirklich ein "No gateways in range" liefert, diese Zeit muss abgewartet werden es kann daher nach ziehen der Uplinkverbindung durchaus bis zu 5 Minuten dauern bis die SSID geändert wurde.
Um zu prüfen welcher Gateway aktuell Verwendung findet, kann per ssh in der Kommandozeile "batctl gwl" eingegeben werden, der Gateway mit "=>" Kennzeichnung wird verwendet.
[[Kategorie:Scripte]]

Aktuelle Version vom 28. Dezember 2016, 20:42 Uhr

Es kommt immer mal wieder vor, dass Router "in der Luft hängen" und keine Verbindung zum Freifunknetz haben. Bei anfälligen Routern macht es Sinn dieses Script zu installieren, dann wird die ausgestrahlte SSID geändert sobald kein Gateway (=DHCP Server) mehr in Reichweite ist. Man kann sich immer noch zum internen Freifunknetz verbinden, wird aber keine IP bekommen. Batman bleibt davon unberührt. Daher funktioniert auch weiterhin der ssh Zugriff oder web-login - sofern Verbindung besteht.

Das unten beschriebene Skript basiert auf folgender Vorlage und wurde für Freifunk-franken angepasst: https://github.com/eulenfunk/packages/blob/master/gluon-ssid-changer/files/lib/gluon/ssid-changer/ssid-changer.sh

Mit diesem Script wird die SSID geändert in "FF_OFFLINE_"ROUTERNAME"".

vi /usr/sbin/ssidoffline 

i drücken zum Bearbeiten

#!/bin/sh

# At first some Definitions:

ONLINE_SSID=$(uci get wireless.w2ap.ssid -q)
: ${ONLINE_SSID:=franken.freifunk.net}   # if for whatever reason ONLINE_SSID is NULL
OFFLINE_PREFIX='FF_OFFLINE_' # Use something short to leave space for the nodename

UPPER_LIMIT='75' #Above this limit the online SSID will be used
LOWER_LIMIT='30' #Below this limit the offline SSID will be used
# In-between these two values the SSID will never be changed to prevent it from toggeling every Minute.

# Generate an Offline SSID with the first and last Part of the nodename to allow owner to recognise wich node is down
NODENAME=`uname -n`
if [ ${#NODENAME} -gt $((30 - ${#OFFLINE_PREFIX})) ] ; then #32 would be possible as well
	HALF=$(( (28 - ${#OFFLINE_PREFIX} ) / 2 )) #calculate the length of the first part of the node identifier in the offline-ssid
	SKIP=$(( ${#NODENAME} - $HALF )) #jump to this charakter for the last part of the name
	OFFLINE_SSID=$OFFLINE_PREFIX${NODENAME:0:$HALF}...${NODENAME:$SKIP:${#NODENAME}} # use the first and last part of the nodename for nodes with long name
else
	OFFLINE_SSID="$OFFLINE_PREFIX$NODENAME" #greate we are able to use the full nodename in the offline ssid
fi

#Is there an active Gateway?
GATEWAY_TQ=`batctl gwl | grep "^=>" | awk -F'[()]' '{print $2}'| tr -d " "` #Grep the Connection Quality of the Gateway which is currently used
if [ ! $GATEWAY_TQ ]; #If there is no gateway there will be errors in the following if clauses
then
	GATEWAY_TQ=0 #Just an easy way to get an valid value if there is no gateway
fi
if [ $GATEWAY_TQ -gt $UPPER_LIMIT ];
then
	echo "Gateway TQ is $GATEWAY_TQ node is online"
	for HOSTAPD in $(ls /var/run/hostapd-phy*); do #Check status for all physical devices
		CURRENT_SSID=`grep "^ssid=$ONLINE_SSID" $HOSTAPD | cut -d"=" -f2`
		if [ $CURRENT_SSID == $ONLINE_SSID ]
		then
			echo "SSID $CURRENT_SSID is correct, nothing to do"
			HUP_NEEDED=0
			break
		fi
		CURRENT_SSID=`grep "^ssid=$OFFLINE_SSID" $HOSTAPD | cut -d"=" -f2`
		if [ $CURRENT_SSID == $OFFLINE_SSID ]
		then
			logger -s -t "gluon-offline-ssid" -p 5 "TQ is $GATEWAY_TQ, SSID is $CURRENT_SSID, change to $ONLINE_SSID" #Write Info to Syslog
			sed -i s/^ssid=$CURRENT_SSID/ssid=$ONLINE_SSID/ $HOSTAPD
			HUP_NEEDED=1 # HUP here would be to early for dualband devices
		else
			echo "There is something wrong, did not find SSID $ONLINE_SSID or $OFFLINE_SSID"
		fi
	done
fi
if [ $GATEWAY_TQ -lt $LOWER_LIMIT ];
then
	echo "Gateway TQ is $GATEWAY_TQ node is considered offline"
	for HOSTAPD in $(ls /var/run/hostapd-phy*); do #Check status for all physical devices
		CURRENT_SSID=`grep "^ssid=$OFFLINE_SSID" $HOSTAPD | cut -d"=" -f2`
		if [ $CURRENT_SSID == $OFFLINE_SSID ]
		then
			echo "SSID $CURRENT_SSID is correct, noting to do"
			HUP_NEEDED=0
			break
		fi
		CURRENT_SSID=`grep "^ssid=$ONLINE_SSID" $HOSTAPD | cut -d"=" -f2`
		if [ $CURRENT_SSID == $ONLINE_SSID ]
		then
			logger -s -t "gluon-offline-ssid" -p 5 "TQ is $GATEWAY_TQ, SSID is $CURRENT_SSID, change to $OFFLINE_SSID" #Write Info to Syslog
			sed -i s/^ssid=$ONLINE_SSID/ssid=$OFFLINE_SSID/ $HOSTAPD
			HUP_NEEDED=1 # HUP here would be to early for dualband devices
		else
			echo "There is something wrong, did not find SSID $ONLINE_SSID or $OFFLINE_SSID"
		fi
	done
fi
if [ $GATEWAY_TQ -ge $LOWER_LIMIT -a $GATEWAY_TQ -le $UPPER_LIMIT ]; #This is just to get a clean run if we are in-between the grace periode
then
	echo "TQ is $GATEWAY_TQ, do nothing"
	HUP_NEEDED=0
fi
if [ $HUP_NEEDED == 1 ]; then
	killall -HUP hostapd # Send HUP to all hostapd um die neue SSID zu laden
	HUP_NEEDED=0
	echo "HUP!"
fi

ESC und :wq zum speichern

ausführbar machen:

chmod +x /usr/sbin/ssidoffline

noch schnell nen cronjob anlegen:

vi /usr/lib/micron.d/fff-offline

i drücken zum Bearbeiten

*/1 * * * * /usr/sbin/ssidoffline

ESC und :wq zum speichern

micrond neu starten

/etc/init.d/micrond restart 

Achtung beim Test: Nach Abbruch der Verbindung dauert es u.U. ein paar Minuten bis batctl gwl wirklich ein "No gateways in range" liefert, diese Zeit muss abgewartet werden es kann daher nach ziehen der Uplinkverbindung durchaus bis zu 5 Minuten dauern bis die SSID geändert wurde. Um zu prüfen welcher Gateway aktuell Verwendung findet, kann per ssh in der Kommandozeile "batctl gwl" eingegeben werden, der Gateway mit "=>" Kennzeichnung wird verwendet.