Community discussions

MikroTik App
 
alex_rhys-hurn
Member
Member
Topic Author
Posts: 348
Joined: Mon Jun 05, 2006 8:26 pm
Location: Kenya
Contact:

Script to change second octect of IP Address in Address List

Tue May 23, 2023 11:53 am

Hi everyone,

I am not very good at scripting, but am clear what I want. I hope somebody can help, or point me in a better direction.

I am using unimus to manage mikrotik devices. I have mikrotiks at 4 different sites:
Site1 10.1.x.y
Site2 10.2.x.y
Site3: 10.3.x.y
Site4 10.11.x.y

What I would like is for the script to somehow identify the site (router), and then add to the script an IP address with correct IP address. Is this possible?

Many thanks.

I am using this script (credit to unimus team) to create the address list:
:do {
  # set your list name and addresses desired in the list
  :local listName "rtr-admins"
  :local addresses {"10.x.y.z/32"="Change the x to match the site"}

  # do not modify past this point
  /ip firewall address-list
  :local existing [find list=$listName]
  
  :foreach r in=$existing do={
    :local shouldDelete true

    # this is inefficient, but ROS scripting has no way to break out of a loop :(
    :foreach a,c in=$addresses do={
      :if ([get $r address] = $a) do={
        set $r comment=$c
        :set shouldDelete false
      }
    }

    :if $shouldDelete do={
      remove $r
    }
  }

  :foreach a,c in=$addresses do={
    :if ([:len [find list=$listName address=$a]] = 0) do={
      add list=$listName address=$a comment=$c
    }
  }
} on-error={
  :put "Error - failed to apply address list configuration!"
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to change second octect of IP Address in Address List

Tue May 23, 2023 12:07 pm

I didn't understand what you want to achieve, or what that script does,
explain yourself better what you have at the beginning and what you want to achieve at the end,
not how you plan to achieve it.
 
alex_rhys-hurn
Member
Member
Topic Author
Posts: 348
Joined: Mon Jun 05, 2006 8:26 pm
Location: Kenya
Contact:

Re: Script to change second octect of IP Address in Address List

Tue May 23, 2023 1:19 pm

Hi, many thanks for reply, and apologies for lack of clarity.

That script was kindly written by the Unimus team, and is a mikrotik script that will either when run within Mikrotik or in this case when executed by unimus will create the address list rtr-admins and apply the ip addresses and comments given in line 4. The rest of the script will check if the address list entries already clean them up and then apply the new ones.

Because I am using unimus to deploy this script across multiple sites with different networks and this script is centraly hosted there are a couple of IP addresses that are specific to the site. So I would like to add to the script something that can identify which site its running on, and then apply a modification to the IP address in the address list, with the appropriate second octet.

No idea I am clarifying or confusing.... Anyway many thanks for taking a look.

Source is from here: https://forum.unimus.net/viewtopic.php?f=11&t=1356
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to change second octect of IP Address in Address List

Tue May 23, 2023 2:00 pm

On purpose it didn't make sense, you changed it at random, it's not the original script...

Now I understand partially what you need:
Import from a list of IP addresses/prefixes in a script,
in a list of addresses on the firewall,
but based on which router is running the list, add or not (or change) the IP addresses in the list.

There are dozens of scripts to import lists with, I won't create another one.
But importing or changing just some IPs based on which router the script runs on, is too complicated.
Simply make 5 lists with the same name, but different scripts: one in common, and 4 with IPs for that router only.
So what changes will be only the one in common, I assume.

However, it's still not clear what you start with and what you want to achieve in the end. A generic sketchy description is not enough, a practical example is needed.
 
alex_rhys-hurn
Member
Member
Topic Author
Posts: 348
Joined: Mon Jun 05, 2006 8:26 pm
Location: Kenya
Contact:

Re: Script to change second octect of IP Address in Address List

Tue May 23, 2023 3:37 pm

Hi,

Thanks for the feedback, and guidance to look for importing from lists and a concept to how to manage them.

Apart from changing:
:local addresses {"1.1.1.1"="some_comment"; "2.2.2.2"="a_different_comment"}
to
 :local addresses {"10.x.y.z/32"="Change the x to match the site"}
I havent made any changes to the script.

Certainly not a random change, and its inline with what I was trying to achieve, so not clear on your first line.

Many thanks for the feedback.

Alex
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to change second octect of IP Address in Address List

Tue May 23, 2023 3:55 pm

Ok, once again, instead of explaining what you want to achieve in the end, and giving practical examples, go comment once again on that script.

So you are more interested in getting that script to work than solving your problem.

I'm done here, that's all you care about.

Have a nice day.

Who is online

Users browsing this forum: No registered users and 15 guests