Skip to content

Instantly share code, notes, and snippets.

@Sucareto
Last active April 10, 2023 02:43
Show Gist options
  • Save Sucareto/19a156fc825afe29fc3dfadf2338511e to your computer and use it in GitHub Desktop.
Save Sucareto/19a156fc825afe29fc3dfadf2338511e to your computer and use it in GitHub Desktop.
电信宽带重拨号更换 IP 的自动化方式。适用于 WSL2。
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from requests import get
from requests.exceptions import RequestException
# from selenium.webdriver import Remote
query_ip_url = "https://4.ipw.cn"
# query_ip_url = "https://ipinfo.io"
print("正在初始化浏览器...")
chrome_options = ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
# chrome_options.add_argument('--start-maximized')
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])
# driver = Remote(
# command_executor='http://172.31.192.1:4444',
# options=chrome_options
# )
driver = Chrome(options=chrome_options)
print("浏览器初始化完成。")
print("当前的 IP 是:{}".format(get(query_ip_url).text))
# driver.implicitly_wait(5)
driver.get("http://192.168.1.1/cgi-bin/luci")
try:
username_tag = driver.find_element(By.ID, "login_username")
username_tag.clear()
username_tag.send_keys("telecomadmin")
driver.find_element(By.ID, "login_password").send_keys("nE7jA%5m")
driver.find_element(By.CLASS_NAME, "btn").click()
# driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div[2]/form/button").click()
driver.switch_to.frame("contentfrm")
driver.find_element(By.LINK_TEXT, "网络").click()
Select(driver.find_element(By.ID, "wanId")).select_by_visible_text(
"2_INTERNET_R_VID_41"
)
driver.find_element(By.ID, "btnOK").click()
driver.quit()
except Exception as err:
print("网页操作失败:{}".format(str(err)))
print("切换完成,等待网络恢复", end="")
while True:
try:
newip = get(query_ip_url, timeout=0.1).text
break
except RequestException:
print(".", end="")
print("\n现在的 IP 是:{}".format(newip))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment