Linux bash script to connect to Telstra Air hotspots and login automatically

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Linux bash script to connect to Telstra Air hotspots and login automatically

Post by ben53252642 »

This is a Linux bash script I wrote for connecting to a Telstra Air hotspot, logging in and maintaining the connection (restarting it if necessary).

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&#39;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
Below is the script I've got in /etc/init.d to automatically start connect.sh in screen and run it in the background on system startup. Make sure you have screen installed: apt-get install screen

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
Separately (mostly for my own use if I need to set this up again) this is the script I use containing iptables to forward traffic between the wan and lan wifi interfaces and enable ip forwarding in my own use case scenario. An extremely advanced level of Linux and iptables is needed to understand this, please do not use it if you do not understand how it works or what it does.

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
And.. this is the content of my /etc/network/interfaces file

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
And.. here is the content of the /etc/wpa_supplicant/wpa_supplicant.conf

Code: Select all

network={
        ssid="Telstra Air"
        key_mgmt=NONE
}
Then I installed dnsmasq: apt-get install dnsmasq

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.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
brenton
Posts: 2
Joined: Tuesday 30 October 2018 1:12
Target OS: Linux
Domoticz version:
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by brenton »

This is awesome, so simple and well written. I can’t wait to try this on openwrt
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by ben53252642 »

brenton wrote: Tuesday 30 October 2018 1:14 This is awesome, so simple and well written. I can’t wait to try this on openwrt
If your going to try with OpenWRT, please document it step by step and post a guide. Good luck! 8-)
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
brenton
Posts: 2
Joined: Tuesday 30 October 2018 1:12
Target OS: Linux
Domoticz version:
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by brenton »

Image
christosbox
Posts: 2
Joined: Saturday 16 February 2019 3:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by christosbox »

thanks @ben53252642 This is awesome!

I've come across a few telstraAir AP's which prompt an 'I agree' before login, this breaks your script.

Is there any way to 'agree' so the connection establishes?

this is what it returns:

Code: Select all

      <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  <title>Telstra with Fon - Home</title>

  <!-- CSS -->
  <link rel="stylesheet" type="text/css" href="/jcp/themes/telstra-residential/static/desktop/css/main.css" media="screen" />
  <!-- GDPR -->
  <link rel="stylesheet" type="text/css" href="/jcp/themes/telstra-residential/static/desktop/css/gdpr.css" media="screen" />
  <!-- Cookies Consent -->
  <link rel="stylesheet" type="text/css" href="/jcp/themes/telstra-residential/static/desktop/css/cookiesconsent.css" media="screen" />

  <!-- FAVICON -->
  <link rel="shortcut icon" href="/jcp/themes/telstra-residential/static/desktop/images/favicon.ico" />

  <!-- SCRIPTS -->
  <script type="text/javascript" src="/jcp/themes/telstra-residential/static/desktop/js/jquery-1.7.1.min.js"></script>
  <script type="text/javascript" src="/jcp/themes/telstra-residential/static/desktop/js/jquery.reveal.js"></script>
  <script type="text/javascript" src="/jcp/themes/telstra-residential/static/desktop/js/updateYear.js"></script>
  <!-- // GDPR JS -->
  <script type="text/javascript" src="/jcp/themes/telstra-residential/static/desktop/js/gdprSelect.js"></script>
  <script type="text/javascript" src="/jcp/themes/telstra-residential/static/desktop/js/gdprModal.min.js"></script>
  <script type="text/javascript" src="/jcp/themes/telstra-residential/static/desktop/js/gdprShowTerms.js"></script>

  <!-- Cookies Consent -->
  <script type="text/javascript" src="/jcp/themes/telstra-residential/static/desktop/js/cookiesconsent.js"></script>


  <script type="text/javascript">
