Community discussions

MikroTik App
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Can the content written to the file be added?

Mon Jan 15, 2024 4:38 pm

/file print file=result.txt
/file set [find name="result.txt"] contents=[/interface pppoe-client get [find name=pppoe-out1] password]
-------------------------------------------------------------------------------------------------------------------------------------------------------
A script like this can generate a file containing variables, but the file content is only one line. If there are new variables, how can they be added to the second line of result.txt?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can the content written to the file be added?

Mon Jan 15, 2024 4:45 pm

add the actual content before add the new value?

on your way:
x = 3
x is 3

x = 4
x is 4

x can't be 7 (or 3,4) on this way




as numbers
x = 3
x = x + 4
x is 7

as strings:
x = "3"
x = x + "<CR><LF>4"
x is "3<CR><LF>4"
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can the content written to the file be added?

Tue Jan 16, 2024 4:38 am

Sorry, maybe you didn't understand what I meant! What I mean is adding content to a text file as the second line, is this feasible?

/file set [find name="result.txt"] contents=[/interface pppoe-client get [find name=pppoe-out1] password+"<CR><LF>"]
/file set [find name="result.txt"] contents=[/interface pppoe-client get [find name=pppoe-out2] password+"<CR><LF>"]
After running this command, there is always only one line of content in the file.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can the content written to the file be added?

Tue Jan 16, 2024 10:14 am

Sorry, maybe you didn't understand what I meant!
So if you can't do something, is it my fault that I don't understand you?
I'm sorry, but as usual, instead of trying to understand how it should be done,
you want to do it the way you think it should be done, so I can't help you understand how it should correctly be done.
 
User avatar
vingjfg
Member Candidate
Member Candidate
Posts: 291
Joined: Fri Oct 20, 2023 1:45 pm

Re: Can the content written to the file be added?  [SOLVED]

Tue Jan 16, 2024 10:39 am

/file print file=result.txt
/file set [find name="result.txt"] contents=[/interface pppoe-client get [find name=pppoe-out1] password]
-------------------------------------------------------------------------------------------------------------------------------------------------------
A script like this can generate a file containing variables, but the file content is only one line. If there are new variables, how can they be added to the second line of result.txt?
Rosa, I think your answer is in viewtopic.php?t=98730.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can the content written to the file be added?

Tue Jan 16, 2024 10:54 am

Rosa, I think your answer is in […]
And when you write that that method is limited to 4kB or 64kB? (depending on RouterOS version)
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Can the content written to the file be added?

Tue Jan 16, 2024 11:58 am

example code

/system clock
:local dt        "$[get date] $[get time]"
:local filename  "datetime.txt"

/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 1s; set $filename contents=""}

:local filecontent    [get $filename contents]
:local newfilecontent "New:\t$dt\r\n$filecontent"
set $filename contents=$newfilecontent
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can the content written to the file be added?

Tue Jan 16, 2024 3:48 pm

/file print file=result.txt
/file set [find name="result.txt"] contents=[/interface pppoe-client get [find name=pppoe-out1] password]
-------------------------------------------------------------------------------------------------------------------------------------------------------
A script like this can generate a file containing variables, but the file content is only one line. If there are new variables, how can they be added to the second line of result.txt?
Rosa, I think your answer is in viewtopic.php?t=98730.
Thank you~I basically understand~
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can the content written to the file be added?

Tue Jan 16, 2024 3:52 pm

example code

/system clock
:local dt        "$[get date] $[get time]"
:local filename  "datetime.txt"

/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 1s; set $filename contents=""}

:local filecontent    [get $filename contents]
:local newfilecontent "New:\t$dt\r\n$filecontent"
set $filename contents=$newfilecontent
Thank you for your script example, but this is not what I want. My script loop will produce some results, and my main purpose is to save these results to a file because saving to a file can prevent sudden power outages from time to time. Besides/file set, is there any other good way?
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can the content written to the file be added?

Tue Jan 16, 2024 3:56 pm

Sorry, maybe you didn't understand what I meant!
So if you can't do something, is it my fault that I don't understand you?
I'm sorry, but as usual, instead of trying to understand how it should be done,
you want to do it the way you think it should be done, so I can't help you understand how it should correctly be done.
After reading the link provided by my friend below, I can understand what you're saying now. SORRY~I have a lot of results to save, so using [/file set] to stack all the time is not very good! I mainly want to save these results to a file to prevent occasional sudden power outages and data loss. Is there any other good way, Elder?
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Can the content written to the file be added?

Tue Jan 16, 2024 5:01 pm

Thank you for your script example, but this is not what I want. My script loop will produce some results, and my main purpose is to save these results to a file because saving to a file can prevent sudden power outages from time to time. Besides/file set, is there any other good way?
I store my sensitive information on an FTP server located within my LAN.
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can the content written to the file be added?

Tue Jan 16, 2024 5:18 pm

Thank you for your script example, but this is not what I want. My script loop will produce some results, and my main purpose is to save these results to a file because saving to a file can prevent sudden power outages from time to time. Besides/file set, is there any other good way?
I store my sensitive information on an FTP server located within my LAN.
Can multiple results be stored in the same file? Is HTTP or FTP okay? Or can it only be FTP? Can you provide some sample scripts?
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Can the content written to the file be added?

Tue Jan 16, 2024 6:29 pm

