#! /usr/bin/env python # -*- coding: utf-8 -*- # # Author: Marcel Fourné # License: MIT-X11 # # This script was made in an attempt to provide a secure, # automatable method for connecting to the RUB-network for those who # care about it (mainly those strange people in ITS). import urllib import urllib2 from M2Crypto import m2urllib2 from M2Crypto import m2, SSL from optparse import OptionParser # adjust this path for your system if needed certificate_path = "/etc/ssl/certs" # adjust the following 3 strings if the need arises (change of web-login-system) site = "https://login.rz.ruhr-uni-bochum.de/cgi-bin/laklogin" liname = "Login" loname = "Logout" # options parser = OptionParser() parser.add_option("-a", "--action", dest="action", help="the Action, namely Login or Logout (Login also needs Username and Password). This is argument is required!", default='', ) parser.add_option("-f", "--file", dest="imageoutput", help="where to store the statistics image. Experience shows, that retrieving the Image may take a while, just like via the webpage...", default='') parser.add_option("-p", "--password", dest="password", help="the Password for a login, you also need a Username", default='') parser.add_option("-q", "--quiet", dest="verbose", action="store_true", help="Don't print stuff to stdout", default=False) parser.add_option("-u", "--username", dest="username", help="the Username for a login, you also need a Password", default='') (options, args) = parser.parse_args() # SSL-setup if options.action!='': ctx = SSL.Context() ctx.load_verify_locations(capath=certificate_path) ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, depth=9) opener = m2urllib2.build_opener(ctx) urllib2.install_opener(opener) if options.action==liname: if options.username=='': if options.verbose!=True: print "Login needs a Username." exit(2) elif options.password=='': if options.verbose!=True: print "Login also needs a Password." exit(2) else: if options.imageoutput!='': data = urllib.urlencode({'username': options.username, 'password': options.password, 'ShowTraffic': "1", 'action': options.action}) else: data = urllib.urlencode({'username': options.username, 'password': options.password, 'action': options.action}) u = urllib2.urlopen(site, data) t = u.read() if t.find("Authentisierung gelungen")!=-1: if options.verbose!=True: print "Connection successfully established" if options.imageoutput!='': if options.verbose!=True: print "Retrieving usage statistics, this may take a while" begin = t.find("IMG SRC=") end = t.find(">\n

") url = t[begin+8:end] img = urllib2.urlopen(url).read() file = open(options.imageoutput, 'w') file.write(img) file.close() exit(0) else: if options.verbose!=True: print "...couldn't log in. Wrong username/password maybe?" exit(1) elif options.action==loname: data = urllib.urlencode({'action': options.action}) u = urllib2.urlopen(site, data) t = u.read() if t.find("Logout erfolgreich")!=-1: if options.verbose!=True: print "Connection successfully closed" exit(0) else: if options.verbose!=True: print "...couldn't log out?!? This should never happen!" exit(1) else: parser.print_help() exit(2) else: parser.print_help() exit(2)