Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fahimshahrierrasel/dac351e8fd95bbe1486794dcc5a47ab4 to your computer and use it in GitHub Desktop.

Select an option

Save fahimshahrierrasel/dac351e8fd95bbe1486794dcc5a47ab4 to your computer and use it in GitHub Desktop.
Dlink DIR-600M ACL Enable and Disable Selenium Automation
#!/usr/bin/python
import sys, logging
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
def main(password, acl_status):
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options)
try:
driver.get("http://192.168.0.1")
password_field = driver.find_element_by_name("password")
password_field.clear()
password_field.send_keys(password)
login_btn = driver.find_element_by_id("loginBtn")
login_btn.click()
try:
WebDriverWait(driver, 3).until(EC.alert_is_present(), 'Waiting for alert about logged in from other device.')
logging.info("Logged in from other device alert appeared")
alert = driver.switch_to.alert
alert.accept()
logging.info("Alert accepted")
except TimeoutException:
logging.info("No alert for logged in from other device")
driver.get("http://192.168.0.1/wlan_basic.htm")
driver.implicitly_wait(2)
driver.get("http://192.168.0.1/wladvanced.htm")
driver.implicitly_wait(2)
acl_set_btn = driver.find_element_by_name("setupList")
acl_set_btn.click()
driver.implicitly_wait(2)
acl_status_checkbox = driver.find_element_by_name("wlanAcEnabled")
is_acl_enabled = acl_status_checkbox.get_attribute("value") == "1"
if is_acl_enabled != acl_status:
logging.info(
"previous status {0} new status will be {1}".format(
"enable" if is_acl_enabled else "disable",
"enable" if acl_status else "disable",
)
)
acl_status_checkbox.click()
acl_apply_btn = driver.find_element_by_name("deleteApplySubmit")
acl_apply_btn.click()
else:
logging.info(
"previous status and new status is same as {0} skipping...".format(
"enable" if acl_status else "disable"
)
)
driver.implicitly_wait(2)
except Exception as ex:
logging.error(ex)
finally:
logging.info("Closing browser")
driver.close()
driver.quit()
if __name__ == "__main__":
logging.basicConfig(
filename="acl_status.log",
level=logging.INFO,
filemode="a",
format="%(asctime)s-%(name)s-%(levelname)s: %(message)s",
datefmt='%Y-%m-%d %H:%M:%S'
)
if len(sys.argv) is not 3:
print(
"Can't run not enough argument!!! Ex: change_acl_status.py <router_password('password')> <new_acl_status(0|1)>"
)
sys.exit()
if sys.argv[2] not in {"0", "1"}:
print("Incorrect new acl status!!! Ex: 0 or 1")
sys.exit()
new_status = sys.argv[2] is "1"
logging.info(
"Calling seleinum to {0} ACL".format("enable" if new_status else "disable")
)
display = Display(visible=False, size=(800,600))
display.start()
main(sys.argv[1], new_status)
display.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment