ASUS Router Firmware Upgrade Notification

I have written this pice of code to let me know when there is a new firmware upgrade of my router. This should work on most of the ASUS Routers. I’ve tested only on RT-AX86U. I’ve scheduled this from my PC to run once a day.

I’m using Pushover to get the notifications on my Iphone, but you could use any method.

import paramiko
import http.client
import urllib

# RT-AX86U
def sendnotification(MyTitle):
    conn = http.client.HTTPSConnection("api.pushover.net:443")
    conn.request(
        "POST",
        "/1/messages.json",
        urllib.parse.urlencode(
            {
                "token": "<put your token here>",
                "user": "<your username here>",
                "message": "Firmware Update " + MyTitle,
            }
        ),
        {"Content-type": "application/x-www-form-urlencoded"},
    )
    conn.getresponse()


def ssh_get_fw(version_type):

    # SSH connection parameters
    hostname = "<HOST IP>"
    port = 22
    username = "<USERNAME>"
    password = "<PASSWORD>"

    # Create an SSH client
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        # Connect to the router
        client.connect(
            hostname, port=port, username=username, password=password, timeout=10
        )

        # Execute the command
        stdin, stdout, stderr = client.exec_command(version_type)

        # Read the output
        firmware_version = stdout.read().decode().strip().replace("_", "")

    except paramiko.AuthenticationException:
        print("Authentication failed. Please check your username and password.")
    except paramiko.SSHException as e:
        print("SSH connection failed:", str(e))
    finally:
        # Close the SSH connection
        client.close()
    return firmware_version


# The webs_state_info will change once the router goes off and does it check
merlin_version = ssh_get_fw("nvram get webs_state_info")[4:]
build_version = ssh_get_fw("nvram get buildno").replace(".", "")
extend_no = ssh_get_fw(" nvram get extendno")

result = int(str(build_version) + str(extend_no))
print(f"Merlins Web version is {merlin_version}")

print(f"current firmware version is {result}")
if int(merlin_version) > result:
    print("please update the firmware")
    sendnotification(result)
else:
    print("Nothing to update")
    sendnotification("Nothing to update")

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.