<help>Block trial user Mikrotik Hot-spot by. hostname

I nead help. I found this script http://forum.mikrotik.com/viewtopic.php?f=2&t=18815&p=89265&hilit=block+hostname+dhcp#p89265
How to make it work on ROS ver. 3.x?
Thanks!

add the bad names here in a list

:local hosts “pcname1”,“pcname2”

:foreach host in $hosts do={
:foreach i in= [/ip dhcp-server lease find host-name $host] do={
:local ipnum [/ip dhcp-server lease get $i address]
:local unum [/ip hotspot active find address $ipnum]
:local usr [/ip hotspot active get $unum user]
:log info ($host . " " . $ipnum . " " . $usr)
#next line uncommend it, if you want to kick them out right now
#/ip hotspot active remove $unum
#other stuff can do now with the identified IP and USER
}
}

The biggest reason why scripts fail on v3.x is because of the way the variables are declared and used. You can no longer directly give a local variable a value and declare it as follows:

:local ipnum [/ip dhcp-server lease get $i address]

instead it should be changed to:

:local ipnum
:set ipnum [/ip dhcp-server lease get $i address]

If you edit the code with this new syntax, it may work better?

:local hosts “461e38a4ab55xxx”
:foreach host in $hosts do={
:foreach i in= [/ip dhcp-server lease find host-name $host] do={
:local ipnum
:set ipnum [/ip dhcp-server lease get $i address]
:local unum
:set unum [/ip hotspot active find address $ipnum]
:local usr
:set usr [/ip hotspot active get $unum user]
:log info ($host . " " . $ipnum . " " . $usr)
/ip hotspot active remove $unum
}
}

Is this OK? :confused: stil nothing :frowning: Active user in hot spot with host name 461e38a4ab55xxx (detected by DHCP) is still in active hot spot list, nothing, no errors in log


Flags: X - disabled, I - invalid

TOPICS ACTION PREFIX

0 info memory
1 error memory
2 warning memory
3 critical echo
4 dhcp memory
5 script memory
6 ups memory

If the script now runs, then you have successfully converted the script to run on v3.x. If it still does not appear to do what you expected it to do, then there is a problem with the script itself, which is a whole new problem as it will need you to examine it line by line, learn for yourself what the original author wanted it to do and then fix that problem. Or get someone else to do it! :slight_smile:

If it does not run at all, then there are still some version sensitive errors.

Does the run counter increase each time you run it?

One suggestion I have, place all the local variable declarations at the top. I.e.

:local hosts "461e38a4ab55xxx"
:local ipnum
:local unum
:local usr
:foreach host in $hosts do={
:foreach i in= [/ip dhcp-server lease find host-name $host] do={
:set ipnum [/ip dhcp-server lease get $i address]
:set unum [/ip hotspot active find address $ipnum]
:set usr [/ip hotspot active get $unum user]
:log info ($host . " " . $ipnum . " " . $usr)
/ip hotspot active remove $unum
}
}

:frowning: still nothing.
This script must remove active user from hot-spot loged by. trial if we detecte that user clone his MAC address to get extra time on free hot-spot. If he clone MAC address DHCP server will detect his computer name on number off diferent leases.. Now we have his computer name and this script must do the rest…

Does the run counter increase each time you run it?

yes!

Is ‘461e38a4ab55xxx’ in the dhcp lease table before you run this and also still there after?

I can see that you have not declared the array correctly nor set it’s value correctly. I will play with this script on a spare RB and make it work. It is not difficult.

Try this (edit your list of computers to be correct for your setup)

:local hosts [:toarray "Computer1,Computer2,Computer3"]
:local host
:local ipnum
:local unum
:local usr
:local i

:foreach host in=$hosts do={
:foreach i in= [/ip dhcp-server lease find host-name=$host] do={
:set ipnum [/ip dhcp-server lease get $i address]
:set unum [/ip hotspot active find address=$ipnum]
:set usr [/ip hotspot active get $unum user]
:log info ($host . " " . $ipnum . " " . $usr)
/ip hotspot active remove $unum
}
}

First… Thanks!!

Is ‘461e38a4ab55xxx’ in the dhcp lease table before you run this and also still there after?

Yes it is still there…
Grr. there is no active user right now to test script…

It is not difficult.

uff… but it is for me :confused:

If you use this on an open public wifi hotspot, and set this as a scheduled script, be careful as some users have never changed the name of their device, e.g. ‘iPhone’, ‘iPod-touch’ and if their DHCP request does not send a hostname at all, it will be blank in the lease table. So there will always be some users who can still keep changing their MAC address and thus bypass the free trial system. If you only have fixed clients with unique names, then maybe this is not that big a problem and it should work.

If this works, I’ll add it to the scripts on the wiki with a lot more comments in the coding and credits to roland who appears to have originally created it. Also, I’ll work on the other script that is associated with it. But I’ll wait to hear from the results of your tests, when/if your bad guy comes back.

