Python to the rescue

Hi

Last year I set up a volume preparations workshop so as to provide ISP type customers with pre-staged/pre-installed CPEs.
I drew up a description of this workshop in the following post : Volume installation/preparation. Incidentally, following the Prague MTPC that was held in April of this year, there is also a video presentation of this workshop. Feel free to request a copy of my slides.

As you may have already discovered, automated setups are a godsend in stable situations. Ansible is a wonderful tool for automating installation and maintenance tasks, but sometimes you will find yourself in unforeseen circumstances which can’t readily be catered for by the automation system you had initially put in place.

It could be that the automated process failed halfway through its tasks and the only immediate way to fix it is to manually connect to each device, which doesn’t seem very efficient at all. However unappealing and taxing, the manual approach may actually end up being less time consuming than trying to develop a new task or set of tasks ad’hoc.

For many of us, time isn’t a resource that is available in abundant supply. We generally have to make do with what we have or chip at the time we normally set aside for R&R, or even worse, for sleeping. Adding tasks to our todo list(s) simply increases the pressure we exert upon ourselves to hopefully get it all done in time.

Unless you are an experienced Ansible administrator, chances are that developing tasks on the fly may not be an option, especially when time is of the essence.

Imagine for instance that you need to configure the boot-device parameter or to upgrade the firmware on 50x units; that no longer have a password. In such situations you unfortunately can’t rely on Ansible to connect via SSH so you have to find another means to reach the devices e.g. Telnet.

Having now prepared close to three thousand units during my volume preparation sessions I have been able to evaluate my processes and to assess where potential improvements would make life easier.

To tackle these improvements, I began toying around, as a total newbie, with Python in the hope of developing specific scripts for specific tasks. ChatGPT and Gemini were very helpful on getting started. These platforms can generate code for you, which is clearly a huge time saver.

However, don’t go thinking that AI contributions will suffice. The code needs to be tested and adjusted. As previously mentioned, my initial attempts were focused on using Telnet to connect to the devices. The down side here is that, compared to the lightning fast SSH, a Telnet session can be rather sluggish at executing any assigned task.

Mostly through ‘stubborn’ trial and error I realized that the suggested Telnet-driven code needed a one second delay following the execution of a RouterOS command, else it would fail. Furthermore, the AI generated code also ignored the fact that the Python TelnetLib module won’t execute a RouterOS command without a carriage-return / line-feed combination suffix : ‘\r\n’.

So far, I have created three Python scripts: a reboot script, a firmware/flashboot activation script and a version control script. The scripts (see attachments) are provided for both Telnet and SSH. Use them as you please and adapt them as best fits your needs. They obviously don’t pretend to be the alpha and omega but maybe they can point you in the right direction.

The version control script basically collects a device’s IP and ether1 MAC addresses, its RouterOS and firmware versions and its serial number.
The ether1 MAC address and serial number are required by customers who prefer to have this information printed on labels using a larger (more legible) font and/or in a different format e.g. as a scannable bar/QR code. The ROS and firmware versions allow me to double-check whether the upgrade processes were completed successfully; A.K.A. quality control.

Whether you are into volume preparations or simply in search of other means to manage your Routerboards, Python is definitely a good GPL to have in your toolbox. I have to admit that I was a little surprised to see how few references existed on the Net mentioning both Python and RouterOS together.

Take care and stay safe
yann
Telnet_scripts.zip (2 KB)
SSH_scripts.zip (1.98 KB)

Please delete the double post.
I am writing this on the first post.

And talking about python, have you seen this repo.
https://github.com/dvtirol/pynetinstall