📜 ⬆️ ⬇️

Centralized collection of configurations from MikroTik by means of Python

What for



With the growing number of network nodes and the complexity of their configuration, many probably have a question - and if the piece of hardware dies, can I quickly restore work to another? If I can do it manually, then how long will I then catch the little things that I forgot?

The googling process has led to an understanding of what specifically for this vendor there are no ready-made solutions of this kind. I did not like what was found in the community - they suggested placing backup scripts on the piece of hardware itself and running them on a schedule. In fact, this solved the problem, but if I have several dozen pieces of equipment, clicking copy-paste on each copy is not according to Feng Shui.
')
Fortunately, there is a little Python utility writing skill and fantasy.


Fantasy



I think in my mind what is needed - a TFTP server, where we will put our farm, * nix machine (it is easier with it than under win to do it all), Python is on board with the necessary set of libraries. As it turned out, you can throw out the TFTP server, you will need FTP.

Attempt once


I try to solve the problem in the forehead - I use telnetlib for communication - it does not work. Strange, because with the switches worked. Having thought it over, I realized that telnet from MikroTik is decorated with all the colors of the rainbow, it means pouring special characters, and it is not my filter to filter them.

Attempt two


I look closely at the paramiko library and its SSHClient component - now everything works out - the connection passes, I can execute commands and get the result.

Now we will understand how the configurations are removed from these really unusual pieces of iron. The usual script on the network equipment is the execution of one command that can send its config to the TFTP server. In the case of MikroTik, this option does not work - it turns out that the first thing to do is to create a backup of the config:

/system backup save [name=]

After that, the file can already be downloaded somewhere, but as it turned out, this can only be done via FTP, and via HTTP and TFTP it can only merge files. It does not matter, we quickly raise an FTP server with a minimal configuration, which will not be difficult for dear readers to google.

/tool fetch address= mode=ftp dst-path= src-path= user= password= upload=yes

And in the end you need to clean up the trash, so as not to sail to the exhaustion of free space on the internal carrier:

/file remove

Result


A couple of hours fantasy gave birth to this script:

 #!/usr/bin/env python # -*- coding: utf-8 -*- # for SSH from paramiko import SSHClient from paramiko import AutoAddPolicy # for versioning import datetime # for file operations import os # for sleep import time # versioning Version = datetime.date.today() #print "\n" + str(Version) # hosts array IP1, IP2, IP3 hosts = ( "1.2.3.4", "5.6.7.8" , "9.10.11.12") # username users = ( "user1", "user2", "user3") iterUser = iter(users) # userpassword passwords = ( "pass1", "pass2", "pass3" ) iterPassword = iter(passwords) # FTPD IP FtpdIP = "13.14.15.16" # ftp user account ftpUser = "ftpuser" ftpPass = "ftppass" # keep backups for 4 weeks backtime = datetime.timedelta(weeks=-4) sshCli = SSHClient() sshCli.set_missing_host_key_policy(AutoAddPolicy()) print "header done" # loop host adresses for host in hosts: print "\n" + str(host) # iterate through user-password pairs user = iterUser.next() Password = iterPassword.next() # define operations CreateLocalBckp = "system backup save name=" + str(host) + "_" + str(Version) + ".backup" UploadToFtp = "tool fetch address=" + str(FtpdIP) + " mode=ftp dst-path=" + str(host) + "_" + str(Version) + ".backup src-path=" + str(host) + "_" + str(Version)+ ".backup" + " user=" + str(ftpUser) + " password=" + str(ftpPass) + " upload=yes" RemoveLocalBckp = 'file remove "' + str(host) + "_" + str(Version) + ".backup" + '"' # try for not to fail the whole script on one error try: print "connecting.." + str(host) + "@" + str(user) + ":" + str(Password) sshCli.connect(str(host), port=2022, username=str(user), password=str(Password)) print "connected.." # creating local backup print "creating local backup.. /" + CreateLocalBckp sshCli.exec_command(CreateLocalBckp) # sleep after each command because mikrotik can not do it so fast as script executes time.sleep(2) print "local backup created.." # uloading local backup to ftp print "uploading local backup to ftp.. /" + UploadToFtp sshCli.exec_command(UploadToFtp) time.sleep(2) print "backup uploaded to remote location.." # removing local backup time.sleep(2) print "removing local backup.. /" + RemoveLocalBckp sshCli.exec_command(RemoveLocalBckp) time.sleep(2) print "local backup removed.." sshCli.close() # try delete old file (if exists) try: os.remove("/tftp/" + str(host) + "_" + str(Version + backtime) +".cfg") except: print "Error while trying to delete old backup " + "/tftp/" + str(host) + "_" + str(Version + backtime) +".cfg" except: print "Error connecting to host", host 


The script will store on our FTP server a set of backup configurations for the month with names like IP_YYY-MM-DD.backup

PS: there is a similar creation for the case of 3Com switches and uploading their configurations via telnet to TFTP - if readers are interested, be sure to publish.

Source: https://habr.com/ru/post/135541/


All Articles