Reboot a MikroTik router with SNMP set (Python Script)

Reboot a MikroTik router with SNMP set (Python Script)

Here is a proof of concept python script I wrote to remotely reboot a MikroTik router using a SNMP set command. SNMP must be enabled on the router and a community string must be setup with write access for this script to work.

When executed with a valid community string the target router will immediately reboot. I understand this is somewhat novel but someone may find it useful.

The script has two python dependencies python-fire and pysnmp. Here are the commands to install these libraries.

pip install fire
pip install pysnmp

Command line arguments

C:\> python --help

INFO: Showing help with the command ' -- --help'.

NAME - Reboot MikroTik router with SNMP


    Reboot MikroTik router with SNMP



    You can also use flags syntax for POSITIONAL ARGUMENTS

Usage Example

C:\> python --community=my-write-string Source Code

import fire
from pysnmp.hlapi import *

def reboot(host, community="public", port=161):
    """Reboot MikroTik router with SNMP"""
    errorIndication, errorStatus, errorIndex, varBinds = next(
            CommunityData(community, mpModel=0),
            UdpTransportTarget((host, port)),
            ObjectType(ObjectIdentity(""), OctetString("1")),

    if errorIndication:
    elif errorStatus:
            "%s at %s"
            % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or "?",)
        for varBind in varBinds:
            print(" = ".join([x.prettyPrint() for x in varBind]))

if __name__ == "__main__":


The script is very short, sweet and to the point. It leverages the python-fire library from Google and pysnmp does the heavy lifting when it comes to SNMP.

I am also working on a similar script that first checks the router’s uptime and only reboots the device if it has been up for x number of days. Would this be useful to anybody? Let me know in the comments below.

More MikroTik Articles

More Python Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

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