Skip to content

Instantly share code, notes, and snippets.

@itavero
Created February 16, 2016 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itavero/a3f6ddb13a53f5bd85bf to your computer and use it in GitHub Desktop.
Save itavero/a3f6ddb13a53f5bd85bf to your computer and use it in GitHub Desktop.
Tele2 Modem Spammer
import urllib2
import re
import collections
from pushbullet.pushbullet import PushBullet
from time import sleep
base_url = 'http://192.168.1.1/'
pushbullet_key = 'YOUR_KEY'
# Set-up basic authentication
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, base_url, "user", "user")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
# Result container
NetworkState = collections.namedtuple('NetworkState', ['wan', 'dns1', 'dns2'])
def check_network_on_modem():
print 'Checking network status on modem...'
response = None
try:
response = urllib2.urlopen(base_url + 'user_info.html')
except urllib2.URLError:
print 'Modem unreachable'
if response is None:
return NetworkState(False, False, False)
table = re.findall('<td\s+class=\'hd\'>([^:]+):</td>\s*<td>([1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}|&nbsp)</td>', response.read())
found_wan = False
found_dns1 = False
found_dns2 = False
print '{:=<48}'.format('')
for field, value in table:
if value == '&nbsp':
value = 'UNKNOWN'
else:
if field == 'WAN IP Address':
found_wan = True
elif field == 'Primary DNS Server':
found_dns1 = True
elif field == 'Seconday DNS Server':
found_dns2 = True
print '{: <24}: {}'.format(field, value)
print '{:=<48}'.format('')
return NetworkState(found_wan, found_dns1, found_dns2)
def restore_factory_settings_on_modem():
print 'Restore factory settings on modem...'
response = None
try:
response = urllib2.urlopen(base_url + 'defaultsettings.html')
except urllib2.URLError:
print 'Modem unreachable'
if response is None:
return False
sessionKey = None
results = re.findall('loc\s+\+=\s+\'([^\']+)', response.read())
for data in results:
if data.startswith('sessionKey'):
sessionKey = data
break
if sessionKey is None:
print 'No sessionKey found!'
return False
response = None
try:
response = urllib2.urlopen(base_url + 'restoreinfo.cgi?' + sessionKey)
except urllib2.URLError:
print 'Modem unreachable'
if response is None:
return False
return ('Broadband Router Restore' in response.read())
def list_pushbullet_devices():
bullet = PushBullet(pushbullet_key)
devices = bullet.getDevices()
for dev in devices:
name = ''
if 'nickname' in dev:
name = dev['nickname']
if 'type' in dev:
name += ' ({})'.format(dev['type'])
print '{}\t: {}'.format(dev['iden'], name)
def send_push_notification(title, body):
bullet = PushBullet(pushbullet_key)
bullet.pushNote(title, title, body, 'title')
print 'Notification "{}": "{}"'.format(title, body)
########################################
modem_online = False
print 'Modem spammer started!'
while not modem_online:
networkState = check_network_on_modem()
if not networkState.wan:
# DSL connection not ready yet. Try again in a minute.
sleep(1 * 60)
elif networkState.dns1 and networkState.dns2:
try:
send_push_notification('Tele2 Modem Spammert', 'Het lijkt er op dat je eindelijk online bent!')
modem_online = True
except:
modem_online = False
else:
# Attempt to restore the factory defaults
restore_factory_settings_on_modem();
# Wait 3 minutes...
sleep(3 * 60)
print 'Done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment