Community discussions

MikroTik App
 
Matas
just joined
Topic Author
Posts: 4
Joined: Fri Aug 12, 2022 2:33 pm

Issue with data stored in a file

Fri Aug 12, 2022 3:10 pm

Hello,
I have a specific issue.
i am downloading some data with /tool fetch command
Thecontent of that file is in correct Mikrotik array format, but when i load it into a variable, it is not recognised as an array, but as an ordinary string instead.
:toarray transforms this string into an array with a single element, which is the complete string.
file contents: {"A"="1";"B"="test";"C"="20";"D"="test2";}
:global data [/file get "file.txt" contents]
:put $data
{"A"="1";"B"="test";"C"="20";"D"="test2";}
:put [:typeof $data ]
str
:put [:toarray $data ]
"A"="1";"B"="test";"C"="20";"D"="test2";
:put ([:toarray $data ]->0)
"A"="1";"B"="test";"C"="20";"D"="test2";
:put ([:toarray $data ]->1)
  (empty)
:put ([:toarray $data ]->"A")
  (empty)
when i copy/paste this string into the terminal, the contents are correctly recognised as an array, and the elements can be called from it
:global data {"A"="1";"B"="test";"C"="20";"D"="test2";}
:put [:typeof $data]
array 
:put ($data->"A")
1
:put ($data->"B")
test
saving files like this also does not help
/file set test1 contents="{\"A\"=\"1\";\"B\"=\"test\";\"C\"=\"20\";\"D\"=\"test2\";}"
contents of the resulting file looks like this, and it has the same issue
{"A"="1";"B"="test";"C"="20";"D"="test2";}
:global data {"A"="1";"B"="test";"C"="20";"D"="test2";}
/file set test123 contents=$data
contents of the resulting file looks like this, but the issue is the same
A=1;B=test;C=20;D=test2

Only data directly pasted into the terminal is properly parsed, anything loaded from the file fails.

Essentially, i want some key->value stored, and read to be used in the script.

Any ideas?
Thanks
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Issue with data stored in a file

Fri Aug 12, 2022 3:20 pm

Any ideas?
Yes
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Issue with data stored in a file

Fri Aug 12, 2022 6:10 pm

You have see the page on the link on signatue and discovered something useful?

You need some hints?
 
Matas
just joined
Topic Author
Posts: 4
Joined: Fri Aug 12, 2022 2:33 pm

Re: Issue with data stored in a file

Sat Aug 13, 2022 12:42 am

You have a ton of interesting stuff, it will take some time to go through it all
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Issue with data stored in a file

Sat Aug 13, 2022 3:26 am

{
:local filecontent [/file get "file.txt" contents]
:global tempconv
[:parse ":global tempconv $filecontent"]
:local arrayfromfile $tempconv
:put [:typeof $arrayfromfile]
:put ($arrayfromfile->"D")
}
 
Matas
just joined
Topic Author
Posts: 4
Joined: Fri Aug 12, 2022 2:33 pm

Re: Issue with data stored in a file

Sat Aug 13, 2022 1:23 pm

Thank you! I will definitely test this.

But this is not the only issue I am experiencing with loading stuff from file.
I also have issues with executing imported scripts.
I'm running 7.4.1 from stable branch on RB951

This is a simple example I've made just for this demonstration:
:global username ""
:global password ""
;global hostname "test.local"

:global readInput do={
  :return
}

:global checkHost do={
  :global port "443"
  :if (:put [:find $hostname ".local"]) do={
    :global host [:pick $hostname 0 [:find $hostname ".local"]]
  } else={
    :global host $hostname
    :global hostname ($host . ".local")
  }
  :global url ("https://".$hostname.":".$port."/".$host)
  :put ("URL: " . $url)
}

:global checkCreds do={
  :if ([:len $username] = 0) do={
    :put "Please enter your username:"
    :global username [$readInput]
  }
  :if ([:len $password] = 0) do={
    :put "Please enter your password:"
    :global password [$readInput]
  }
  :put ("username: " . $username)
  :put ("password: " . $password)
}

The $checkCreds function just skips over, and asks nothing. $readInput is not processed.
:if in $checkHost returns false, instead of true, and the $hostname variable is not even evaluated.
This only applies if the script is loaded from the rsc file.
if the code is pasted directly in the terminal, everything works as expected.

Why is this behaviour so unpredictable?
What am I missing?

Image
Image
 
Matas
just joined
Topic Author
Posts: 4
Joined: Fri Aug 12, 2022 2:33 pm

Re: Issue with data stored in a file

Sat Aug 13, 2022 9:48 pm

Okay, I've got it.

the globals need to be registered inside the functions, which, in hindsight, makes sense.
since functions are also variables in RouterOS, they need to be declared as well.
However, this inconsistency is ridiculous.
How can one thing work in terminal, but not in imported script.
The contents of the variables are exactly the same.
@RouterOS devs, please fix this.

For future reference this is the code that works:
:global username ""
:global password ""
;global hostname "test.local"

:global readInput do={
  :return
}

:global checkHost do={
  :global hostname
  :global host
  :global port "443"
  :global url

  :if (:put [:find $hostname ".local"]) do={
    :set host [:pick $hostname 0 [:find $hostname ".local"]]
  } else={
    :set host $hostname
    :set hostname ($host . ".local")
  }
  :set url ("https://".$hostname.":".$port."/".$host)
  :put ("URL: " . $url)
}

:global checkCreds do={
  :global readInput
  :global username
  :global password

  :if ([:len $username] = 0) do={
    :put "Please enter your username:"
    :set username [$readInput]
  }
  :if ([:len $password] = 0) do={
    :put "Please enter your password:"
    :set password [$readInput]
  }
  :put ("username: " . $username)
  :put ("password: " . $password)
}
 
User avatar
BrianHiggins
Forum Veteran
Forum Veteran
Posts: 702
Joined: Mon Jan 16, 2006 6:07 am
Location: Norwalk, CT
Contact:

Re: Issue with data stored in a file

Sat Nov 05, 2022 1:27 am

Hello,
I have a specific issue.
i am downloading some data with /tool fetch command
Thecontent of that file is in correct Mikrotik array format, but when i load it into a variable, it is not recognised as an array, but as an ordinary string instead.
:toarray transforms this string into an array with a single element, which is the complete string.
file contents: {"A"="1";"B"="test";"C"="20";"D"="test2";}
:global data [/file get "file.txt" contents]
:put $data
{"A"="1";"B"="test";"C"="20";"D"="test2";}
:put [:typeof $data ]
str
:put [:toarray $data ]
"A"="1";"B"="test";"C"="20";"D"="test2";
:put ([:toarray $data ]->0)
"A"="1";"B"="test";"C"="20";"D"="test2";
:put ([:toarray $data ]->1)
  (empty)
:put ([:toarray $data ]->"A")
  (empty)
when i copy/paste this string into the terminal, the contents are correctly recognised as an array, and the elements can be called from it
:global data {"A"="1";"B"="test";"C"="20";"D"="test2";}
:put [:typeof $data]
array 
:put ($data->"A")
1
:put ($data->"B")
test
saving files like this also does not help
/file set test1 contents="{\"A\"=\"1\";\"B\"=\"test\";\"C\"=\"20\";\"D\"=\"test2\";}"
contents of the resulting file looks like this, and it has the same issue
{"A"="1";"B"="test";"C"="20";"D"="test2";}
:global data {"A"="1";"B"="test";"C"="20";"D"="test2";}
/file set test123 contents=$data
contents of the resulting file looks like this, but the issue is the same
A=1;B=test;C=20;D=test2

Only data directly pasted into the terminal is properly parsed, anything loaded from the file fails.

Essentially, i want some key->value stored, and read to be used in the script.

Any ideas?
Thanks
I'm struggling with this same exact issue, and have had no luck. I see the response from rextended but the code snippet does not work (I'm not reading from a file, but using output=user for my fetch command, perhaps that changes things?) and I don't see anything he's posted before that solves the issue either.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Issue with data stored in a file

Sat Nov 05, 2022 8:37 am

Is this?
viewtopic.php?p=966051#p966051

continue on that topic

Who is online

Users browsing this forum: Ahrefs [Bot], GoogleOther [Bot] and 17 guests