You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Mechanize开发路由器重启Bot:非表单按钮触发及页面跳转问题

解决路由器重启按钮的触发问题

Hey there! Let's get that router reboot bot up and running. Since the reboot button isn't a standard form element, we need to dig into how the button actually triggers the restart action—most likely it's either a hidden form, a direct HTTP request (GET/POST), or a JavaScript-triggered call. Here's how to tackle it step by step:

Step 1: Navigate to the Reboot Page First

After logging in successfully, you need to get to the page with the reboot button. You can either follow the relevant link from the dashboard or directly access its URL if you know it.

Using mechanize, you can scan for links that lead to the reboot/system settings page:

# After successful login
content = response.read()
if b"wifi" in content:
    print("Login successful!")
    
    # Look for links to the reboot/system page
    reboot_link_found = False
    for link in br.links():
        # Adjust the keywords to match your router's UI (e.g., "重启", "System", "Reboot")
        if "重启" in link.text or "system" in link.url.lower():
            br.follow_link(link)
            reboot_link_found = True
            print("Navigated to reboot page")
            break
    
    # If no link found, manually open the reboot page URL (find this from your browser's address bar)
    if not reboot_link_found:
        br.open("http://192.168.0.1/cgi-bin/system_reboot.cgi")  # Replace with actual URL

Step 2: Trigger the Reboot Action

Now that you're on the right page, you need to replicate what the reboot button does. Here are the common scenarios:

Scenario 1: Reboot is triggered by a hidden form

Some routers use a hidden form that gets submitted when you click the button. Try selecting and submitting the form:

try:
    # Select the form (use nr=0 if it's the only form, or use name/id if available)
    br.select_form(nr=0)
    br.submit()
    print("Reboot request sent via form submission!")
except mechanize.FormNotFoundError:
    # If no form exists, move to the next scenario
    pass

Scenario 2: Reboot is a direct HTTP request (GET/POST)

Most routers use a simple HTTP call to trigger reboot. To find this:

  1. Right-click the reboot button in your browser, select "Inspect"
  2. Check the onclick attribute or look at the network tab in dev tools when you click the button

If it's a GET request, just open the URL:

br.open("http://192.168.0.1/cgi-bin/reboot.cgi")  # Replace with actual URL

If it's a POST request with parameters, send the data directly:

# Example: Send POST data with action=confirm
br.open("http://192.168.0.1/reboot", data=b"action=confirm")
print("Reboot request sent via POST!")

Full Updated Code

Putting it all together, here's your complete bot code:

import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)
# Handle page refreshes (common after login/reboot)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# Login to router
response = br.open("http://192.168.0.1/")
br.select_form(nr=0)
br.form['loginUsername'] = 'support'
br.form['loginPassword'] = '71689637'
response = br.submit()

# Verify login success
content = response.read()
if b"wifi" in content:
    print("Login successful!")
    
    # Navigate to reboot page
    reboot_link_found = False
    for link in br.links():
        if "重启" in link.text or "system" in link.url.lower():
            br.follow_link(link)
            reboot_link_found = True
            break
    if not reboot_link_found:
        br.open("http://192.168.0.1/cgi-bin/system_reboot.cgi")  # Update this URL
    
    # Trigger reboot
    try:
        br.select_form(nr=0)
        br.submit()
        print("Reboot initiated via form!")
    except mechanize.FormNotFoundError:
        # Fallback to direct POST/GET request (update URL and data as needed)
        br.open("http://192.168.0.1/cgi-bin/reboot.cgi", data=b"action=reboot")
        print("Reboot initiated via direct request!")
else:
    print("Login failed. Check username/password!")

Pro Tips

  • Always check your router's page source or browser dev tools to find the exact reboot endpoint—this will save you guesswork.
  • Mechanize doesn't support JavaScript, so you have to mimic the underlying HTTP request that the JS would send.
  • Some routers require a confirmation step (like clicking "Yes" after hitting reboot), so you might need to add an extra step to handle that.

内容的提问来源于stack exchange,提问作者alex boal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:47:46