I run it on a Raspberry Pi 3 with an additional WiFi USB adapter, the second WiFi connects to my home network so Domoticz can switch it's gateway to it for sending notifications should my primary internet connection fail.
Your Telstra Air username and password can be obtained by logging into your Telstra account, going to the Telstra Air section (make sure you have activated the service) then go to "Connect from overseas" and there is a section with your username and password which need to be entered into the script.
Finally this is only meant for users with an advanced level of Linux knowledge, please do not ask questions such as how to use Linux or bash programming.
connect.sh
Code: Select all
#!/bin/bash
while true; do
# Configuration
username="[email protected]"
password="PASSWORD"
wifiadapter="wlan0"
wifiap="Telstra Air"
looptime="300" # in seconds, eg 300 = 5 minutes
# Check if already connected to internet
echo "Checking connection state..."
check=$(curl -s --max-time 10 "http://captive.apple.com/hotspot-detect.html" | grep -q "Success" && echo "Success" || echo "Fail")
if $(echo "$check" | grep -q "Success"); then
echo "Already connected to the internet"
else
# Check if connected to Telstra Air Access Point
if $(iwconfig "$wifiadapter" | grep -q "$wifiap"); then
signal=$(iwconfig "$wifiadapter" | grep 'Signal level=' | awk -F= '{ print $3 }' | awk '{ print $1 }')
echo "Connected to $wifiap WiFi access point"
echo "Signal strength: $signal , ideally this should be under -70dBm, anything over this may experience reliability issues"
else
echo "Not connected to a $wifiap WiFi access point"
fi
# Display in terminal status
echo
echo "Getting WiFi station info..."
# Get wifi ap station info
ipparm=$(curl -s --max-time 10 "http://8.8.8.8" | grep "<LoginURL>")
# Breakdown variables
nasid=$(echo "$ipparm" | grep -Po -- 'nasid=\K[_\-."[:alnum:]]*')
ipaddr=$(echo "$ipparm" | grep -Po -- 'uamip=\K[_\-."[:alnum:]]*')
port=$(echo "$ipparm" | grep -Po -- 'uamport=\K[_\-."[:alnum:]]*')
macaddr=$(echo "$ipparm" | grep -Po -- 'mac=\K[_\-."[:alnum:]]*')
challenge=$(echo "$ipparm" | grep -Po -- 'challenge=\K[_\-."[:alnum:]]*')
# Display the variables in terminal
echo "nasid: $nasid"
echo "ipaddr: $ipaddr"
echo "port: $port"
echo "macaddr: $macaddr"
echo "challenge: $challenge"
echo
# Check viability
if [ "$port" -gt 2 &> /dev/null ]; then
# Connect
echo "Connecting..."
connect=$(wget -qO- --timeout=10 --keep-session-cookies \
--post-data "UserName=$username&Password=$password&_rememberMe=on" \
"https://telstra.portal.fon.com/jcp/telstra?res=login&nasid=$nasid&uamip=$ipaddr&uamport=$port&mac=$macaddr&challenge=$challenge")
if $(echo "$connect" | grep -q "You're connected!"); then
echo "Connected!"
echo
echo "Logout url is:"
logouturl=$(echo "$connect" | grep "<LogoffURL>" | sed 's/\(<LogoffURL>\|<\/LogoffURL>\)//g')
echo "$logouturl"
else
echo "Unable to connect"
looptime="120"
fi
else
echo "Unable to get connection info from the WiFi AP, likely insufficient signal, resetting the wireless network interface..."
echo
ifdown "$wifiadapter"
sleep 1
ifup "$wifiadapter"
looptime="5"
fi
fi
echo "Sleeping for $looptime seconds"
sleep "$looptime"
done
connectionstart
Code: Select all
#!/bin/bash
### BEGIN INIT INFO
# Provides: connectionstart
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description: Starts connectionstart
### END INIT INFO
case "$1" in
'start')
sudo -u root /bin/bash -c 'screen -dmS connectionmonitor /root/scripts/connect.sh'
;;
'stop')
screensession=$(sudo -u root screen -ls | grep "connectionmonitor" | awk '{ print $1 }')
sudo -u root screen -X -S "$screensession" quit
;;
*)
echo "Usage: $0 { start | stop }"
;;
esac
exit 0
firewall.sh (in my case needs to be run every time the system boots), you can add it to the init.d script if you want to do this automatically.
Code: Select all
#!/bin/bash
# Configuration
lan="wlan1"
wan="wlan0"
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW ! -i "$wan" -j ACCEPT
iptables -A FORWARD -i "$wan" -o "$lan" -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i "$lan" -o "$wan" -j ACCEPT
iptables -t nat -A POSTROUTING -o "$wan" -j MASQUERADE
iptables -A FORWARD -i "$wan" -o "$wan" -j REJECT
echo 1 > /proc/sys/net/ipv4/ip_forward
Code: Select all
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet manual
# Telstra Air WiFi
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
# My Home WiFi network
allow-hotplug wlan1
iface wlan1 inet static
wpa-conf /etc/wpa_supplicant/wpa_supplicant2.conf
address 192.168.0.69
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
Code: Select all
network={
ssid="Telstra Air"
key_mgmt=NONE
}
Now in my use case scenario, at this stage if I was to change the IP address details of a machine on my WiFi network to:
gateway: 192.168.0.69
dnsserver: 192.168.0.69
That machine would be able to access the internet through 192.168.0.69 which is acting as a gateway and providing DNS (in my experience I found it was not possible to use third party DNS servers, I had to use the ones provided by Telstra Air which is why it makes sense to use dnsmasq for my use case.
1) You will need to read, understand and customize this script for your own environment. If you are unable to understand exactly what the script does, do not use it. You agree to take full responsibility for any loss of damage caused using the script should it occur.
2) Consider and understand the security implications of using this script, you should probably install a firewall and block incoming traffic on the WiFi interface that connects to the "Telstra Air" WiFi network. This is not a comprehensive guide to securing your system or how to use various Linux firewalls, that is your responsibility!
3) The looptime specified in the script is how often the script is re-run, it is also approximately how often the script checks that the internet connection is still alive (and if its not it attempts to reconnect to the hotspot). I don't suggest setting this lower than 300 seconds.
3) I use this script running on Raspbian version Jessie.