Community discussions

MikroTik App
 
nexgenaccess
just joined
Topic Author
Posts: 17
Joined: Mon Feb 21, 2005 8:57 pm

[CONTRIB] UPDATED Automated Batch Commands...

Sat May 19, 2007 1:56 pm

Following on a previous posters [iredden] idea, I created a script to automate batch commands from a linux host.

First, you have to setup the SSH for the server you will be running this from.
See http://wiki.mikrotik.com/wiki/Use_SSH_t ... y_login%29

Create listing of Mikrotik Routers...
admin@watchdog:/$ cat /opt/ros/routers.txt 
172.16.48.1
172.16.48.35
172.16.48.37
Create listing of commands...
admin@watchdog:/$ cat /opt/ros/ros-commands 
/system identity print
/system resource print
Create username/password file
admin@watchdog:/usr/sbin# cat /etc/monitor.passwd 
remote||password
And then launch the script below like so...

ros-remote /opt/ros/ros-commands
#!/usr/bin/perl
# By Matthew D. Smith (c) 2007 with help of the orignal script by
# by Ian Redden
# Purpose: To get info or execute commands on a Mikrotik RouterOS.
#
# Prerequisites: Expect, IO::Tty, IO:Stty
#
# Script will log into each router one a time (from ROUTER_LIST) and issue the commands from CMD_LIST one at a time.
#
# Usage: ros-remote [filename]          ie; #ros-remote /opt/ros/ros-commands.txt
#
#
#######################CONFIGURATION########################################

# Text Listing of Routers - Insert Your Filename Here
my $ROUTER_LIST = '/opt/ros/routers.txt';

######################END CONFIGURATION#####################################
#############DO NOT ALTER BELOW THIS LINE###################################

# Open File
open DATA, "$ROUTER_LIST" or die "can't open $ROUTER_LIST $!";

# Assign array to Router List
my @routers_array = <DATA>;

# Close File - Done.
close (DATA);

# Assign variable to command file passed to script
my $CMD_LIST = "$ARGV[0]";

# Loop through each router in Array
foreach my $line (@routers_array)
{

# Use Expect
  use Expect;

# Where is SSH?
  $ssh = "/usr/bin/ssh";

# Get Username/Password
  open(PS,"/etc/monitor.passwd");
   while (<PS>) {
     chomp;
     ($usernm,$pass) = split(/\|\|/, $_);
   }
  close(PS);

        # Command to launch SSH
        $command = "$ssh -l $usernm -i /root/.ssh/id_dsa $line";

        # Use Expect to connect to Router
          $ssh = Expect->spawn("$command");
        # Do not echo Router Banner
          $ssh->log_stdout(0);

# Send Password - enable if user account has password
#   if ($ssh->expect(undef, "password:")) {
#     print $ssh "$pass\r";
#   }

# Send Commands From File
   open( FILE, "< $CMD_LIST" ) or die "can't open $CMD_LIST $!";
   if ($ssh->expect(undef, ">")) {
           while ($cmd = <FILE>) {
              # Echo Commands
              $ssh->log_stdout(1);
              print $ssh "$cmd\r";
              $ssh->expect(undef, ">");
           }
  }
  # Close File
  close FILE;

  #Quit SSH Session
  print $ssh "/quit\r"; 

}

print "\r";
I plan on using this to distribute a common list of firewall rules to all of my Routers once I figure out how to either remove all existing firewall rules or how to avoid duplicate rules....

I'm planning on elaborating on this in the future such as polling MT's neighbor viewer to get router listing, some sort of php/mysql interface to maintain listing of commands/routers, etc. Any help would be appreciated!

I know SNMP Write is coming in 3.0, but hopefully this helps out anyone who planning on using 2.x for a while...

-Matt
 
jo2jo
Forum Veteran
Forum Veteran
Posts: 973
Joined: Fri May 26, 2006 1:25 am

Re: [CONTRIB] UPDATED Automated Batch Commands...

Wed Feb 27, 2019 7:57 am

thank you for this. im gong to try it out, and hope it still works (13 yrs later, and this is still the only solution i know of to mass config/update config of many MTs)
:beep :beep :beep
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1805
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: [CONTRIB] UPDATED Automated Batch Commands...

Wed Feb 27, 2019 8:15 am

In my first version of Splunk for MikroTik, lots of the information from the routers was collected using script from Linux.
This works fine as long as you only have a few routres, but with many routers it becomes more complicated and if you have a router that is behind an ISP where you can not open SSH, it will not work. So I dropped this form of data collecting and started to use script on the router to push data out on syslog udp/514. Then I only need on script that is equal for all routers, works behind ISP and no need for a linux server.

So I went from collecting data using:
1. Syslog
2. SNMP
3. Remote scripts

To:
1. Syslog for logs + local script sending SNMP and other data using Syslog

SNMP has the same problem as Remote Script. You need to configure one and one device to monitor, does not work behind NAT (that is out of your control)
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 

Who is online

Users browsing this forum: Baidu [Spider] and 11 guests