<!--
      function redirect(select) {
        language = select[select.selectedIndex].value;
        var url = document.URL;
        if (url.lastIndexOf('?') == -1) {
          url += '?lang=' + language;
        } else {
          var index = url.lastIndexOf('lang=');
          if (index == -1) {
            url += '&lang=' + language;
          } else {
            var pre = url.substring(0, index);
            url2 = url.substring(index, url.length);
            var index2 = url2.lastIndexOf('&');
            var post = '';
            if (index2 != -1) {
              post = url2.substring(index2, url.length);
            }
            url = pre + 'lang=' + language + post;
          }
        }
        location.href = url;
      }
    -->
  </script>

  <script type="text/javascript">
      document.createElement('header');
    document.createElement('nav');
    document.createElement('article');
    document.createElement('section');
    document.createElement('footer');
  </script>

  <!--[if IE]><script src="js/html5.js" th:href="@{desktop/js/html5.js}"></script><![endif]-->

  <script type="text/javascript">
    $(document).ready(function () {

      $(".loginTelstra").click(function () {
        $(this).hide();
        $(".telstraForm").slideDown("fast");
        $(".login").hide("fast");
        $(".loginFonButton").slideDown("fast");
      });
      $(".loginFonButton").click(function () {
        $(this).hide();
        $(".login").slideDown("fast");
        $(".telstraForm").hide("fast");
        $(".loginTelstra").slideDown("fast");
      });

    });
  </script>

</head>

<!-- Cookies Consent -->
<div id="cookiesConsent">
  <p>Fon has updated its cookie policy. This website uses cookies to improve your browsing experience and to show you relevant advertising. These cookies come from us and third parties. You can change the cookie settings at any time. <a href="https://fon.com/legal/">Read More...</a></p>
  <div>
    <a href="#" class="acceptButton">Accept</a>
  </div>
</div>

