forked from inderpreet99/afraid-autologin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autologin.py
executable file
·106 lines (82 loc) · 3.18 KB
/
autologin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
from selenium import webdriver
import argparse
import os
import sys
import time
import random
import logging
# init logging to stdout and file
logger = logging.getLogger('afraid-autologin')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s >>> %(levelname)s: %(message)s')
file_logger = logging.FileHandler('afraid-autologin.log')
file_logger.setFormatter(formatter)
logger.addHandler(file_logger)
stdout_logger = logging.StreamHandler(sys.stdout)
stdout_logger.setFormatter(formatter)
logger.addHandler(stdout_logger)
logger.debug('afraid-autologin startup')
try:
from settings import USERNAME, PASSWORD, URL
if USERNAME == "" or PASSWORD == "":
raise ImportError
except ImportError:
logger.error("You need the username and password in the settings.py file:")
logger.error("USERNAME = 'my_username_here'")
logger.error("PASSWORD = 'my_password_here'")
sys.exit(-1)
def main(logger):
parser = argparse.ArgumentParser(description=' \
Logs in freedns.afraid.org using USERNAME/PASSWORD from settings.py. \
Avoids account from being terminated due to inactivity (6 months).\
Run this script using cron every few months:\
source bin/activate && python autologin.py')
parser.add_argument('--browser', default='firefox', help='browser name \
must be supported by selenium \
(options: firefox, phantomjs')
parser.add_argument('--verbose', action='store_true',
help='show extra output')
parser.add_argument('--headless', action='store_true', help='initialise a \
virtual display for headless systems')
args = parser.parse_args()
if args.headless:
logger.debug('init virtual display')
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1366, 768))
display.start()
logger.debug('init browser')
if args.browser == 'phantomjs':
browser = webdriver.PhantomJS()
else:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
browser = webdriver.Firefox(firefox_profile=profile)
browser.get(URL)
time.sleep(random.randint(1,3))
logger.debug('navigating to login page')
browser.find_element_by_link_text("Domains").click()
time.sleep(random.randint(1,3))
logger.debug('submit login form')
username_field = browser.find_element_by_name('username')
password_field = browser.find_element_by_name('password')
username_field.send_keys(USERNAME)
password_field.send_keys(PASSWORD)
browser.find_element_by_name('submit').click()
time.sleep(random.randint(1,3))
# view the subdomains
browser.find_element_by_link_text("Subdomains").click()
# check whether login was successful
# 'Last IP' is only shown after login
success = 'Last IP' in browser.page_source
if success:
logger.info('login sucessful')
else:
logger.error('login unsuccessful')
browser.quit()
exit_code = 0
if not success:
exit_code = -1
exit(exit_code)
if __name__ == "__main__":
main(logger)