Tuesday, March 16, 2010

Reboot Huawei SmartAX MT882 router from CLI

Reboot Huawei SmartAX MT882 router from CLI

#! /usr/bin/env python

import sys;
import telnetlib;

def establish_conn(host):
    tn = telnetlib.Telnet();
    tn.open(host);
    return tn;

def login(tn, user, passwd):
    tn.read_until("Login: ");
    tn.write(user + "\r\n");
    tn.read_until("Password: ");
    tn.write(passwd + "\r\n");
    return None;

def exec_cmd(tn, cmd, prompt):
    tn.read_until(prompt);
    tn.write(cmd + "\r\n");
    return None;

def reboot_router(tn, username, passwd):
    login(tn, username, passwd);
    exec_cmd(tn, "console enable\n", "--> ");
    exec_cmd(tn, "restart", "Quantum> ");
    return None;

if __name__ == "__main__":
    if len(sys.argv) != 4:
        print "Usage: python", sys.argv[0], "<Router IP> <username> <password>";
        sys.exit(0);
    tn = establish_conn(sys.argv[1]);
    reboot_router(tn, sys.argv[2], sys.argv[3]);
    print "Router rebooted!";

Author: mwnn <mwnnlin AT gmail DOT com>

HTML generated by org-mode 6.21b in emacs 23

No comments:

Post a Comment