Identity by [Part] IP-Address

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?

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])

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"
}

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"

@rextended : Thanks for the Scripting Tipps & Tricks

Your Script is way better than mine! =)

Not so much :stuck_out_tongue: , 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. :smiley:

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!
:slight_smile:

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"

Nice …
As I said before: I’m staring to like the RouterOS Language …