#!/usr/bin/python # # check_gwwalogon # - Attempts to log on to a GroupWise Web Access server # - Checks the title of the returned page to confirm that the logon has succeeded # # Usage example: check_gwwalogon -a https://webmail.xyz.com -u BLOGGSJ -p abcd1234 -f "Joe Bloggs" # # Requires: Mechanize package (tested with version 0.2.5) # # 2014-11-13 (v1.01) # import getopt, sys, os, time from mechanize import Browser options, remainder = getopt.getopt(sys.argv[1:], "a:u:p:f:h", ["address=", "user=", "password=", "fullname=", "help"]) helpMode = False url = "" username = "" password = "" fullName = "" programName = os.path.basename(__file__) for opt, arg in options: if(opt in ("-a", "--address")): url = arg if(opt in ("-u", "--user")): user = arg if(opt in ("-p", "--password")): password = arg if(opt in ("-f", "--fullname")): fullName = arg if(opt in ("-h", "--help")): helpMode = True if helpMode or not(url) or not(user) or not(password) or not(fullName): print "Usage:\n " + programName + " -a GWWA_BASE_URL -u USERNAME -p PASSWORD -f FULL_NAME" print "\nAttempts to log on to a GroupWise Web Access Server, verifying success by confirming that the returned page contains as part of its title the user's full name." print "\nMandatory arguments:" print " -a, --address GroupWise Web Access base URL" print " -u, --user Username of test account with GWWA rights" print " -p, --password GroupWise password of test account" print " -f, --fullname Full name associated with test account (for checking success)" print "\nUsage example:\n " + programName + " -a https://webmail.xyz.com -u BLOGGSJ -p abcd1234 -f \"Joe Bloggs\"" sys.exit() code = { "OK" : 0, "Critical" : 2, } expectedTitle = "Novell WebAccess (" + fullName + ")" startTime = time.clock() try: browser = Browser() except Exception as e: print "CRITICAL - Unable to create browser object: " + str(e) exit(code["Critical"]) try: browser.open(url + "/gw/webacc") except Exception as e: print "CRITICAL - Unable to browse to " + url + "/gw/webacc" + ": " + str(e) exit(code["Critical"]) try: browser.select_form(nr=0) except Exception as e: print "CRITICAL - Unable to select form 0: " + str(e) exit(code["Critical"]) try: browser["User.id"] = user browser["User.password"] = password except Exception as e: print "CRITICAL - Unable to fill form fields with username and password: " + str(e) exit(code["Critical"]) try: browser.submit() except Exception as e: print "CRITICAL - Unable to submit form: " + str(e) exit(code["Critical"]) try: title = browser.title() except Exception as e: print "CRITICAL - Unable to get web page title: " + str(e) exit(code["Critical"]) if(title == expectedTitle): print "OK - Expected web page title received: " + title + " | logon_time=" + str(int(round(1000*time.clock() - startTime))) + "ms" exit(code["OK"]) else: print "CRITICAL - Unexpected web page title received: " + title exit(code["Critical"])