Testing menu and netmiko in python

Background
This is a testing code to try out on a simple interactive text menu with two options, download cisco config and quit.
I want to extend this config to have an interactive menu and prompt to configure cisco routers.

Here’s the code:

from netmiko import ConnectHandler
import os.path
from datetime import datetime

#Main menu function
def main_menu():
    print(30 * "-","Menu",30 * "-" )
    print("1. Download Current Config")
    print("2. Quit")
    print(60 * "_")

#dictionary of R2 parameters, secret is an optional requirement for ConnectHandler
#secret is actually for enable secret if local user account has no privilege 15
R2 = {'device_type': 'cisco_ios', 'ip': '192.168.1.247', 'username': 'cisco', 'password': 'cisco', 'secret': 'cisco'}

#this is to prepare for connection and go directly to privilege exec mode
def r2_connect():
    try:
        r2 = ConnectHandler(**R2)
        r2.enable()
        return  r2
    except IOError:
        print("There is a problem connecting to router, please check if router is online")

#Loop the menu until it is quit is selected
loop = True
while loop:
    main_menu()
    try:
        choice = int(input("What do you want to do?: [1-2]"))
    except ValueError:
        print("Oops you have entered an invalid number.")
        continue
        
    if choice == 1:
        print("starting backup...")
        r2 = r2_connect()
        r2.send_command('terminal length 0')
        output = r2.send_command_expect('sh run')
        date = datetime.today().strftime('%d%m%y-%H%M')
        path = 'd:/temp/'
        filename = os.path.join(path, "config"+date+".cfg")
        config = open(filename,"w")
        config.write(output)
        config.close()
        r2.disconnect()
        print("backup finished")
    elif choice == 2:
        loop = False
    else:
        print("Invalid choice selected")

The output
Invalid selection

------------------------------ Menu ------------------------------
1. Download Current Config
2. Quit
____________________________________________________________
What do you want to do?: [1-2]4
Invalid choice selected


------------------------------ Menu ------------------------------
1. Download Current Config
2. Quit
____________________________________________________________
What do you want to do?: [1-2]e
Oops you have entered an invalid number.

Download config selection

------------------------------ Menu ------------------------------
1. Download Current Config
2. Quit
____________________________________________________________
What do you want to do?: [1-2]1
starting backup...
backup finished

Quit menu selection

------------------------------ Menu ------------------------------
1. Download Current Config
2. Quit
____________________________________________________________
What do you want to do?: [1-2]2

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s