<body class="home">

  <div class="container">

    <header class="header">
      <div class="wrap">
        <div class="logoFon"></div>


        <div class="right">
          <nav>
            <ul>
              <li>
                <a href="https://telstra.portal.fon.com/jcp/telstra?res=failed&amp;nasid=xx-xx-xx-xx-xx-xx&amp;uamip=192.168.182.1&amp;uamport=3990&amp;mac=xx-xx-xx-xx-xx-xx-&amp;challenge=123401324013240123401432&amp;userurl=http://taco.com/cname.aspx">Home </a>
              </li>
              <li>
                <a href="#" class="big-link" data-reveal-id="aboutpartner" data-animation="fade">Telstra and Fon</a>
              </li>
              <li>
                <a href="#" class="big-link" data-reveal-id="aboutfon" data-animation="fade">About Fon </a>
              </li>
            </ul>
          </nav>
        </div>
      </div>
    </header>

    <div class="wrap">

      <div class="main" role="main">

        <div class="col1 left">

          <!-- login with Telstra -->
          <div class="loginTelstra" style="display:none;"><span>Connect to Telstra Air &reg;</span>      <span class="logoTelstra"></span> </div>

          <div class="telstraForm">

            <div class="loginTelstra2"><span>Connect to Telstra Air &reg;</span>         <span class="logoTelstra2"></span> </div>

            <div class="error">
              <span>It is currently not possible to login, please try again later. (Code 303)</span>

            </div>

            <p>If you're a Telstra Air member, use your Telstra ID to connect</p>

            <form id="hsp" action="https://www.telstra.com.au/airconnect#fon">
              <input type="hidden" name="res" value="hsp-notyet" />
              <input type="hidden" name="HSPNAME" value="FonTEL:AU" />
              <input type="hidden" name="VNPNAME" value="FonTEL:AU" />
              <input type="hidden" name="LOCATIONNAME" value="FonTEL:AU" />
              <input type="hidden" name="LOCATIONID" value="0" />
              <input type="hidden" name="LANGUAGE" value="en_US" />
              <input type="hidden" name="WISPURL" value="https://telstra.portal.fon.com/jcp/telstra?res=failed&amp;nasid=xx-xx-xx-xx-xx-xx&amp;uamip=192.168.182.1&amp;uamport=3990&amp;mac=xx-xx-xx-xx-xx-xx-&amp;challenge=123401324013240123401432&amp;userurl=http://router4.teamviewer.com/cname.aspx" />
              <input type="hidden" name="WISPURLHOME" value="https://telstra.portal.fon.com/jcp/telstra?res=notyet&amp;nasid=xx-xx-xx-xx-xx-xx-xx&amp;uamip=192.168.182.1&amp;uamport=3990&amp;mac=xx-xx-xx-xx-xx-xx-xx-xx&amp;challenge=1234&amp;userurl=http://router4.teamviewer.com/cname.aspx" />
              <input type="submit" id="hsp" value="Log In" />
            </form>

          </div>

          <!-- login with FON -->

          <div class="loginFonButton"><span class="logoFon2"></span> Members log in here</div>


          <div class="login" style="display:none;">

            <form id="login" action="https://telstra.portal.fon.com/jcp/telstra?res=login&amp;nasid=xx-xx-xx-xx-xx-xx-xx-xx&amp;uamip=192.168.182.1&amp;uamport=3990&amp;mac=xx-xx-xx-xx-xx-xx-xx-xx&amp;challenge=1234&amp;userurl=http://router4.teamviewer.com/cname.aspx" method="post">
              <h2>Log in with <span class="loginFon"></span></h2>

              <!-- // GDPR radiobuttons -->
              <fieldset id="gdprRadio">
                <span>
                  <input type="radio" name="chooseUser" id="passUsers" value="passusers" class="passUsers" />
                  <span>Pass & Fon Users</span>
                </span>
                <span>
                  <input type="radio" name="chooseUser" id="otherUsers" value="otherUsers" class="otherUsers" />
                  <span>Other Users</span>
                </span>
              </fieldset>

              <div class="error">
                <span>It is currently not possible to login, please try again later. (Code 303)</span>

              </div>

              <fieldset>
                <input id="user" name="UserName" value="" type="text" placeholder="Username" />
              </fieldset>

              <fieldset>
                <input id="password" name="Password" value="" type="password" autocomplete="off" placeholder="Password" />
              </fieldset>

              <fieldset class="checkbox">
                <input type="checkbox" name="rememberMe" id="remember" value="true" /><input type="hidden" name="_rememberMe" value="on" />
                <span>Remember me</span>
              </fieldset>

              <fieldset>
                <!-- // CDPR fake button -->
                <a href="#ex1" class="gdpr_submit">Login</a>
                <span class="gdpr_realsubmit">
                  <input type="hidden" name="language" id="language" value="en_US" />
                  <input value="Login" type="submit" onmousedown="javascript:_paq.push(['trackPageView', 'cp_loggedoff_login']);" />
                </span>
              </fieldset>

              <fieldset class="alignCenter">
                <a href="https://fonglobal.userzone.fon.com/dfp/forgotPassword">Forgot your password?</a>
              </fieldset>

            </form>


          </div>
          <!-- // login -->

          <div class="promo">
            <a onmousedown="javascript:_paq.push(['trackPageView', 'cp_loggedoff_noproduct']);" href="https://telstra.purchases.fon.com/purchase-access?WISPURL=https%3A%2F%2Ftelstra.portal.fon.com%2Fjcp%2Ftelstra%3Fres%3Drpp-login%26nasid%uamip%3D192.168.182.1%26uamport%3D3990%26mac%%26challenge%asdfadsfasfd%26userurl%3Dhttp%3A%2F%2Frouter4.teamviewer.com%2Fcname.aspx&amp;WISPURLHOME=https%3A%2F%2Ftelstra.portal.fon.com%2Fjcp%2Ftelstra%3Fres%3Dnotyet%26nasid%asdfdsasdfsafd%26uamip%3D192.168.182.1%26uamport%3D3990%26mac%asfdasdfasdfasdf26challenge%3D042ef020c846b1e3fc360c908641a604%26userurl%3Dhttp%3A%2F%2Frouter4.teamviewer.com%2Fcname.aspx&amp;EXPIRATION_DATE=1111111111&amp;SMSCONFIRM=false&amp;LANGUAGE=en_US&amp;AP_MAC=sdasfdasdfasdfsafd&amp;DEV_MAC=asdsafdsadf&amp;PRODUCT_ID=Promocode&amp;TOKEN=df34082d07209841d688c877ed7760d00bb990b5&amp;LANGUAGE=en_US">Redeem a promo code</a>
          </div>

        </div>
        <!-- col1 -->

        <div class="col2 right">
          <div class="buyNow">
            <div class="pass2">
              <div class="product normalPass">

                <span class="timing">1 Hour</span>
                <span class="method">??E-PaymentSMS|_en_US??</span>

                <div>
                  <span class="price">$ 6.60</span>
                </div>


                <a href="https://telstra.purchases.fon.com/purchase-access?WiFi pass</a>
              </div>
            </div>
            <div class="pass2">
              <div class="product normalPass">

                <span class="timing">1 Day</span>
                <span class="method">E-Payment</span>
                <div>
                  <span class="price">$ 10.00</span>
                </div>



                <a href="https://telstra.purchases.fon.com/purchase-access?WISPURL=https%3A%2F%2Ftelstra.portal.fon.com%2Fjcp%2Ftelstra%3Fres%3Drpp-login%Buy WiFi pass</a>
              </div>
            </div>
            <div class="pass2">
              <div class="product popularPassBg">
                <div class="popularPass"></div>
                <span class="timing">5 Days</span>
                <span class="method">E-Payment</span>
                <div>
                  <span class="price">$ 23.00</span>
                </div>


