scripts not running unless from terminal

I have a script that works from terminal, or if I execute system script run test, but will not run by clicking run script or using scheduler. All this does is read a list of IP addresses, ssh into the RB and do a reboot. I am using ROS 6.17. Any ideas?

:local content [/file get [/file find name=Iptest.txt] contents] ;
:local contentLen [ :len $content ] ;
:local lineEnd 0;
:local line "";
:local lastEnd 0;

:do {
       :set lineEnd [:find $content "," $lastEnd ] ;
       :set line [:pick $content $lastEnd $lineEnd] ;
       :set lastEnd ( $lineEnd ) ;
	:local identity
       :local tmpArray [:toarray $line] ;
       :if ( [:pick $tmpArray 0] != "" ) do={
		/system ssh address=[$tmpArray] user=admin port=22 "/system reboot";
       }
} while ($lineEnd < $contentLen);

First of all: THE MAXIMUM SIZE OF FILE READABLE FROM SCRIPT ARE 4096Byte!

Second: fix all syntax errors (ignoring the logic):

:local content value=[/file get [/file find where name="Iptest.txt"] value-name=contents];
:local contentLen value=[:len $content];
:local lineEnd value=0;
:local line value="";
:local lastEnd value=0;

:do {
       :set $lineEnd value=[:find $content "," $lastEnd];
       :set $line value=[:pick $content $lastEnd $lineEnd];
       :set $lastEnd value=$lineEnd;
       :local identity value="";
       :local tmpArray value=[:toarray $line];
       :if ([:pick $tmpArray 0] != "") do={
              /system ssh address=$tmpArray port=22 user=admin command="/system reboot;\r\ny\r\n" output-to-file=tmp;
       }
} while=($lineEnd < $contentLen);

Third: on this moment I’m checking the logic of the script…

But I already see this script… is familiar…

It’s one of my scripts!!! :wink:

http://forum.mikrotik.com/t/create-an-address-list-from-a-text-file/77741/3



If you provide on the file one list of IP like

1.1.1.1,2.2.2.2,3.3.3.3,4.4.4.4, etc.

you can alter the script for be more simple:

:local arrayofip value=[:toarray [/file get [/file find where name="Iptest.txt"] value-name=contents]];
:foreach ip in=$arrayofip do={
 /system ssh address=$ip port=22 user=admin command="/system reboot;\r\ny\r\n" output-to-file=tmp;
 /delay delay-time=10s;
};

Use one reasonable time between SSH commands.

ok so I tried your script with a couple of modfications. For testing purposes I am using the following:

:global arrayofip [:toarray [/file get [/file find name="Iptest2.txt"] contents]];
:do {
:foreach ip in=$arrayofip do={:put $ip;} 
}

This works great with one exception. The output contains an addition line. My text file contains 2 ip addresses in this format, x.x.x.x,x.x.x.x I have also tried with x.x.x.x,x.x.x.x, which gives me a second extra line. This does not affect the functionality other than it gives an error because the last line is not an ip address. I have tried a couple of different solutions to stop after the last ip is read but am having no luck. using while (:pick $ip != “”) throws an infinite loop.. what is the best way to find the last entry?

Oh and yes Rex, I have borrowed parts of your code for a number of things. :wink:

The problem is the “Enter” at the end (\r\n or \n only for unix system)
The list must end with last number, not by “enter”

Right file format:
start =>1.1.1.1,2.2.2.2,3.3.3.3<= end

unexpected file format:
start =>1.1.1.1,2.2.2.2,3.3.3.3
<= end

On this way, you see one empty line because “return” is part of 3.3.3.3 string.

With this you can check if the last ip as “\r\n” (carriage return & new line) at the end.
:if ($ip~“\r\n”) do= { :put “!!!” };

Solution: simply do not press return at the end of IP list.