Trigger Script when Client registers on Network

Hi,

I am looking for a way to trigger a script when someone registers its client to the network (maybe identified by a dhcp request from specific MAC addresses?). Specifically I would like to call an URL (REST API) to turn on the light (Philips Hue) when his/her device successfully connects to the network.

My search for an example was not successful but maybe I was searching for the wrong terms.

Could anyone guide me to an example or provide a link where I can find a similar approach?

Thanks,
Martin

There must be someone in this community who can help me with this or is my goal inconvertible (which would be a helpful information as well)?

You could try it with a DHCP lease script.
Within this script you have access to a variable named $leaseActMAC so you can easily check if it’s the MAC address you’re looking for.

You can call an URL with the /tool fetch command.

-Chris

Shoe, did you ever get this to work?

@Chris: Thanks for your input, it is very appreciated! I think this could work.

@Mallabik: Unfortunately not. My coding skills are not that good so I was searching for an example of a DHCP Lease script but couldn’t find one that is similar to my approach.

Two things I am not sure how to achieve:

  • Triggering the script on a new DHCP lease (or do I need a constant loop to check?)
  • How to distinguish a “new” lease from an existing (the expiry time (currently 3 days) of the lease does not reset if a client reconnects to the network after a one or two day absence. Is there a “last seen” variable for MAC addresses?)

Read about ‘lease-script’ here http://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server and http://wiki.mikrotik.com/wiki/Manual:Scripting

I'm working on something similar, basically to get notified when a certain host connects.
The "lease script" which is called when a dhcp-server lease is given, seems like the way to go about this.
Though RouterOS 5.x does not have the "lease script" feature, but 6.x does.

So in order to do that in 5.x you could use a scheduled script, run every 10 secs,
but this is not very efficient and would probably lead to performance(?) issues.

Another way to trigger a script when a host connects to your network, is by using hotspot.
You can trigger a script when a user logs in, which can happen automatically if you let users log in with mac address.
This means that the user/device has to log in with a username and password at least once, which at that point his mac address is saved and remembered when he returns.

I'm a novice at this, so please correct me if I'm wrong.
EDIT:
a tested & working dhcp lease script below
it checks the hotspot ip-bindings for a matching mac address
change your server URL and comment out any log info lines you don't want
not sure how and when is triggered though - I'm looking more into this
- help anyone?

this runs on routerOS 6.x(?) which supports triggering a script (5.x does not)

when a lease is given from the DHCP server

Internal "global" variables that can be used in the script:

leaseBound - set to "1" if bound, otherwise set to "0"

leaseServerName - dhcp server name

leaseActMAC - active mac address

leaseActIP - active IP address

the lease script is run two times, the first leaseBound is 0, the second 1

script ENDS here if lease bound is 0

:if ([:tonum $leaseBound] < 1) do={
:log info "lease mac: $leaseActMAC not bound, script exits";
return
}

your url here, the GET vars will be added later

:local serverUrl "http://www.yourserver.com/api";

for debugging, set our MAC to check,

place the script in the repository and run it manually

uncomment the two lines below, and comment out the line #26

#:local ourMAC "CC:FA:00:B4:1D:8B";
#:local ipBindingID [/ip hotspot ip-binding find mac-address=$ourMAC];

first check if the MAC exists in hotspot bindings

get the ID of that binding (NULL if it does not exist)

:local ipBindingID [/ip hotspot ip-binding find mac-address=$leaseActMAC];

for debugging, comment out the lines below

:log info "ip binding id: $ipBindingID";
:log info "new lease: MAC: $leaseActMAC";
:log info "Server Name: $leaseServerName";
:log info "Lease Bound: $leaseBound";
:log info "Lease IP $leaseActIP";

:if ([:len $ipBindingID] > 0) do={

# it does exist, the comment for this ip-binding can be the user's name
:local macComment [/ip hotspot ip-binding get $ipBindingID comment];

# log the match
:log info "lease exists in ip-bindings with comment: $macComment";

# the GET vars that will be added in our URL
:local getVars "?lease_act_mac=$leaseActMAC";
:set getVars ($getVars."&lease_server_name=$leaseServerName");
:set getVars ($getVars."&lease_bound=$leaseBound");
:set getVars ($getVars."&lease_act_ip=$leaseActIP");
:set getVars ($getVars."&mac_comment=$macComment");

# make the call to our webserver 
# store the server's response(if any) in a file for reference 
/tool fetch mode=http url=($serverUrl.$getVars) dst-path=http_response.txt;

# display the contents of the file with the server's response
:local httpResponse [/file get http_response.txt contents];
:log info "HTTP server response: $httpResponse";

} else={

:log info "MAC does not exist in ip-bindings";

}

There is a similar -but with different functionality- idea in another thread:
http://forum.mikrotik.com/t/count-users-with-api/81240/1

Probably not what we want because the dchp lease script is not triggered every time a client is connected,
for example if the MAC address of the client exists at the leases table and the lease is not expired.

So the only way I see this working is by hotspot user login/logout script

Thanks a lot macns! Your script looks exactly what I was looking for, you just made my day!

I think it works pretty good with my needs because I don’t need to trigger the script every time the user connects. It’s sufficient once per visit and with the lease time i can define how much time needs to pass until we count it as a new visit.

Hopefully I can try it on the weekend and give you some feedback.

You’re most welcome.
Re-reading your post, this might not be what you want, but it can be modified easily.

Just tried it today and it works like a charm. I had to modify the script very slightly because I want to fetch the URL even if the MAC address is not bound (I do the checks and API calls to control the light with a PHP script). Since today we do not need to tap the switch anymore the room is already well-lit when we arrive…brilliant!

Thanks again macns and all others that helped me to achieve this.

Ok then, great!
Just a note, if no checks are needed, you only need one line as boen_robot suggests in http://forum.mikrotik.com/t/count-users-with-api/81240/1

/tool fetch url=("http://youserver.com/api\?mac=" . $leaseActMAC)