Skip to content

Instantly share code, notes, and snippets.

@gshimansky
Last active November 10, 2019 02:10
Show Gist options
  • Save gshimansky/4cf1f1607fdc9663adf97c36b9700c5b to your computer and use it in GitHub Desktop.
Save gshimansky/4cf1f1607fdc9663adf97c36b9700c5b to your computer and use it in GitHub Desktop.
import sys
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
devices = {
'nintendo': 36,
'samsung': 31,
'ps4': 32,
'lgtv': 33,
'roku': 34
}
def usage():
print("xvfb-run python3 packet_filter_control.py {-e|-d|--help|-h} ", end='')
str = '['
for name, tag in devices.items():
print(str + name, end='')
str = '|'
print("]")
print("-h, --help Print this message")
print("-e Enable corresponding devices")
print("-d Disable corresponding devices")
print("If no devices are listed, all of them are enabled or disabled")
def get_device_list(args, tag_str):
names = []
if len(args) == 2:
for name, tag in devices.items():
names.append(name)
else:
for n in args[2:]:
if n in devices:
names.append(n)
else:
print("Unknown device", n)
sys.exit()
tags = []
for n in names:
tags.append(tag_str + str(devices[n]))
return {'names': names, 'tags': tags}
if len(sys.argv) < 2 or len(sys.argv) >= 2 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
usage()
sys.exit()
elif sys.argv[1] == "-d":
listed_devices = get_device_list(sys.argv, 'Complete_')
print("Disabling", listed_devices['names'])
state_str = "off"
elif sys.argv[1] == "-e":
listed_devices = get_device_list(sys.argv, 'UnComplete_')
print("Enabling", listed_devices['names'])
state_str = "on"
else:
usage()
device_actions = dict(zip(listed_devices['names'], listed_devices['tags']))
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
driver.get("http://192.168.1.254/cgi-bin/packetfilter.ha")
print("Before login:", driver.title)
# Login if we're asked for router key
if driver.title == "Login":
passwordElement = driver.find_element_by_name("password")
passwordElement.send_keys("my router password")
inputElement = driver.find_element_by_name("Continue")
inputElement.click()
WebDriverWait(driver, 15).until(EC.title_contains("Packet Filter"))
print("After login", driver.title)
for name, tag in device_actions.items():
button = None
try:
button = driver.find_element_by_name(tag)
print("button = ", button)
except NoSuchElementException:
print(name, "is already turned", state_str)
if button != None:
button.click()
WebDriverWait(driver, 15).until(EC.title_contains("Packet Filter"))
print("After click", driver.title)
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment