Python Forum

Full Version: working ciscoconfparse code, suggestion please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

below script take switches IP from file and create threads to take backup of each switch and ciscoconfparse, parse config file and print output whether interface is in Trunk mode or Access mode.

Took help from my other scripts and Ciscoconfparse docs.
python3.6

#!/usr/bin/python
# This program is to check switch port is in trunk mode for access mode.
# date:- 19/08/2018 21:44
from ciscoconfparse import CiscoConfParse
import datetime
import threading
import sys
import os
import unidecode
import telnetlib
import time
import re
from time import sleep
start = time.time()
global trunk_intfs_mode 
global access_intfs_mode
trunk_intfs_mode = []
access_intfs_mode= []
user = 'xxx'
password = 'xxx@123'
enapassword = "xxxxPp53N"
start = time.time()
def open_telnet(host):
         timeout = 10
         sr_no = 0
         try:
            session = telnetlib.Telnet(host, 23, timeout)
            #session.set_debuglevel(2)
            time.sleep(1)
            session.read_until(b"Username:")
            session.write(user.encode('ascii')+b"\r\n")
            time.sleep(2)
            session.read_until(b"Password:",2)
            session.write(password.encode('ascii')+ b"\r\n")
            time.sleep(2)
            session.read_until(b">")
            session.write(b"enable\r\n")
            session.read_until(b"Password:",2)
            session.write(enapassword.encode('ascii')+b"\r\n")
            session.read_until(b"#",2)
            session.write(b"term len 0\r\n")
            session.write(b"show running-config\r\n")
            time.sleep(10)
            session.write(b"exit\n\r")
            output = session.read_all().decode('ascii')
            time.sleep(10)
            filename = host+".conf"
            fp=open(filename,"w")
            fp.write(output)
            fp.close()
            cisco_cfg = CiscoConfParse(filename)
            trunk_intfs_mode = [obj for obj in cisco_cfg.find_objects(r"^interf") if obj.re_search_children(r"trunk")]
            access_intfs_mode = [obj for obj in cisco_cfg.find_objects(r"^interf") if obj.re_search_children(r"access")]
            for t_interface in trunk_intfs_mode:
                trunk_interface_mode = t_interface.text.strip().split(' ')[1]
                print("{}\t{}\tTrunk\t".format(host,trunk_interface_mode ))
            for a_interface in access_intfs_mode:
                access_interface_mode = a_interface.text.strip().split(' ')[1]
                print("{}\t{}\taccess\t".format(host,access_interface_mode ))
            session.close()
         except Exception as excp:
                print('time out:- {}\n '.format(host))

def create_threads():
    threads = []
    with open('bs.txt','r') as ipfile:
        for sr_no, line in enumerate(ipfile, start=1):
            host = line.strip()
            th = threading.Thread(target = open_telnet ,args = (host,))
            th.start()
            threads.append(th)
        for thr in threads:
            thr.join()

if __name__ == "__main__":
        create_threads()
        print ("Exiting the program")
        print('It took', time.time()-start, 'seconds.')
sample output

Output:
1x.21.160.52 FastEthernet0/20 access 1x.21.162.61 FastEthernet0/22 access 1x.21.160.52 FastEthernet0/22 access 1x.21.162.61 FastEthernet0/23 access 1x.21.160.35 FastEthernet0/15 access 1x.21.160.35 FastEthernet0/16 access 1x.21.160.35 FastEthernet0/20 access 1x.21.160.35 FastEthernet0/21 access 1x.21.160.35 FastEthernet0/22 access 1x.21.160.35 FastEthernet0/23 access 1x.21.160.35 FastEthernet0/24 access
Hello,
Is this a completed working script that functions as intended, but you would like to hear comments/suggestions?
yes, this is working script, comments and suggestion please for improvement.