I realize I need to add a script to “/system script” and then add the name of that script to the “on-login” attribute on the user profile. When I attempt to add the script to the “/system script”, I’m getting a syntax error.
This script is what I’m trying to do:
/system script
add \
name=foobar_script \
source={ :if ([:find $user "foobar"] == 0) do={ :log debug "The user '$user' contains foobar!";
} else={ :log debug "The user '$user' does not contain foobar!"; } }
The error occurs on the “$user” variable. I believe this is because when I’m defining this script the “$user” variable does NOT exist! However, the documentation clearly states that when the script is run the $user variable will be available.
Please let me know the proper way to reference the $user variable when defining scripts for “on-login”!
There a few things to fix… Here is code you can import that will work:
/system script
add name=foobar_script
source=“:if ([:find $user "foobar"] = 0) do={\r
\n\t:log info "The user ‘$user’ contains foobar!";\r
\n} else={\r
\n\t:log info "The user ‘$user’ does not contain foobar!";\r
\n}\r
\n"1. As you can see, the source must be in double quotes: source=”…", and special characters (nested quotes and $) are backslashed, and I also added line breaks.
2. In RouterOS scripting, when you test if something equals something, only a single “=” sign.
3. I couldn’t get “debug” logging to work (maybe I’m doing it incorrectly), but if you change to “info” logging, the logs show up.
4. You can put the code directly into the Hotspot on-login field. You don’t have to create a separate script…
The code above is the “exported” code… whereas I generally use winbox to do scripting, as the code is easier to read and update. The following code can be placed directly into the on-login box in winbox:
:if ([:find $user “foobar”] = 0) do={
:log info “The user ‘$user’ contains foobar!”;
} else={
:log info “The user ‘$user’ does not contain foobar!”;
}
@skot, thanks so much for your reply! Admittedly, there were a bunch of things wrong with my first attempt there!
I have my script working now!
The fact that I needed to use a multiline string instead of defining the script between braces “{}” (and just how to create multiline strings) was the most confusing part for me.
As an additional note:
I actually had to add the script (via a multiline string) directly to the “on-login” attribute of the profile directly. When I created a new “/system script” with the exact same multiline string, the newly created script showed a syntax error at the “$user” variable. Its like the “on-login” attribute knows that it will have “$user” variable available in its scope, but the “/system script” doesn’t, so it errors.