Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guiferrpereira/92ed5bd4ed9e538817bbc106de980a23 to your computer and use it in GitHub Desktop.
Save guiferrpereira/92ed5bd4ed9e538817bbc106de980a23 to your computer and use it in GitHub Desktop.
fonzon auto connect
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib, urllib2
import traceback, time, random, datetime
from urlparse import urlparse, parse_qs
from urllib import urlencode
from os import getenv
import sys
from datetime import datetime
randomSleep = 0
while True:
try:
# Get username and password from shell environment passwords
FON_USERNAME = getenv('FON_USERNAME')
FON_PASSWORD = getenv('FON_PASSWORD')
print str(datetime.now()), " sleeping time ", randomSleep, "... "
if randomSleep > 10:
randomSleep = random.randint(4,10)
time.sleep(randomSleep)
#Pass arguments option
if len(sys.argv) < 2 and (not FON_USERNAME or not FON_PASSWORD):
print "Command: python zon_fon_authenticate.py <LOGIN> <PASSWORD>";
continue
FON_USERNAME = sys.argv[1]
FON_PASSWORD = sys.argv[2]
# Attempt to fetch the START_URL in order to be redirected
# to the Zon@Fon authetincation page
START_URL = 'http://www.sapo.pt'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor)
urllib2.install_opener(opener)
data = urllib2.urlopen(START_URL,timeout=10)
auth_url = data.geturl()
if not auth_url.startswith('https://nos2.portal.fon.com/') and not auth_url.startswith('https://nos.portal.fon.com') and not auth_url.startswith('https://zon.portal.fon.com/'):
print "Zon fon authentication was not requested. Already authenticated?"
randomSleep = random.randint(0,30)
continue
# Build the POST URL from the redirect location
url_data = parse_qs(urlparse(auth_url).query, keep_blank_values=True)
fields = [ 'nasid', 'uamip', 'uamport', 'mac', 'challenge' ]
str_data = 'res=login'
for f in fields:
str_data += '&%s=%s' % (f, url_data[f][0])
str_data += '&tab=2'
url = "%s://%s%s?%s" % (urlparse(auth_url).scheme, urlparse(auth_url).netloc, urlparse(auth_url).path, str_data)
html = urllib2.urlopen(url,
data=urllib.urlencode({'UserName': FON_USERNAME, 'Password': FON_PASSWORD}),timeout=10)
html_data = html.read()
# Check the result
if 'Incorrect username or password' in html_data:
print "Login failed, check username/password!"
randomSleep = random.randint(30,60)
continue
elif "'You're connected!":
print "You are now connected"
randomSleep = random.randint(30,60)
continue
else:
print "Something failed"
randomSleep = random.randint(10,30)
continue
except Exception:
print "Without internet?"
print(traceback.format_exc())
randomSleep = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment