Community discussions

MikroTik App
 
User avatar
TomjNorthIdaho
Forum Guru
Forum Guru
Topic Author
Posts: 1058
Joined: Mon Oct 04, 2010 11:25 pm
Location: North Idaho
Contact:

Bulk manage customer Mikrotiks using Linux

Thu Jun 25, 2020 8:30 pm

Bulk manage customer Mikrotiks using Linux

I posted this somewhere else on another Facebook group dedicated to Mikrotiks , and I thought it should also go here.

This is part of a set of scripts on my Ubuntu Linux server I use to bulk manage my entire list of thousands of customer Mikrotiks.
note - you will need expect on your linux compute.

Below is one of my Mikrotik scripts to bulk upgrade all of my customer Mikrotiks. I also have many other scripts that to different things.

---------------------------------------------------------------------------

Here is a set of scripts that will bulk update all customer Mikrotiks.
I assume the following with my scripts.
- you are using a Linux computer
- your linux computer can telnet to each customer mikrotik
- all bulk customer mikrotiks have the same login "admin" password
- your linux server can ping all of your bulk mikrotiks.
**
So here is the first file you need to create.
This is a list of your Mikrotik IP addresses.
You will need to edit this file to include your IP addresses.

Create a file named "Do-These-IPs.txt". This file needs to be readable.
add some or all of your bulk Mikrotik IP address into this file.
Here is an example ( you need to change these IPs for your bulk customer IP addresses.


- - - first file below this line - the bulk IP address list - file name: Do-These-IPs.txt - - -
#
# this is the contents of the file named: Do-These-IPs.txt
# start of my bulk customer Mikrotik IP address list
10.10.172.2
10.10.172.3
# 10.10.172.3 note - this customer turns off their Mikrotik at night
10.10.172.4
10.10.172.5
# 10.10.172.6 skip this one because there is a # at the beginning
192.168.33.1
192.168.33.2
192.168.33.3
192.168.33.253
192.168.33.254
192.168.33.255
# end of my bulk customer Mikrotik IP address list
#
- - - first file above this line - the bulk IP address list - file name: Do-These-IPs.txt - -


The next file process the IP addresses in your file: Do-These-IPs.txt
Create a file named "UpgradeROS-Part1.sh" then make it executeable
example to make it executable: chmod 777 UpgradeROS-Part1.sh


- - - second file below this line - process the IP address list - file name: UpgradeROS-Part1.sh - - -
#
for i in `cat Do-These-IPs.txt | grep -v "#"`; do
echo
#
#
ping -q -c 1 $i > /dev/null
if [ $? -eq 0 ]
then
echo "$i Ping test On-Line"
./Called-by-UpgradeROS-Part1.sh $i &

sleep 7
else
echo "$i - WARNING - does not answer a ping test - Off-Line - WARNING"
fi
sleep 3
done
- - - second file above this line - process the IP address list - file name: UpgradeROS-Part1.sh - - -


Below , the next shell program actually does the work.
This third file also needs to be executable.
This next program will do the following:
telnet into a customer mikrotik and uptrade the ROS to the newest/latest Mikrotik ROS.
If/when a Mikrotik gets upgraded, that Mikrotik will do a reboot.
Note - below , you need to change --> YourCustomerPassword <-- to the password you use to manage your customer Mikrotiks.


- - - third file below this line - connect and upgrade customer mikrotik - file name: Called-by-UpgradeROS-Part1.sh - - -
#
host=$1
#
#
/usr/bin/expect <<EOF | tee TEMP-FILE-Results.txt > logfile.txt

spawn telnet $host
expect "Login"
send "admin\r"
expect "Password"
#
send "YourCustomerPassword\r"
sleep 9
send "..\r"
sleep 1
send "..\r"
sleep 1
send "system\r"
sleep 1
send "..\r"
sleep 1
send "..\r"
sleep 1
send "..\r"
sleep 1
send "system\r"
sleep 1
send "system\r"
sleep 1
send "system\r"
sleep 1
send "package\r"
sleep 1
send "update\r"
sleep 1
send "check-for-updates\r"
sleep 40
send "install\r"
sleep 400
expect eof
- - - third file above this line - connect and upgrade customer mikrotik - file name: Called-by-UpgradeROS-Part1.sh - - -


OK now , when you want to start the upgrade process to all of your bulk customer mikrotiks type in this below:
./UpgradeROS-Part1.sh
The program will begin running and processing your customer Mikrotik IP address list.
It is safe to re-run this as many times as you wish.
Note - only mikrotiks that get upgraded will reboot. If a mikrotik is already the current version , then it will not reboot.
this fourth file needs to be executable

###########################################
now below , I have the script tools to perform the sy ro up part of the upgrade.
note - after you upgrade a Mikrotik to a newer version of ROS , you also have to upgrade the BIOS on the Mikrotik.
This fourth script will upgrade the Mikrotik BIOS and reboot the Mikrotik


- - - fourth file below this line - process the ip address list for BIOS upgrade - file name: UpgradeROS-Part2.sh - - -
#
for i in `cat Do-These-IPs.txt | grep -v "#"`; do
echo
#
ping -q -c 1 $i > /dev/null
if [ $? -eq 0 ]
then
echo "$i Ping test On-Line"
sleep 3
./Called-by-UpgradeROS-Part2.sh $i &


else
echo "$i - WARNING - does not answer a ping test - Off-Line - WARNING"
fi
sleep 3
done
- - - fourth file above this line - process the ip address list for BIOS upgrade - file name: UpgradeROS-Part2.sh - - -


now the fifth file is the script that actually upgrades the remote Mikrotik BIOS
file name is: Called-by-UpgradeROS-Part2.sh
this file also needs to be executable
Note - below , you need to change --> YourCustomerPassword <-- to the password you use to manage your customer Mikrotiks.


- - - fifth file below this line - connect and upgrade customer mikrotik BIOS then reboot - file name: Called-by-UpgradeROS-Part2.sh - -
#
host=$1
#
#
/usr/bin/expect <<EOF | tee TEMP-FILE-Results.txt > logfile.txt

spawn telnet $host
expect "Login"
send "admin\r"
expect "Password"
#
send "YourCustomerPassword\r"
sleep 9
send "..\r"
sleep 1
send "system\r"
sleep 1
send "system\r"
sleep 1
send "system\r"
sleep 1
send "system\r"
sleep 1
send "system\r"
sleep 1
send "system\r"
sleep 1
send "routerboard\r"
sleep 1
send "upgrade\r"
sleep 3
send "y\r"
sleep 1
send "..\r"
sleep 1
send "reboot\r"
sleep 1
send "y\r"
sleep 2
expect eof
- - - fifth file above this line - connect and upgrade customer mikrotik BIOS then reboot - file name: Called-by-UpgradeROS-Part2.sh - -


To bulk upgrade the Mikrotik BIOS for all of the Mikrotiks in your IP address list , use the following command:
./UpgradeROS-Part2.sh

################################
Note - If you have a slow network , you may need to increase all of the sleep periods in all scripts.
I generally prefer to run a little slow so as to not load down any netwoks and to ensure there are no errors.

I have many othere scripts that do other things - but this set of scripts should help you with one major pain-in-the-ass I used to have with bulk management.

North Idaho Tom Jones - ISP & WISP - Network Engineer - I have thousands of Mikrotiks in my networks.

Who is online

Users browsing this forum: YaCy [Bot] and 22 guests