Community discussions

MikroTik App
 
sysadmbonn
just joined
Topic Author
Posts: 6
Joined: Tue Aug 24, 2021 5:55 pm

Identity by [Part] IP-Address

Fri Sep 10, 2021 6:48 pm

Hi there!

I would like to script my new "hEX s" Buddies (65 so far ... more to come) with some settings to make life easier ...
I alreday have the settings I need for SNMP, Firewall, Upgrade and so on.
Runs pretty need and I do find the logic of the SSH commands usefull.
But! I would like to change the last thing I have to manually adjust on each Router: the Identity

Each Router gets it's WAN IP from my Firewall via DHCP Reservation = 192.168.160.50 ... 192.168.160.51 ... 192.168.160.52 ... you get the picture ...
Now I would like to name (Hostname / Identity) each Router with "MT-%%%" - where as "%%%" ist the last part of the IP-Address ...
Examples:
Router with the WAN IP Address: 192.168.160.50 gets the Identity "MT-50"
Router with the WAN IP Address: 192.168.160.51 gets the Identity "MT-51"
Router with the WAN IP Address: 192.168.160.52 gets the Identity "MT-52"

Catch my drift?
Now, If some of you Script-Masters would be so kind and show me the correct way of doing this?

I found an Script (Snippet) for the MAC-Addres, but for the Love of God I've been trying for two hours now to change it for the IP-Address ...

[admin@MT-XXX] > :global IDENTITY [interface ethernet get ether1 mac-address ]
[admin@MT-XXX] > :env print
IDENTITY="2C:C8:1B:42:17:77"


Any Idea?
 
ConnyMercier
Forum Veteran
Forum Veteran
Posts: 723
Joined: Tue Dec 17, 2019 1:08 pm

Re: Identity by [Part] IP-Address

Sat Sep 11, 2021 1:00 am

I kept it short and simple...
I hope it helps !


#-----------------------------------------
:local InterfaceID "bridge1"
:local prefix "MT-"
:local suffix ""
#-----------------------------------------

# Generate infix
:local infix
:if ([/ip dhcp-client get $InterfaceID address] != nil) do={
:set infix [/ip dhcp-client get $InterfaceID address]
:set infix [:pick $infix 0 [:find $infix "/"]]
:set infix ($infix<<24)
:set infix [:pick $infix 0 [:find $infix "."]]
}

# Set identity
/system identity set name=([$prefix].[$infix].[$suffix])
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Identity by [Part] IP-Address

Sat Sep 11, 2021 2:20 am

You can put on "dhcp-client / advanced / script" this, it also update the identity everytime you changed the assigned IP to that device:
:if ($bound = 1) do={
    :local part ($"lease-address" << 24)
    :local part [:pick $part 0 [:find $part "." -1]]
    /system identity set name="MT-$part"
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Identity by [Part] IP-Address  [SOLVED]

Sat Sep 11, 2021 2:29 am

I kept it short and simple...
I hope it helps !
:local InterfaceID "bridge1"
:if ([/ip dhcp-client get $InterfaceID address] != nil) do={
}
The script can not work.


Fixed script:
#-----------------------------------------
:local interfaceName "ether1"
:local prefix "MT-"
:local infix ""
:local suffix ""
#-----------------------------------------

# Generate infix
/ip dhcp-client 
:local interfaceID [find where interface=$interfaceName]
:if ([get $interfaceID address] != nil) do={
    :set infix [get $interfaceID address]
    :set infix [:pick $infix 0 [:find $infix "/" -1]]
    :set infix ($infix << 24)
    :set infix [:pick $infix 0 [:find $infix "." -1]]
}

# Set identity
/system identity set name="$prefix$infix$suffix"
 
ConnyMercier
Forum Veteran
Forum Veteran
Posts: 723
Joined: Tue Dec 17, 2019 1:08 pm

Re: Identity by [Part] IP-Address

Sat Sep 11, 2021 3:07 am

@rextended : Thanks for the Scripting Tipps & Tricks

Your Script is way better than mine! =)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Identity by [Part] IP-Address

Sat Sep 11, 2021 3:29 am

Not so much :-P , you only forget to convert the interface from label to ID because get do not support the direct use of name

[find where interface=$interfaceName]

the rest is only preference on how I coding. :D
 
sysadmbonn
just joined
Topic Author
Posts: 6
Joined: Tue Aug 24, 2021 5:55 pm

Re: Identity by [Part] IP-Address

Sat Sep 11, 2021 10:27 pm

Hi there!
Thanx for your fast replys!

The "fixed script" did the Trick quite nice. Only it doesn't work on "startup" in a schedular. I guees it is too soon to getthe IP from DHCP at that moment.

Anyhoy, I let the script run every five Minutes wich is fine by me ...

Cheers Mates!
:)
 
ConnyMercier
Forum Veteran
Forum Veteran
Posts: 723
Joined: Tue Dec 17, 2019 1:08 pm

Re: Identity by [Part] IP-Address

Sat Sep 11, 2021 10:43 pm

You can copy the Code into a /system script
and trigger the Script in different ways ...

Solution 1: Via Scheduler (startup)
/system scheduler
add name=schedule1 on-event=":delay 20s\r\
    \n/system script run script1\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup
Solution 2: Via DHCP-Client Event
/ip dhcp-client
add disabled=no interface=ether1 script="/system script run script1"
 
sysadmbonn
just joined
Topic Author
Posts: 6
Joined: Tue Aug 24, 2021 5:55 pm

Re: Identity by [Part] IP-Address

Sun Sep 12, 2021 1:44 am

Nice ...
As I said before: I'm staring to like the RouterOS Language ...

Who is online

Users browsing this forum: No registered users and 16 guests