FONAccessBundle&#39;]);">Buy WiFi pass</a>
              </div>
            </div>
            <div class="pass2">
              <div class="product normalPass">

                <span class="timing">30 Days</span>
                <span class="method">E-Payment</span>
                <div>
                  <span class="price">$ 44.90</span>
                </div>
FONAccessThirtyDayBundle&#39;]);">Buy WiFi pass</a>
              </div>
            </div>
            <!-- // pass2 -->


          </div>
          <!-- // buyNow -->



        </div>
        <!-- // col2 -->



      </div>
      <!-- // Main -->





    </div>
    <!-- // wrap -->

    <div class="clr"></div>

    <footer>
      <a href="https://corp.fon.com/legal" target="_blank">Legal notice</a> |
      <a href="https://support.fon.com/hc/categories/200575272-Access-network" target="_blank">Support</a> |
      <span id="copyright_year"></span>
    </footer>


  </div>
  <!-- // container -->

  <div id="aboutfon" class="reveal-modal">
    <h1>About Fon </h1>
    <article><h2>Fon is your WiFi network</h2><p>Fon's mission is to blanket the world with WiFi, so you can easily upload pictures, stream music and videos and update your social network profiles from anywhere. Whether you're relaxing in your living room, having a tea at your local coffee shop or shopping in Tokyo, Fon is where you are going.</p><h2>How does Fon work?</h2><p>Fon splits your WiFi signal into two. A private one that is just for you, and a public one, which you share with other Fon members. Both networks are secure and since the public signal is created from the unused portion of your bandwidth.</p><p>As a sharing Fon member, you will be able to enjoy all of the network's hotspots both in Australia and abroad for free. </p><p>Not ready to become a member? We make it easy and affordable for you to connect to the network. Just buy a pass and connect to any of our hotspots!</p><h2>Want to learn more?</h2><p>Visit it us at fon.com or talk to one of the millions of Fon members worldwide active on all the major social networks.</p></article>
    <a class="close-reveal-modal">×</a>
  </div>

  <div id="aboutpartner" class="reveal-modal">
    <h1>Telstra and Fon</h1>
    <article><p>We’re creating Australia’s largest Wi-Fi network, and it’s called Telstra Air™. With the help of Telstra customers, we’re planning to bring Wi-Fi to more places across Australia. And with the help of global Wi-Fi provider Fon, Telstra Air customers will be able to access more than 13 million hotspots overseas.</p></article>
    <a class="close-reveal-modal">×</a>
  </div>


  <!-- WISPr message -->
  <span class="displayNone"><!--<?xml version="1.0" encoding="UTF-8"?>
 <WISPAccessGatewayParam xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.acmewisp.com/WISPAccessGatewayParam.xsd">
 <AuthenticationReply>
