def is_internet_on_classroom(classroom): print 'asasas 1.1' network_type=classroom.network_device.network_type print 'asasas 1.2' firewall_rule=classroom.firewall_rule classroom_pcs=classroom.computer_set.all().order_by('identifier') #First Classroom computer pc1=None if classroom_pcs: pc1=classroom_pcs[0] if network_type=="Mikrotik": #try: mk =MikrotikRouter() mk.set_networkdevice(classroom.network_device) print 'asasas 1.3' #If we block the classroom only taking into account MAC addresses if classroom.mac_filter: if pc1: resposta = mk.list_firewall_rule(settings.FW_PREFIX+' '+pc1.mac.upper()) else: print 'asasas 1.4 '+firewall_rule.comment resposta = mk.list_firewall_rule(firewall_rule.comment,firewall_rule.src_address,str(firewall_rule.src_netmask)) print 'asasas 1.5' if resposta: return False else: return True #except Exception ,msg: # return False else: return True
def is_barra_lliure_set(classroom_id): classroom_=Classroom.objects.get(id=classroom_id) network_type=classroom_.network_device.network_type if network_type=="Mikrotik": try: mk =MikrotikRouter() mk.set_networkdevice(classroom_.network_device) return mk.is_firewall_nat_rule_enabled("src-nat-aula-27") except Exception, e: response.write("EERROR: "+str(e))
def is_internet_partial_on_classroom(classroom): network_type=classroom.network_device.network_type firewall_rule=classroom.firewall_rule if network_type=="Mikrotik": mk =MikrotikRouter() mk.set_networkdevice(classroom.network_device) resposta = mk.list_firewall_rule("LLISTA-"+str(classroom.id)) if resposta: return resposta[0]['dst-address-list'] else: return None else: return None
def allow_by_mac(request,pc_id,pc_state): if not request.user.is_authenticated(): return HttpResponseRedirect('/login/') response = HttpResponse() pc=Computer.objects.get(id=pc_id) network_type=pc.classroom.network_device.network_type if network_type=="Mikrotik": try: mk =MikrotikRouter() mk.set_networkdevice(pc.classroom.network_device) mk.login() if pc_state == "ON": mk.add_firewall_rule_by_mac(pc.mac,"accept") else: mk.delete_firewall_rule_by_mac(pc.mac,"accept") response.write(""); except Exception, e: response.write("ERROR: "+str(e))
def remove_list_network_device(request,classroom_id): response = HttpResponse() classroom_=Classroom.objects.get(id=classroom_id) network_type=classroom_.network_device.network_type if network_type=="Mikrotik": try: mk =MikrotikRouter() mk.set_networkdevice(classroom_.network_device) mk.delete_firewall_rule("POST-LLISTA-"+classroom_id) mk.delete_firewall_rule("LLISTA-"+classroom_id) except Exception, e: response.write("ERROR: "+str(e))
def set_barra_lliure(request,classroom_id,barralliure_id): response = HttpResponse() classroom_=Classroom.objects.get(id=classroom_id) network_type=classroom_.network_device.network_type #response.write("set barra lliure") if network_type=="Mikrotik": try: mk =MikrotikRouter() mk.set_networkdevice(classroom_.network_device) #response.write("ID barra lliure : "+classroom_id+" IS="+barralliure_id) if barralliure_id == "OFF": #La regla 8 es srcnat per a l'aula 27 HARD WIRED mk.enable_disable_firewall_nat_rule("src-nat-aula-27","0") else: #La regla 8 es srcnat per a l'aula 27 HARD WIRED mk.enable_disable_firewall_nat_rule("src-nat-aula-27","1") except Exception, e: response.write("EERROR: "+str(e))
def get_pcs_information(classroom,list_pcs_classroom): pcs=[] network_type=classroom.network_device.network_type if network_type=="Mikrotik": mk =MikrotikRouter() mk.set_networkdevice(classroom.network_device) mk.login() #print classroom for computer in list_pcs_classroom: print computer resposta = mk.list_firewall_rule_by_mac(computer.mac) if resposta: network_action=resposta[0]['action'] blocked = True else: network_action=None blocked = False pcs.append({'id':computer.id,'network_action':network_action,'identifier':computer.identifier,'mac':computer.mac,'resposta':resposta,'serial':computer.serial}) return pcs
def add_list_network_device(request,classroom_id,list_id): response = HttpResponse() url_list = UrlList.objects.get(id=list_id) url_list_items = url_list.url_list_items.all() classroom_=Classroom.objects.get(id=classroom_id) network_type=classroom_.network_device.network_type firewall_rule=classroom_.firewall_rule if network_type=="Mikrotik": try: mk =MikrotikRouter() mk.set_networkdevice(classroom_.network_device) mk.add_firewall_list(url_list_items,url_list.name) ''' Add drop firewall rule''' mk.delete_firewall_rule("POST-LLISTA-"+classroom_id) #If we don't have other choice and we use the less preferable way to block a classroom. Blocking using every single MAC #from the PCs of the Classroom if classroom_.mac_filter: mk.block_classroom_by_mac(classroom_,"POST-LLISTA-"+classroom_id) else: mk.add_firewall_rule("POST-LLISTA-"+classroom_id,str(firewall_rule.src_address),str(firewall_rule.src_netmask)) ''' Add accept firewall rule to destination list''' mk.delete_firewall_rule("LLISTA-"+classroom_id) mk.add_firewall_rule("LLISTA-"+classroom_id,"","",url_list.name,"accept") except Exception, e: response.write("ERROR: "+str(e))
def draw_classroom(request,classroom_id,internet_id): response = HttpResponse() if not request.user.is_authenticated(): return HttpResponseRedirect('/login/') now = datetime.datetime.now() classroom_=Classroom.objects.get(id=classroom_id) #admin=classroom_.network_device.admin #password=classroom_.network_device.password #ip=classroom_.network_device.ip network_type=classroom_.network_device.network_type mac_filter=classroom_.mac_filter firewall_rule=classroom_.firewall_rule #network_device_by_classroom= ApiNetworkDevice.objects.filter(id=classroom_net_device) if network_type=="Mikrotik": try: mk =MikrotikRouter() mk.set_networkdevice(classroom_.network_device) mk.delete_classroom_firewall_rules(classroom_) if internet_id == "OFF": #mk.delete_firewall_rule("POST-LLISTA-"+classroom_id) #mk.delete_firewall_rule("LLISTA-"+classroom_id) #If we filter classroom by mac because doesn't follow any IP range knowed if mac_filter: mk.block_classroom_by_mac(classroom_) else: mk.add_firewall_rule(firewall_rule.comment,str(firewall_rule.src_address),str(firewall_rule.src_netmask)) else: #mk.delete_firewall_rule(firewall_rule) if mac_filter: mk.delete_classroom_firewall_rules(classroom_) else: mk.delete_firewall_rule(firewall_rule.comment) # Esborrem residus de regles parcials quan internet On or OFF mk.delete_firewall_rule("POST-LLISTA-"+classroom_id) mk.delete_firewall_rule("LLISTA-"+classroom_id) except Exception, e: response.write("ERROR: "+str(e))