Community discussions

MUM Europe 2020
 
pkelly1603
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 78
Joined: Mon Jun 15, 2009 11:13 pm

Can't create script of more than one line with API

Mon Aug 20, 2018 10:53 pm

Hello,

I'm trying to create an API app that logs into a router and creates a script, then runs that script. The problem is that I can only do a single line at at a time. When I use "\n" or "\r\n" inside the PHP API app text, the result created under /system/script is everything before the first backslash and nothing after it.

Here is my code:

Code: Select all

$API->write("/system/script/add
=name=test1
=source=:log info test \r\n test
");
Here is the result under /system/script print
test router] > system script print
Flags: I - invalid
0 name="test1" owner="admin" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon
run-count=0 source=:log info test


Any ideas? I have tried many variations and have read that the escaped characters in PHP only work with double quotes so I am aware of this issue but routerOS doesn't seem to handle them correctly anyway.

If I run this command in the router terminal, it works perferctly:
system script add name=test2 source=":log info test \r\n test"
 
pkelly1603
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 78
Joined: Mon Jun 15, 2009 11:13 pm

Re: Can't create script of more than one line with API

Mon Aug 20, 2018 11:13 pm

Update:

I solved this problem by removing newlines from script text and terminating each command with a semicolon (leaving all commands on one line). I still am not sure why newlines aren't working but at least that is a good workaround.
 
nescafe2002
Long time Member
Long time Member
Posts: 649
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Can't create script of more than one line with API

Mon Aug 20, 2018 11:51 pm

Have you tried an actual newline in your script?

E.g.
$API->write("/system/script/add
=name=test1
=source=:log info test 
 test
");

Seems to work fine:

ASCII:
explorer_2018-08-20_22-49-01.png

Hex:
Wireshark_2018-08-20_22-44-42.png

Notice the 0d0a in line 0000007A which represents the actual newline.

Result:
explorer_2018-08-20_22-48-09.png
You do not have the required permissions to view the files attached to this post.

Who is online

Users browsing this forum: MSN [Bot] and 16 guests