Page 1 of 1
Can the content written to the file be added?
Posted: Mon Jan 15, 2024 4:38 pm
by rosa
/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?
Re: Can the content written to the file be added?
Posted: Mon Jan 15, 2024 4:45 pm
by rextended
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"
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 4:38 am
by rosa
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.
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 10:14 am
by rextended
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.
Re: Can the content written to the file be added? [SOLVED]
Posted: Tue Jan 16, 2024 10:39 am
by vingjfg
/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.
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 10:54 am
by rextended
Rosa, I think your answer is in […]
And when you write that that method is limited to 4kB or 64kB? (depending on RouterOS version)
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 11:58 am
by diamuxin
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
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 3:48 pm
by rosa
/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~
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 3:52 pm
by rosa
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?
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 3:56 pm
by rosa
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?
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 5:01 pm
by diamuxin
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.
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 5:18 pm
by rosa
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?
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 6:29 pm
by diamuxin
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"
}
Re: Can the content written to the file be added?
Posted: Tue Jan 16, 2024 9:38 pm
by rextended
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.
Re: Can the content written to the file be added?
Posted: Wed Jan 17, 2024 3:52 am
by rosa
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.
Re: Can the content written to the file be added?
Posted: Wed Jan 17, 2024 4:01 am
by rosa
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~
Re: Can the content written to the file be added?
Posted: Wed Jan 17, 2024 6:29 pm
by rextended
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.
Re: Can the content written to the file be added?
Posted: Thu Jan 18, 2024 3:36 am
by rosa
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,