Last active
October 21, 2017 20:52
-
-
Save bennet0496/1ce5f17be0786e3562c3f963481e2f1e to your computer and use it in GitHub Desktop.
Sync Fritz!BOX Wifi-Password and SSID to hostapd
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
#!/bin/bash | |
# Set to your FB addr | |
_BOXURL="http://fritz.box" | |
_USERNAME="wlanscript" | |
_PASSWORD="veryverysecurepassword" | |
# DO NOT EDIT FROM HERE | |
_CHALLENGE=$(curl -s "${_BOXURL}/login_sid.lua?username=${_USERNAME}" |\ | |
grep -Po '(?<=<Challenge>).*(?=</Challenge>)') | |
_MD5=$(echo -n ${_CHALLENGE}"-"${_PASSWORD} |\ | |
iconv -f ISO8859-1 -t UTF-16LE |\ | |
md5sum -b | awk '{print substr($0,1,32)}') | |
_RESPONSE="${_CHALLENGE}-${_MD5}" | |
#curl -v -i -s -k -d "response=${_RESPONSE}&lp=&username=${_USERNAME}" "${_BOXURL}" | |
_SID=$(curl -i -s -k -d "response=${_RESPONSE}&username=${_USERNAME}" "${_BOXURL}" |\ | |
grep -Po -m 1 '(?<=sid=)[a-f\d]+') | |
#uiView_SSID | |
#ssid=the_ssid | |
#grep ^ssid= /etc/hostapd/hostapd.conf | |
SSID=$(curl -sSL "${_BOXURL}/data.lua" -d "xhr=1&sid=${_SID}&lang=de&no_sidrenew=&page=wSet" |\ | |
grep -E '<input.*id="uiView_SSID"' |\ | |
grep -Eo '"([^"]*)"' | tr -d '"' |\ | |
tail -n 1 | perl -MHTML::Entities -e 'while(<>) {print decode_entities($_);}' | tr -d '\n' ) | |
PSK=$(curl -sSL "${_BOXURL}/data.lua" -d "xhr=1&sid=${_SID}&lang=de&no_sidrenew=&page=wKey" |\ | |
grep -E '<input.*id="uiViewpskvalue"' | grep -Eo '"([^"]*)"' |\ | |
tr -d '"' | tail -n 1 | perl -MHTML::Entities -e 'while(<>) {print decode_entities($_);}') | |
# Change SSID and reload if nessesacry | |
test -z $(grep ^ssid=$SSID /etc/hostapd/hostapd.conf) && \ | |
sed -i.bak "s/^ssid=.*$/ssid=$SSID/g" /etc/hostapd/hostapd.conf && kill -HUP $( pidof hostapd ) | |
TMPPSK=$(mktemp /tmp/psk.XXXXX) | |
cat <<EOF > $TMPPSK | |
00:00:00:00:00:00 $PSK | |
EOF | |
# Reload hostap if password changed | |
diff -q $TMPPSK /etc/hostapd.wpa_psk > /dev/null || \ | |
(mv $TMPPSK /etc/hostapd.wpa_psk && kill -HUP $( pidof hostapd )) && rm $TMPPSK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment