HOW TO: Using API Add a script and run script?

Hi Guys,

I have been working with the API a bit, I am wondering how I would add a script and run that script?

I understand how to run the script, but not how to add a script?

how do I convert this to API code???
/system script add name=“test” source=“:log info("hello")”

via API to run script:
/system/script/run
=.id=test

as any other CLI command out there

/system/script/add
=name=test1
=source=:log info hello



 1   name="test1" owner="admin" policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive run-count=0 source=:log info hello

and if you like something fancy in script:

/system/script/add
=name=test2
=source=:log info ("hello")



 2   name="test2" owner="admin" policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive run-count=0 source=:log info ("hello")

just remember there is string length limitation. That is, how long can be one word in sentence.

i have times when using API (and even regular CLI) that variables are not really global. For instance, user ‘admin’ logs in, declares a variable using :global. Then a script cannot access that same variable because it’s running as a different user (*sys?). Even declaring it to import it doesnt bring it in. Is it also possible API user isnt able to read global variables set by other users? Seems like sometimes they are isolated, and if you reboot and allow only one user (the system user, or admin) then it all works fine. Are global variables truly GLOBAL (api, scheduler jobs that runs scripts, and admin user interactively) ?

yes, that is so, that one user cannot access other users global variables.

can you control which user a script is run under? for example, scheduler has a job that runs on startup and it declares some global variables. How can a user login with CLI and/or API and still be able to modify those globals? and what is *sys?

You can access global variables by all users via this script:

# Display global variables for all users on RouterOS

:local users

/system script environment {
   :foreach v in=[find] do={
      :if ([:len [:find [:toarray $users] [get $v user]]] = 0) do={
         :set users ([:tostr $users] . [get $v user] . ",")
      }
   }

   :foreach u in=[:toarray $users] do={
      :put ($u . " variables:")
      :foreach v in=[find user=[:tostr $u]] do={
         :put ("   \"" . [get $v name] . "\"=\"" . [get $v value] . "\"")
      }
   }

}

just a note - you have to run this as user with policy permissions set. If we look at default groups, that means that users of group full can cross access other user variables. but even then, not in a direct way.

for example, setting up as admin

:global test1 aaa

and as user “tester”

:global test1 bbb

will result in

> sy script environment print 
 # USER                    NAME                                               VALUE
 0 admin                    test1                                              aaa 
 1 tester                    test1                                              bbb

Aaaah, this is helping me understand! I didn’t even know /system script environment existed, I have only known about ‘:environment print’ for a long time. . . This info should be added to the scripting wiki or manual : ) Thanks!

In RouterOS v4.6, here are the specific user permissions needed to view /system script environment

read,write,policy,test