It’s WORK!

Thank you wery mush!

If you use this on an open public wifi hotspot, and set this as a scheduled script, be careful as some users have never changed the name of their device, e.g. ‘iPhone’, ‘iPod-touch’ and if their DHCP request does not send a hostname at all, it will be blank in the lease table. So there will always be some users who can still keep changing their MAC address and thus bypass the free trial system. If you only have fixed clients with unique names, then maybe this is not that big a problem and it should work.

That’s right, but I have 10-20 free users sou… it is not a problem.

Wiki is good idea.. when I search for this script there is nothing on google about this. Thanks for big help!

You’re very welcome.

I’ll convert the other script that roland published now, which looks for bad users who are changing their MAC address often and auto deletes them from the active list as soon as they appear. :slight_smile:

I hardly waiting!

OK - I don’t have anyone changing their MAC address to get free access on my hotspots, so cannot really test this. But it seems to be working, as best as I can tell. I have basically taken rolands original scripts and made them work on v3.x. I have not tried very hard to make the code efficient. So, if it doesn’t work, I’ll try my best to fix it, but no guarantees as I didn’t originally write this!

Set up a schedule to run this script every few minutes

:local hosts [/ip dhcp-server lease find]
:local pcname ""
:local pcnum 0
:global hacklist ""
# To log the value of $hacklist each hour, make debug 1 (if $hacklist is blank, nothing will be logged)
:local debug 1

:foreach h1 in=$hosts do={
:local host [/ip dhcp-server lease get $h1 host-name] 
:if ([:len $host] >0) do {
:set pcname ($pcname . "," . $host)
:set pcnum ($pcnum + 1)
}
}

:local pcnameArr [:toarray $pcname];

:foreach h2 in=$pcnameArr do={
:local hh 0
:if (!([:find $hacklist $h2]>=0)) do={
:foreach k in=$pcnameArr do={ :if ($k=$h2) do={:set hh ($hh + 1) } }
:if ($hh>2) do={ 
:if ([:len $hacklist] >0) do {:set hacklist ($hacklist . "," . $h2)} else={:set hacklist $h2}
}
}
}

# monitor results in logfile once an hour 
:local timer [:pick [/system clock get time] 3 5]
:if (($debug > 0) || ($timer >= "58")) do={ 
:if ([:len $hacklist] >0) do={
:log warning ("New Hacklist: " . $hacklist)
}
}

Set up a schedule to run this every 20 seconds. (Which is the period of time a hacker will be able to remain active before they get kicked off, therefore this value can be increased if you want to reduce CPU utilisation or you are not worried that the hacker stays connected for longer. Your decision! Do not reduce time below 15 seconds, otherwise script could still be running when it gets called again by scheduler.)

:local host
:local ipnum
:local unum
:local usr
:local i
:global hacklist

:foreach host in=$hacklist do={
:foreach i in= [/ip dhcp-server lease find host-name=$host] do={
:set ipnum [/ip dhcp-server lease get $i address]
:set unum [/ip hotspot active find address=$ipnum]
:if ([:len $unum] >0) do {
:set usr [/ip hotspot active get $unum user]
:log warning ($host . " " . $ipnum . " " . $usr)
/ip hotspot active remove $unum
}
}
}

great! i will test it.. all my hackers are gone.. Any idea how to make first script remove hackers lease in dhcp-server??

In winbox, just highlight the entries and delete them. Or let them expire naturally when the lease period expires. If there is an entry for them in the lease table, this is not a problem.

:local hosts [:toarray “Computer1,Computer2,Computer3”]

Something is wrong… only work for Computer1

I think that is a problem. When there is two leases for same user, there is two diferent ip address too. whic ip will script useto detect client?

I have no idea, as I didn’t write the script. I only converted it to v3.x. I suggest you contact the original author?

If I find the time, I will try to see where the problem is. But no promises, as I didn’t write the original and also don’t have a live system to test this on.

Hello, maybe an old post, but here is the solution.

You can delete the leases you dont need. (for cheating users) by adding a code to the script after you have removed user from hotspot active list:

/ip dhcp-server lease remove [/ip dhcp-server lease find host-name=$host]

That way lease will be deleted too so there will be no conflict which ip address is the cheating user.


The whole script look like this :

:local hosts [:toarray "comp1,comp2"]
:local host;
:local ipnum;
:local unum;
:local usr;
:local i;

:foreach host in=$hosts do={
   :foreach i in= [/ip dhcp-server lease find host-name=$host] do={
      :set ipnum [/ip dhcp-server lease get $i address];
      :set unum [/ip hotspot active find address=$ipnum];
      :set usr [/ip hotspot active get $unum user];
      :log warning ($host . " " . $ipnum . " " . $usr);
      /ip hotspot active remove $unum
      /ip dhcp-server lease remove [/ip dhcp-server lease find host-name=$host]
   }
}

Pipi