#!/usr/local/packages/@appstore/Python/usr/local/bin/python
import urllib
import urllib2
import cookielib
import getopt
import sys
import logging
def getRandHTMLResponse(response):
target = "<form id=\'login"
targetresponse = "<div id=\'sign-up-wrap\'"
response = response[response.find(targetresponse):len(response)]
return response[response.find(target)+len(target):response.find(target)+len(target):response.find(target)+len(target)+4]
def getHiddenRandHTMLResponse(response):
target = "<input type=\'hidden\' name=\'Login\' value=\'"
targetresponse = "<div id=\'sign-up-wrap\'"
parsedres = response[response.find(targetresponse):len(response)]
return parsedres[parsedres.find(target)+len(target):parsedres.find(target)+len(target)+34]
def checkLogin(response):
target = "<title>No-IP Members Portal: Your No-IP</title>"
if response.find(target) == -1:
return False
return True
def usage():
print "usage: ./noipAutoLogin [options]"
print ""
print "options:"
print "-h, --help show this help message and exit"
print "-u, --username set your NoIP login_username"
print "-p, --password set your NoIP login_password"
print ""
print "example:"
print "./noipAutoLogin -u username -p password"
class HTMLSession:
cj = None
opener = None
txHeaders = None
def __init__(self, txHeaders):
#The CookieJar will hold any cookies necessary throughout the login process.
self.cj = cookielib.MozillaCookieJar()
self.txHeaders = txHeaders
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)
def setHeaders(self, txheaders):
self.txHeaders = txHeaders
def getHeaders(self):
return self.txHeaders
def openURI(self, uri, txdata):
try:
req = urllib2.Request(uri, txdata, self.txHeaders)
# create a request object
handle = urllib2.urlopen(req)
# and open it to return a handle on the url
except IOError as e:
print 'we failed to open "%s".' % uri
if hasattr(e, 'code'):
print 'We failed with error code - %s.' % e.code
logging.error('We failed with error code - %s.' % e.code)
elif hasattr(e, 'reason'):
print "The error object has the following 'reason' attribute :"
print e.reason
print "This usually means the server doesn't exist,'"
print "is down, or we don't have an internet connection."
return None
else:
return handle.read()
def main(argv):
username = ""
password = ""
logfile = "/volume1/logs/noipAutoLogin.log"
hiddenval = ""
theurl = "https://www.noip.com/login"
thelogouturl = "https://www.noip.com/logout"
txdata = None
txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
# fake a user agent, some websites (like google) don't like automated exploration
logging.basicConfig(filename=logfile,level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y/%m/%d %H:%M:%S')
try:
opts, args = getopt.getopt(argv, "hu:p:", ["help", "username=","password="])
except getopt.GetoptError:
usage()
logging.warning('Manual login with incorrect parameters')
exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
exit(2)
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
myhtmlsession = HTMLSession(txheaders)
response = myhtmlsession.openURI(theurl, None)
if response == None:
sys.exit(0)
hiddenval = "1"
txdata = urllib.urlencode({'username':username, 'password':password, 'Login':hiddenval, 'Login': "Sign In"})
response = myhtmlsession.openURI(theurl, txdata)
if response == None:
sys.exit(0)
#we should sleep here for about 10 seconds.
if checkLogin(response):
print 'We have succesfully logged into NoIP.'
logging.info('We have succesfully logged into NoIP.')
else:
print 'Login failed'
logging.info('Login failed')
response = myhtmlsession.openURI(thelogouturl, None)
if response == None:
sys.exit(0)
if __name__ == "__main__":
main(sys.argv[1:])