My own config generator and autoloader part 3 – Verification

Hello

In part 2 i uploaded the config using Telnet. Now, I don’t exactly trust telnet + my connection to the company install center is sometimes… not very trustworthy. Therefore, it would be good to verify the config against what was intended. To do that, i’ve extended the last upload script.

import pexpect
import os
import sys
from pathlib import Path

#now, the main method also includes a command that can be run, e.g. show run, creates a text file with the command output and copies it back to the windows folder. 

def createall(ip,port,consoleserver,argcommand):
   myarraypath = str(Path.home()).split('/')
   table = { 39 : None }
   myfinaluser = myarraypath[2].translate(table)
   print(myfinaluser)
   clearcommand = "truncate -s 0 c.txt"
   os.system(clearcommand)
   command = "tail --lines=+1 /mnt/c/Users/" + myfinaluser + "/Desktop/mycurrentscripts/test.txt >> /home/" + myfinaluser + "/c.txt"
   os.system(command)
   child = pexpect.spawn('telnet ' + ip + ' ' + port)
   argcommandnew = argcommand
   commandfile = open(argcommandnew, 'wb')
   child.timeout = 50
   child.delaybeforesend = 1.0
   myconsoleserver = consoleserver
   child.expect(consoleserver + ' login:')
   child.sendline('hereyourusername')
   child.expect('Password:')
   child.sendline('hereyourpassword')
   child.sendline()
   neverknow = child.expect(['#', '>'])
   if neverknow==1:
      child.sendline('en')
      child.expect('Password:')
      child.sendline('myenablepassword')
      child.expect('#')
   elif neverknow==0:
      child.sendline('configure terminal')
      child.expect('#')
      child.sendline('do terminal length 0')
      child.expect('#')
      child.logfile = commandfile
      child.sendline('do ' + argcommandnew)
      child.expect('(config)#')
   copycommand = 'cp ./' + commandfile + '/mnt/c/Users/' + myfinaluser + '/Desktop/mycurrentscripts/commandoutput'
   os.system(copycommand)
     # filename = 'c.txt'
     # with open(filename, encoding="utf8", errors='ignore') as file_object:
      #  lines = file_object.readlines()
       # for line in lines:
        #  line = line #+ "\015"
         # child.sendline(line)
#usage:
#python3 script.py 172.16.0.1 myconsoleserver 'show run'
if __name__ == "__main__":
  ip = sys.argv[1]
  port = sys.argv[2]
  consoleserver = sys.argv[3]
  argcommand = sys.argv[4]
  createall(ip, port, consoleserver, argcommand)


Now, you can use Notepad++ compare plugin to compare the two text files. At this point you will probably see that the two files are not identical due to peculiar formatting of the show run output. So then it’s time to go back to the template in the config python script and add extra spacing etc.

!
vrf definition 1
 rd 1:1
 !
 address-family ipv4
 exit-address-family
!
vrf definition 2
 description OtherVRF
 rd 2:1
 !
 address-family ipv4
 exit-address-family
!
vrf definition 3
 description VRF MGMT
 rd 3:1
 !
 address-family ipv4
 exit-address-family
!

Finally, don’t do”show run” but ”show run brief” to verify the config. This will remove the certificates from the output (less differences to look at in notepad). You will never get this 100% right, but in the end it looks pretty good.

 

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google

Komentujesz korzystając z konta Google. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Połączenie z %s