<MessageType>120</MessageType>
<ResponseCode>255</ResponseCode>
<ReplyMessage>Request Denied</ReplyMessage>
<FONResponseCode>910</FONResponseCode>
</AuthenticationReply>
</WISPAccessGatewayParam>--></span>


  <!-- Piwik code: added on 2014 March,18 -->
  <span>
    <script type="text/javascript">
      var _paq = _paq || [];
      _paq.push(["setCookieDomain", "*.fon.com"]);
      _paq.push(["setDomains", ["*.fon.com"]]);
      _paq.push(['setDocumentTitle', "cp_residential"]);
      _paq.push(["trackPageView"]);
      _paq.push(["enableLinkTracking"]);
      (function () {
        var u = (("https:" == document.location.protocol) ? "https" : "http") + "://stats.fon.com/";
        _paq.push(["setTrackerUrl", u + "piwik.php"]);
        _paq.push(["setSiteId", "62"]);
        var d = document, g = d.createElement("script"), s = d.getElementsByTagName("script")[0];
        g.type = "text/javascript";
        g.defer = true;
        g.async = true;
        g.src = u + "piwik.js";
        s.parentNode.insertBefore(g, s);
      })();
    </script>
  </span>

  <!-- End Piwik Code -->

  <!-- // GDPR Modal -->
  <div id="ex1" class="modal">

    <div class="gdpr_title">Basic information on data protection</div>
    <!-- // GDPR default users -->
    <span id="default_gdpr" class="gdpr_text">
      <div class="gdpr_content"><table id="gdpr_table"> <tbody> <tr> <td>Data Controller</td> <td>FON WIRELESS LIMITED.</td> </tr> <tr> <td>Purpose</td> <td>Managing subscription of the end users and their identification.</td> </tr> <tr> <td>Legitimation</td> <td>Execution of a contract </td> </tr> <tr> <td>Recipients</td> <td>The personal data will share to Fon’s Group, competent Public Administrations in accordance with the applicable law and Fon’s Partners (Telecom Operators) in the territory where we offer and you get access to the Fon’s services, as well as to the data processors under the provision of its services (see additional information section below).</td> </tr> <tr> <td>Rights</td> <td>You have the right to access, rectify and erase personal data, as well as other rights, as explained in the additional information section below. </td> </tr> <tr> <td>Additional Information</td> <td>You can consult additional and detailed information on Data Protection in our website: <a href="https://fon.com/privacy-policy/" target="_blank">Privacy Policy of Fon</a></td> </tr> </tbody> </table></div>
      <div class="gdpr_checkbox" id="fon_check">
        <label class="gdpr_label">
          <input type="checkbox" class="myCheck01" />
          <span>By completing and submitting this form, you agree that you have read and understand  <a href="https://corp.fon.com/legal-cp/" target="_blank">Terms of Use of Fon</a>, as well as all other applicable policies.</span>
        </label>
      </div>
    </span>

    <fieldset>
      <input type="hidden" name="language" id="language" value="en_US" />
      <input value="Ok" id="login" type="submit" form="login" disabled="disabled" onmousedown="javascript:_paq.push(['trackPageView', 'cp_loggedoff_login']);" />
    </fieldset>

  </div>
  <!-- // MODAl -->


</body>

</html>

ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by ben53252642 »

I think the script can definitely be adapted, I'm overseas (not back in Aus) until the 23rd of Feb, I'll have a look in the following week.

Unless the I agree is a captcha it should just be a case of posting the I agree as part of the request, you might be able to figure it out using chrome inspect feature.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
christosbox
Posts: 2
Joined: Saturday 16 February 2019 3:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by christosbox »

I tried the chrome inspect but there seems to be a bit more to it.
Enjoy your time away! I'd be happy to work on it with you, it's just gone a bit over my head
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by ben53252642 »

Steps I followed on Raspbian Stretch Lite on a Raspberry Pi 3 B+

Internal Wifi adapter connected to my home network
USB Wifi adapter connected to Telstra Air

Steps I followed (DO NOT connect the second wifi adapter or reboot the system until mentioned in the steps):

1) sudo passwd root
2) sudo nano /etc/ssh/sshd_config
Change line: #PermitRootLogin prohibit-password
To: PermitRootLogin yes
2) sudo reboot and login as root
3) deluser pi
4) apt-get install screen lshw
5) run: lshw -c network
Below change the mac address in step 6 to that of the wifi adapter shown in the previous command
6) run: echo 'SUBSYSTEM==”net”, ACTION==”add”, ATTR{address}==”b8:27:a3:04:d3:23″, NAME=”wlan0″' > /etc/udev/rules.d/70-wifi.rules
7) connect the second wifi adapter and run: lshw -c network
Below change the mac address in step 7 to that of the additional wifi adapter shown in the previous command
8) echo 'SUBSYSTEM==”net”, ACTION==”add”, ATTR{address}==”c8:27:e9:04:f3:15″, NAME=”wlan1″' >> /etc/udev/rules.d/70-wifi.rules
9) Disable the second network adapters driver from automatically starting at boot:
The driver name for the second adapter was listed in step 7, mine was driver=rtl8192cu
I added these 2 lines to: /etc/modprobe.d/blacklist.conf
echo 'blacklist rtl8192cu' >> /etc/modprobe.d/blacklist.conf
echo 'blacklist 8192cu' >> /etc/modprobe.d/blacklist.conf
10) Set a static IP for the home network:
nano /etc/dhcpcd.conf
Add lines changing as needed:

Code: Select all

