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