My own config generator and autoloader part 3 – Verification


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)
   clearcommand = "truncate -s 0 c.txt"
   command = "tail --lines=+1 /mnt/c/Users/" + myfinaluser + "/Desktop/mycurrentscripts/test.txt >> /home/" + myfinaluser + "/c.txt"
   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:')
   neverknow = child.expect(['#', '>'])
   if neverknow==1:
   elif neverknow==0:
      child.sendline('configure terminal')
      child.sendline('do terminal length 0')
      child.logfile = commandfile
      child.sendline('do ' + argcommandnew)
   copycommand = 'cp ./' + commandfile + '/mnt/c/Users/' + myfinaluser + '/Desktop/mycurrentscripts/commandoutput'
     # 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)
#python3 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
vrf definition 2
 description OtherVRF
 rd 2:1
 address-family ipv4
vrf definition 3
 description VRF MGMT
 rd 3:1
 address-family ipv4

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.



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


Komentujesz korzystając z konta 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