Community discussions

MikroTik App
 
iRZ0mbie
just joined
Topic Author
Posts: 4
Joined: Sat Nov 07, 2020 3:39 am

Static Ip hostname insert into comment

Tue Jan 19, 2021 2:58 am

### Static IP - Hostname - Comment / script will take host-name of all static IP's that dont have existing comments, and insert host-name into comment. will not overwrite comments.
:foreach leaseId in=[/ip dhcp-server lease find where !comment && !dynamic] do=[
 :local ip [/ip dhcp-server lease get $leaseId address];
 :local hostname [/ip dhcp-server lease get $leaseId host-name];
 :ip dhcp-server lease set $leaseId comment="$hostname" 
 :log info "set Static IP $ip comment to $hostname"]
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Static Ip hostname insert into comment

Tue Jan 19, 2021 11:01 am

Just some comment to your script.
No need for ; after each line. Only when there are multiple commands on same line.
Loops are normally in {}, not []. (I see that it works).
If you go to a location like /ip dhcp-server lease, you can skip the path for the rest of the line where its used.
/ip dhcp-server lease
:foreach leaseId in=[find where !comment && !dynamic] do={
	:local ip [get $leaseId address]
	:local hostname [get $leaseId host-name]
	set $leaseId comment="$hostname" 
	:log info "set Static IP $ip comment to $hostname"
}

Who is online

Users browsing this forum: Google [Bot], maxslug and 27 guests