Skip to content

Instantly share code, notes, and snippets.

@lzcjames
Last active Mar 14, 2021
Embed
What would you like to do?
Auto-connect to SFR WiFi (Authentification automatique sur le SFR WiFi)
#!/bin/bash
# Auto-connect to SFR WiFi
# To use in combination with wicd
# by vincesafe (https://vincesafe.fr/2014/11/10/authentification-automatique-sur-le-sfr-wifi/)
# modified by lzcjames (GitHub:https://github.com/lzcjames)
# VARS
# location: URL associated with 302 reply (contains challenge)
# challenge: part of POST data to be sent
# target: target URL for POST
# nasid: hotspot's MAC address (part of location)
# mac: client's MAC address (part of location)
# uamport: same here
# uamip: same here
# mode, channel: ...
# uname: username
# pass: password
uname="change your login here"
pass="change your password here"
target=https://hotspot.wifi.sfr.fr/nb4_crypt.php
autoLogin() {
# Step 0: wait for the connection to establish
sleep 2
# Step 1: get the location with wget
# wget -O /dev/null -o loc http://perdu.com
# echo `grep Location loc` > loc
#location=`awk 'BEGIN { FS=" " } { print $2 }' loc`
# YEP! my solution 1 without a file temp
# -q : suppresses the progress bar and some other annoyingly chatty parts of the wget output
# -S : print the headers sent by HTTP servers
# -O - : writes the headers to the stdout ("the console")
# s/ //g OR s/[[:space:]]//g : remove all whitespaces
#location=`wget -q -S -O - http://perdu.com 2>&1 | grep -Fi Location: | sed 's/ //g'`
# YEP! my solution 2 using cURL without a file temp
# -s : silences everything, don't show progress meter or error messages
# -S : allows error reports when -s is in force
# -D - : "-D": writes the headers to the stdout ("the console")
location=`curl -s -S -D - http://perdu.com | grep -Fi Location:`
# Step 2: extract challenge, MAC addresses, ... from location
#challenge=`awk 'BEGIN { FS="&" } { print $4 }' loc | awk 'BEGIN { FS="=" } { print $2 }'`
#nasid=`awk 'BEGIN { FS="&" } { print $6 }' loc | awk 'BEGIN { FS="=" } { print $2 }'`
#mac=`awk 'BEGIN { FS="&" } { print $7 }' loc | awk 'BEGIN { FS="=" } { print $2 }'`
#uamport=`awk 'BEGIN { FS="&" } { print $3 }' loc | awk 'BEGIN { FS="=" } { print $2 }'`
#uamip=`awk 'BEGIN { FS="&" } { print $2 }' loc | awk 'BEGIN { FS="=" } { print $2 }'`
#mode=`awk 'BEGIN { FS="&" } { print $8 }' loc | awk 'BEGIN { FS="=" } { print $2 }'`
#channel=`awk 'BEGIN { FS="&" } { print $9 }' loc | awk 'BEGIN { FS="=" } { print $2 }' | awk 'BEGIN { FS=" " } { print $1 }'`
# remove beginning string until first '&'
substr=`echo ${location#*&}`
# Note: sh or ash etc... do not support array but bash. See:https://unix.stackexchange.com/questions/253892/syntax-error-unexpected-when-creating-an-array
# Regex : ^BeginStrEndStr$
array=($(echo "$substr" | tr "^&=$" "\n" ))
challenge=${array[5]}
nasid=${array[9]}
mac=${array[11]}
uamport=${array[3]}
uamip=${array[1]}
mode=${array[13]}
channel=${array[15]}
# Step 3: prepare POST with target URL (set in the code)
postdata="choix=neuf&username=$uname&password=$pass&conditions=on&challenge=$challenge&username2=$uname&accessType=neuf&lang=fr&mode=$mode&userurl=http://perdu.com&uamip=$uamip&uamport=$uamport&channel=$channel&mac=$nasid|mac&connexion=Connexion"
# Step 4: send POST request
wget -O debug $target --post-data="$postdata"
# Step 5: get a page on local AP
newloc=`grep location debug | awk 'BEGIN { FS="\"" } { print $2 }'`
wget $newloc -O newloc
# DEBUG
echo START DEBUG
echo challenge $challenge
echo nasid $nasid
echo mac $mac
echo uamport $uamport
echo uamip $uamip
echo mode $mode
echo channel $channel
echo location $location
}
# Check if wifi extended is SFR
ap=`iw dev wlan-sta info | grep -Fi ssid`
if [[ "$ap" != *"SFR"* ]] # use double brackets to apply regex
then
echo "The wifi extended is not SFR, please connect to a wifi SFR "
else
# ICMP Protocol is blocked by SFR wifi, to verify network connection, we check the return html code
code=`wget --spider -S "http://perdu.com" 2>&1 | grep "HTTP/" | awk '{print $2}'`
if [[ "$code" == "200" ]]
then
echo "Conntection works, not need to reconnect"
else
autoLogin
fi
fi
@lzcjames
Copy link
Author

lzcjames commented Mar 14, 2021

To execute the script with cron tab:
chmod +x /usr/script/connectSFR.sh
Every 2 hours at 1 minute to execute:
1 */2 * * * /bin/bash -c "/usr/script/connectSFR.sh"

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment