Cyberoam login script in python (updated, now with notifications)

Published: by

  • Categories:

At our institute, we are forced to login into cyberoam and then only we can surf the web. The logging process is either through WEB INTERFACE or using their client provided by them.

I didnt like any of the provided methods and also "the session logs out after every 1 hr" which is very irritating. So, few months ago, I wrote a script that simulates web login by using urllib in python.

This script basically asks u for the username and password first time you run the script and then logs in automatically after the specified interval…


#Author: Abhijeet Rastogi (

cyberroamIP = "" #The IP of the Cyberoam site.
cyberroamPort = "8090" #Set to "" if not using.
never_quit = True #Once started cyberthon will never, even when the cyberoam server cannot be connected.
reconnectAfter = 3500
use_notification = True

import sys
import getpass
from time import strftime
import cookielib
import urllib2
import re
import urllib, sgmllib,time,commands,os
import thread
import threading

 import pynotify
 pynotify.init("Cyberoam Login Script")
 print "You dont have pynotify installed, Notifications will not be shown :-( "
 use_notification = False

username = raw_input("Enter your ID: ")
username = username
passwd = getpass.getpass()

cyberroamAddress = cyberroamIP
if cyberroamPort != "":
 cyberroamAddress = cyberroamAddress+":"+cyberroamPort

#Parsing and logging in too.
import sgmllib

download_quota_remaining = 0

def netUsage():
 url = "http://"+cyberroamAddress+"/corporate/servlet/MyAccountManager"
 data = "mode=1&login_username=&secretkey=&js_autodetect_results=SMPREF_JS_OFF&just_logged_in=1&username="+username+"&password="+passwd+"&select=My+Account&soft_25.x=0&soft_25.y=0"
 cj = cookielib.CookieJar()
 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
 opener.addheaders = [('Referer','')]
 usock =, data)
 the_page =
 transfer_log = []
 for i in range(1,10):
	 start = the_page.find('')
	 the_page = the_page[start+53:]
	 end = the_page.find('     transfer_log.append(the_page[0:end])
 return transfer_log

def periodicNotification(threadname, show_again_after):
 while True:
  if use_notification == True:
   show_quota = pynotify.Notification("Cyberoam Script", "Remaining Download Quota: %s\nRemaining Upload Quota: %s" % (netUsage()[-1], netUsage()[-4]))
   print "Hello"

class MyCyberroamParser(sgmllib.SGMLParser):
 "A simple parser class."

 def parse(self, s):
  "Parse the given string 's'."

 def __init__(self, verbose=0):
  "Initialise an object, passing 'verbose' to the superclass."
	 sgmllib.SGMLParser.__init__(self, verbose)
  self.required_entities = ['message','loginstatus','liverequesttime']
  self.frames_attr = []
  self.in_required_entity = False
  self.current_entity = ""
  self.entity_values = {}

 def do_frame(self, attributes):
  for name, value in attributes:
   if name == "src":

 def unknown_entityref(self,ref):
  self.current_entity = ref
  if ref in self.required_entities:

 def handle_data(self, data):
  "Try to get the value of entity &message. Used in 2nd pass of parsing."

  if self.in_required_entity:
   self.entity_values[self.current_entity] = data[1:] #To remove the preceeding =
   self.in_required_entity = False

 def get_src(self,index=-1):
  "Return the list of src targets."
  if index == -1:
   return self.frames_attr
   return self.frames_attr[index]

lastmsg = ""
msgChanged = True
sec2sleepOnError = 6
changeUser = True

 while True: 
   # Logging in and fetching the Cyberroam login page.
   if changeUser == True:
	line = cred_file.readline()
	cred = line.split(" ")
	cred[1] = cred[1][:-1]
	username = str(cred[0])+""
	passwd = str(cred[1])
	#passwd = getpass.getpass()"""
   print "Username in use: "+username 
   f = urllib.urlopen("http://"+cyberroamAddress+"/corporate/servlet/CyberoamHTTPClient","mode=191&isAccessDenied=null&url=null&message=&username="+username+"&password="+passwd+"&saveinfo=saveinfo&login=Login")
  except IOError, (errno, strerror):
   if not silent:
	print "Connection to Cyberoam server timed out. Error(%s): %s" % (errno, strerror)
	print "Retrying in %s seconds" % sec2sleepOnError
  s =
  # Try and process the page.
  # The class should have been defined first, remember.
  myparser = MyCyberroamParser()

  # Get the the src targets. It contains the status message. And then parse it again for entity &message.
  qindex = myparser.get_src(1).index('?')
  srcstr = myparser.get_src(1)[:qindex+1]+'&'+myparser.get_src(1)[qindex+1:]


  message = myparser.entity_values['message']
  message = message.replace('+',' ')

  if message=="You have successfully logged in":
	thread.start_new_thread(periodicNotification,("Notification of Quota",900))
	changeUser = False
	download_quota_remaining = netUsage()[-1]
	transfer_log = netUsage()
	print "Total TRANSFER till now: "+str(transfer_log[2])
	print "Remaining Download QUOTA: "+str(transfer_log[-1])
	print "You have successfully logged in"
	if use_notification == True:
	 successful_login = pynotify.Notification("Cyberoam", "Login attempt successful \n Remaining Download QUOTA: %s" % download_quota_remaining)
   except KeyboardInterrupt:
	username=raw_input("Enter your ID: ")
	username = username+""
	passwd= getpass.getpass()

  if message == "The system could not log you on. Make sure your password is correct":
   print "Wrong password.. Enter again"
   passwd = getpass.getpass()
   changeUser = False

  if message == "DataTransfer limit has been exceeded":
   if use_notification == True:
	data_limit_exceeded = pynotify.Notification("Cyberoam", "DATA-TRANSFER limit for the day exceeded")
   print "Download Quota Exceeded"
   username=raw_input("Enter another ID: ")
   username = username+""
   passwd= getpass.getpass()
except KeyboardInterrupt: