Advanced Stripting in DHCP

hi,

Can anybody help me out for advance scripting? the problem that im facing is through dhcp server i am getting the same ip each time. i have dhcp pool in my internal network of real ip and i want the dhcp server to allocate ip each time a different one.

Can anybody help me out.

Regards
neive

reduce lease time.

tried by reducing lease time but still getting the same ip address

here is a small script i used in 2.9 to do just that, increment a dhcp address for a specific client.

:set i [ /ip dhcp-server lease get [find comment=“SonicWall”] address ]

:if ([:toip $i] >10.20.3.250) do={:set i 10.20.3.110} else={:set i ($i+1)}

/ip dhcp-server lease set [find comment=“SonicWall”] address=$i

i tried with that script also but that script seems not working.

regards
Neive

change the “SonciWall” comment to match that of the one you want to target - case sensative i believe… and is this on 2.9 or 3.x ? This was only testing on 2.9 branch.

hi,

i tried with the same but its saying invalid item number. Please find the details of the same as mentioned below.

in in/dhcpserver/lease these are the following dhcp pc connected

ipaddress xxx.xxx.xxx.xxx
mac xx:xx:xx:xx:xx:01
clientid 1:0:1b:b9:53:2:48
hostname bdc
expires after 06:04:30
server dhcp1
status bound

So, here which comment i sould add. i tried by adding find comment=“bdc” but its not working.

Regards
Neive

make sure comment is there… i dont see it in your example.

2.9 or 3.x ?

its 2.9

hi,

there is no comment tag as i mentioned above ..

regards
Neive

That’s Sam’s point, you NEED to have the comment so the script can reference it.

Also post your exact RouterOS version.

Version is 2.9.6

If I have my DHCP knowledge correct, when the workstation requests a DHCP lease it includes the last IP address, subnet mask, gateway and other options it had. If the workstation is on a subnet that matches all those properties then the DHCP server will acknowkedge the request and the workstation uses the same address and options. If the workstation has been turned off and you turn it on, it’s up to the DHCP server to give out the address. If the DHCP server still has a copy of the lease the workstation had then it will likely issue the same address. Setting the lease time shorter might work so some other workstation can get the address after the lease times out but if the next available address happenes to be the same one, you will get the same one again.

HI,

I tried to check the same but there is no any option named “comment” kindly help with the same. I have one doubt that isnt it be configured on other variables like hostname or with the existing dchp client ip address.

Regards
Neive

When you either create a lease manually or if there is an existing lease created, you need to hit the ‘comment’ button and type something there that describes that lease. I think this is MAC based, not IP based. So that comment will stay with the MAC address. Someone else correct me if I’m wrong please.

But I would also upgrade to 2.9.51, it’s very stable.

Hey Ya!

got with the solution.. first you have to make that dhcp client showing in the lease to a static.. then right click and add a comment to that lease. then have to try with the script..

Fine let me try with it ill post it after i check the same.

Neive

hey i have tried its still not working.

/ip dhcp-server export

and post the results.

/ip dhcp-server
add address-pool=dhcp_pool1 authoritative=after-2sec-delay bootp-support=static disabled=no interface=ether1
lease-time=3d name=dhcp1
/ip dhcp-server config
set store-leases-disk=5m
/ip dhcp-server lease
add address=10.200.200.255 client-id=1:0:17:8:36:96:32 comment=test disabled=no mac-address=00:17:08:36:96:32 server=
dhcp1
/ip dhcp-server network
add address=10.200.200.0/24 comment=“” dns-server=0.0.0.0 gateway=10.200.200.1

:global a ;
:set a [/ip dhcp-server lease get [find comment=“an”] address ]
:if ([:toip $a] >10.200.200.254) do={:set a 10.200.200.100} else={:set a ($a+1)}
/ip dhcp-server lease set [find comment=“an”] address=$a


heya this script seems to be working but when i renew and release the ip address in pc after that i need to re enter the below command..

:if ([:toip $a] >10.200.200.254) do={:set a 10.200.200.100} else={:set a ($a+1)}
/ip dhcp-server lease set [find comment=“an”] address=$a

Then n then the ip get + 1

so, now can somebody help to make the script on event when someone login to hotspot user ? for example hotspot user “an”

Regards
Neive