Can multiple results be stored in the same file? Is HTTP or FTP okay? Or can it only be FTP? Can you provide some sample scripts?
1) Yes, you can save several results in the same file: see example in my previous post.

2) I have only tested it by FTP and it works.

3) Example of script using FTP

# FTP Config
:local ftpserver "192.168.88.10"
:local username  "myftpusername"
:local password  "myftppassword"
:local port      21

/system clock
:local dt        "$[get date] $[get time]"
:local filename  "datetime.txt"

/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 1s; set $filename contents=""}

:local filecontent    [get $filename contents]
:local newfilecontent "New:\t$dt\r\n$filecontent"
set $filename contents=$newfilecontent

:delay 1s
:do {
/tool fetch address=$ftpserver mode=ftp user=$username password=$password \
    src-path=$filename dst-path=$filename upload=yes port=$port
:log info "FTP Backup: File $textfilename sent successfully"
:delay 5s
/file remove $filename
} on-error={
        :log info "=> FTP sent failed"
}

 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can the content written to the file be added?

Tue Jan 16, 2024 9:38 pm

After reading the link provided by my friend below, I can understand what you're saying now.
Oh, finally you understand and now I can help you without brainless cut-and-paste

some code

{
:local filename "history_users_and_passwords.txt"
:local pppoeArr [/interface pppoe-client print detail as-value]
:local addThis  ""

:foreach item in=$pppoeArr do={
    :set addThis ($addThis."Password for user >$($item->"user")< is >$($item->"password")<\r\n")
}

/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 4s; set $filename contents=""}
:delay 2s
:local filecontent [get $filename contents]
:local newfilecontent "$filecontent$addThis"
set $filename contents=$newfilecontent
}

But it would be better if you explained exactly what you need:
Do you lose your password when the device turns off???
Probably you just need one "export" for backup all the RouterBARD configuration, and save it elsewhere.
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can the content written to the file be added?

Wed Jan 17, 2024 3:52 am

Can multiple results be stored in the same file? Is HTTP or FTP okay? Or can it only be FTP? Can you provide some sample scripts?
1) Yes, you can save several results in the same file: see example in my previous post.

2) I have only tested it by FTP and it works.

3) Example of script using FTP

# FTP Config
:local ftpserver "192.168.88.10"
:local username  "myftpusername"
:local password  "myftppassword"
:local port      21

/system clock
:local dt        "$[get date] $[get time]"
:local filename  "datetime.txt"

/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 1s; set $filename contents=""}

:local filecontent    [get $filename contents]
:local newfilecontent "New:\t$dt\r\n$filecontent"
set $filename contents=$newfilecontent

:delay 1s
:do {
/tool fetch address=$ftpserver mode=ftp user=$username password=$password \
    src-path=$filename dst-path=$filename upload=yes port=$port
:log info "FTP Backup: File $textfilename sent successfully"
:delay 5s
/file remove $filename
} on-error={
        :log info "=> FTP sent failed"
}

Thank you, thank you. After checking, it seems that I still use/file set, so uploading to FTP is not very meaningful! Why not save it directly to the local location! The main issue is that the/file set can only save about 4K of content, and my return result may be greater than 4K.
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can the content written to the file be added?

Wed Jan 17, 2024 4:01 am

After reading the link provided by my friend below, I can understand what you're saying now.
Oh, finally you understand and now I can help you without brainless cut-and-paste

some code

{
:local filename "history_users_and_passwords.txt"
:local pppoeArr [/interface pppoe-client print detail as-value]
:local addThis  ""

:foreach item in=$pppoeArr do={
    :set addThis ($addThis."Password for user >$($item->"user")< is >$($item->"password")<\r\n")
}

/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 4s; set $filename contents=""}
:delay 2s
:local filecontent [get $filename contents]
:local newfilecontent "$filecontent$addThis"
set $filename contents=$newfilecontent
}

But it would be better if you explained exactly what you need:
Do you lose your password when the device turns off???
Probably you just need one "export" for backup all the RouterBARD configuration, and save it elsewhere.
Oh, Elder! I am going to perform a PPPOE dialing test on 10000 numbers, then record the normal numbers and save them to a file, as there may be many results, resulting in file sizes exceeding 4K. I have read your article and it seems that [/file set contents] has a 4K limit! The ROS version I am currently using is 6.48.7. Do you know if the latest version of ROS has any other new and more convenient script operation modes? If you can only use [/file set contents], can you only save the results in segments? Can you provide some relevant scripts~
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can the content written to the file be added?

Wed Jan 17, 2024 6:29 pm

on some 7.13.something is 64KB

use

/interface pppoe-client export file=exportuserandpassword

the export is "dirty" of other values, but export all users.
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can the content written to the file be added?

Thu Jan 18, 2024 3:36 am

on some 7.13.something is 64KB

use

/interface pppoe-client export file=exportuserandpassword

the export is "dirty" of other values, but export all users.
[/interface pppoe client export file] This feature is not sufficient~It exports all information, I just want the value that can successfully dial the result. Thank you very much! Elder, big! I will try the latest ROS 7.13, and if the [/file set contents] value can reach 64KB, then it basically meets the requirements. One hundred million is about 88K, and the most successful outcome is only one-third,

Who is online

Users browsing this forum: alixviral and 2 guests