Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import urllib2
- import re
- import time
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support import expected_conditions as EC
- from time import gmtime, strftime
- from datetime import datetime
- # Function that retrieves the table with all traffic data from the admin interface (refreshes periodically automatically) and then checks each entry against the threshold.
- def checkNetworkIndividual():
- tableContent = driver.find_element_by_id("bwm-details-grid")
- soup = BeautifulSoup(tableContent.get_attribute('innerHTML'))
- count = 0
- for data in soup.find_all("tr"):
- if("traffictable_footer" in str(data)):
- break
- prettyData = data.prettify()
- total = prettyData.splitlines()
- for entry in total:
- if("MB" in entry ):
- count = count + 1
- #print(count)
- number = float(entry.strip('MB '))
- if(number > 0.50):
- checkViolation(total[3], total[6])
- return
- # If someone is downloading too much, give them a strike, if they get 5 strikes, set of the alarm.
- # Strikes are reset after a timeout.
- def checkViolation(name, ip):
- global last_ip
- global violations
- global alarm_active
- global timestamp
- difference = datetime.now() - timestamp
- timestamp = datetime.now()
- if(ip == last_ip and difference.total_seconds() < 20.0):
- violations = violations + 1
- else:
- last_ip = ip
- violations = 1
- if(violations > 4 and alarm_active == False):
- alarm_active = True
- alert(name, ip)
- time.sleep(14)
- violations = 0
- alarm_active = False
- return
- # Update the screen with the name and IP of the violator (terrible)
- # The html page is hosted with apache in this case, so it can be accesed from a different machine
- # And call a shell script that handles the lights and sound.
- # Lights are controlled with a RF receiver using http://wiringpi.com/ and this example code https://www.dropbox.com/s/uc1d4igaeviys4a/lights.zip?dl=1
- # Tutorial I used in Ducth: https://weejewel.tweakblogs.net/blog/8665/lampen-schakelen-met-een-raspberry-pi.html
- def alert(name, ip):
- f = open('/var/www/html/index.html','w')
- message = """<html>
- <meta http-equiv="refresh" content="25; URL=http://192.168.1.123">
- <head>
- <link rel="stylesheet" type="text/css" href="mystyle.css">
- </head>
- <body style="background: red; margin-top: 150px;">
- <style>
- h1 {
- font-size: 150px;
- text-align: center;
- }
- h2 {
- font-size: 120px;
- text-align: center;
- }
- h1, h2 {
- animation: blink-animation 1s steps(5, start) infinite;
- -webkit-animation: blink-animation 1s steps(5, start) infinite;
- }
- @keyframes blink-animation {
- to {
- color: white;
- }
- }
- @-webkit-keyframes blink-animation {
- to {
- color: white;
- }
- }
- </style>
- <h1>""" + name + """</h1>
- <h2>""" + ip + """</h2>
- </body>
- </html>"""
- f.write(message)
- f.close()
- import subprocess
- subprocess.Popen("./alarm.sh", shell=True)
- time.sleep(10)
- defaultHTML()
- return
- # Reset the screen to the default black state.
- def defaultHTML():
- f = open('/var/www/html/index.html','w')
- message = """<html>
- <meta http-equiv="refresh" content="5; URL=http://192.168.1.123">
- <head></head>
- <body style="background: black;">
- <h1>Stand by</h1>
- </body>
- </html>"""
- f.write(message)
- f.close()
- return
- # Location of the router admin interface
- host = '192.168.1.1'
- trafic_url = 'http://' + host + '/Main_TrafficMonitor_devrealtime.asp'
- # Use selenium to log into the admin control panel of the router
- driver = webdriver.Firefox()
- driver.get(trafic_url)
- username = driver.find_element_by_name("login_username")
- password = driver.find_element_by_name("login_passwd")
- username.send_keys("Username")
- password.send_keys("SomePassword")
- driver.find_element_by_class_name("button").click()
- wait = WebDriverWait(driver, 20)
- element = wait.until(EC.presence_of_element_located((By.ID, 'bwm-details-grid')))
- defaultHTML()
- violations = 0
- alarm_active = False
- last_ip = ""
- timestamp = datetime.now()
- # Call the traffic check function each second for al eternity
- while (True):
- checkNetworkIndividual()
- time.sleep(1)
Add Comment
Please, Sign In to add comment