# Home network static IP
interface wlan0
static ip_address=192.168.0.121/24
11) Set home WiFi network configuration:
nano /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=AU

network={
        ssid="SSID"
        psk="PASSWORD"
}
12) Set Telstra Air WiFi network configuration:
nano /etc/wpa_supplicant/wpa_supplicant-wlan1.conf

Code: Select all

country=AU
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
        ssid="Telstra Air"
        bssid=
        key_mgmt=NONE
}
13) mkdir -p /root/scripts
14) nano /root/scripts/connect.sh

Code: Select all

#!/bin/bash

# Configuration
username="[email protected]"
password="PASSWORD"
wifiadapter="wlan1"
wifiap="Telstra Air"
looptime="300" # in seconds, eg 300 = 5 minutes

# Bring up wifi adapter
echo "Starting WiFi adapter..."
modprobe 8192cu
ifconfig "$wifiadapter" up
sleep 10

# Start loop
while true; do

# 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&chooseUser=passusers" \
"https://telstra.portal.fon.com/jcp/telstra?res=login&nasid=$nasid&uamip=$ipaddr&uamport=$port&mac=$macaddr&challenge=$challenge&userurl=http://www.google.com&ANID=$username&ANIDPASSWORD=$password")

echo "$connect"
if $(echo "$connect" | grep -q "You&#39;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
ifconfig "$wifiadapter" down
sleep 1
ifconfig "$wifiadapter" up
sleep 30
fi
fi

echo "Sleeping for $looptime seconds"
sleep "$looptime"
done
15) nano /etc/init.d/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
16) chmod 755 /etc/init.d/connectionstart
17) update-rc.d connectionstart defaults
18) nano /root/scripts/firewall.sh

Code: Select all

#!/bin/bash

# Configuration
lan="wlan0"
wan="wlan1"

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
WORK IN PROGRESS...
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
seattle72
Posts: 2
Joined: Sunday 08 December 2019 15:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by seattle72 »

hey @ben53252642 I came across your script and was really excited to try it. I wanted to see if you’ve had any progress on your end as I’m a little stuck on mine haha.

My setup is a headless buster-lite Raspberry Pi Zero W with a wifi adapter TP-Link TL-WN725N.

The adapter (wlan1) connects to Telstra Air then bridges the connection to the Pi’s internal wifi which acts as an AP (wlan0).

I’ve tested all this with my home network and it worked great but when I try to implement this script to only use Telstra Air as the network, that’s when something breaks.

I don't know if Telstra has changed something on their end or I've just missed something little. Any help you'd be an absolute lifesaver! I've been trying to figure this out for a few years now. I feel like I'm finally close, just missing the last final bit.
Last edited by seattle72 on Wednesday 11 December 2019 15:49, edited 1 time in total.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by ben53252642 »

seattle72, I live in an apartment building with > 500 other apartments.

No matter how much work I put into the script there was always a major problem that unfortunately made me have to abandon using Telstra Air.

Read under the "Network Speed" heading (I'm not sure if its correct but it is consistent with my observation that there are limits on how many clients can join the access points): https://whirlpool.net.au/wiki/telstra_air

Often my Raspberry Pi simply could not get an available access point despite being within good signal range (remember now that people are walking around with the Telstra Air app that automatically connects them to Telstra Air hotspots).

What I did for my backup internet connection is a $10 a month Belong sim card in a 4G USB modem. The first month with Belong I went on the plan that had 30gb included for $40 then dropped it straight back to the $10 plan.

My backup gateway doesn't even establish a connection unless its needed so I'm not wasting any data.

Hope this helps.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
seattle72
Posts: 2
Joined: Sunday 08 December 2019 15:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by seattle72 »

Hey ben53252642

I just wanted to say thanks again for your script. It was extremely well written, it just took me a little bit to understand being self taught. I finally got it to work on my end. For some weird reason I just had to amend the link and that's what was needed to get the Breakdown variables to work and make the script work.

# Get wifi ap station info
ipparm=$(curl -s --max-time 10 "http://8.8.8.8" | grep "<LoginURL>")

To this:

# Get wifi ap station info
ipparm=$(curl -s --max-time 10 "http://google.com" | grep "<LoginURL>")
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Linux bash script to connect to Telstra Air hotspots and login automatically

Post by ben53252642 »

Thanks for posting the update, by all means good luck to you if you can make use of Telstra Air.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest