Community discussions

MikroTik App
 
User avatar
akira463
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Thu Jan 11, 2018 12:30 pm
Location: Ph

Foreach data string holding

Tue Feb 28, 2023 7:27 pm

Hello master, Im here again with another question.

I do want to write all ppp secret details txt file format, The problem is, when i run my code, it only register one name , what could be wrong?
:local filedata ""

:foreach i in=[/ppp secret find] do={
  :local name [/ppp secret get $i name]
  :local password [/ppp secret get $i password]
  :set  $filedata ("$name:$password\n")
}
/file print file="ppp-secrets.txt"
:delay 2
/file set "ppp-secrets.txt" content=$filedata
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Foreach data string holding

Tue Feb 28, 2023 9:28 pm

You do not keep previous values....

set is... set, not "add at the end"
set x a
set x b
set x c
and at the end x is only c, not "abc"

Please write code correctly, no $ on variable name after global, local or set
:local filedata ""
/ppp secret
:foreach i in=[find] do={
  :local name     [get $i name]
  :local password [get $i password]
  :set filedata "$filedata$name:$password\r\n"
}
/file print file="ppp-secrets.txt"
:delay 2s
/file set "ppp-secrets.txt" content=$filedata
 
User avatar
akira463
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Thu Jan 11, 2018 12:30 pm
Location: Ph

Re: Foreach data string holding

Wed Mar 01, 2023 2:52 am

Thankyou for the reply sir, but the problem sir still insist, it does not work :( . i also tried in telegram, but still i got no reply
here's my code
#telegram
    :local botToken "XXXXXXXXX:XXXXXXX"
    :local chatID "-XXXXXXX"

:local filedata ""
/ppp secret
:foreach i in=[find] do={

  :local name     [get $i name]
  :local password [get $i password]
  :set filedata "$filedata$name:$password\r\n"
}

/tool fetch url="https://api.telegram.org/bot$botToken/sendMessage?chat_id=$chatID&text=test $filedata"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Foreach data string holding

Wed Mar 01, 2023 11:39 am

The script on post #2 for me works, but how many items are?
The file created on this way can not exceed ~4000 Bytes...


On terminal, THIS work? (do not omit parenthesys { })
{
:local filedata ""
/ppp secret
:foreach i in=[find] do={
  :local name     [get $i name]
  :local password [get $i password]
  :set filedata "$filedata$name:$password\r\n"
}
:put $filedata
}

You can not send the data directly to telegram if you do not convert it to UTF-8 + URLencode....
And probably GET on fetch can not support more than 256 characters (not tested)
 
User avatar
akira463
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Thu Jan 11, 2018 12:30 pm
Location: Ph

Re: Foreach data string holding

Wed Mar 01, 2023 4:12 pm

The script on post #2 for me works, but how many items are?
The file created on this way can not exceed ~4000 Bytes...


On terminal, THIS work? (do not omit parenthesys { })


Yes sir, on terminal it works, but when i create file.txt, it does not.. what could be the solution ?

here's the result after running the script, it just log the file tree
zasd.PNG
You do not have the required permissions to view the files attached to this post.
 
User avatar
akira463
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Thu Jan 11, 2018 12:30 pm
Location: Ph

Re: Foreach data string holding

Wed Mar 01, 2023 5:07 pm

Thank you sir, It works now, but the problem the limit of the data must only 4000bytes as you said, anyway, thankyou so much!!!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Foreach data string holding

Wed Mar 01, 2023 5:54 pm

Thank you sir, It works now, but the problem the limit of the data must only 4000bytes as you said, anyway, thankyou so much!!!

If are not soo much, consider this.... remove unused start letters on your secrets, or add numbers or symbols you use...

/ppp secret
:foreach l in=[:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"] do={
    :local filedata ""
    :local letter $l
    :local filename "ppp-secrets-$letter.txt"
    /file print file=$filename
    :delay 2s
    :foreach i in=[find where name~"^$l"] do={
        :local name [get $i name]
        :local password [get $i password]
        :set filedata "$filedata$name:$password\r\n"
    }
    /file set $filename content=$filedata
}
 
User avatar
akira463
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Thu Jan 11, 2018 12:30 pm
Location: Ph

Re: Foreach data string holding

Thu Mar 02, 2023 5:10 pm

Thankyou again sir. Youre the best!!! 😁😁😁

Who is online

Users browsing this forum: No registered users and 15 guests