I am trying to make a script that counts the number of trial users online and writes it to a file. I want it to count the number of users every hour, and add the result to a new line in the file (to be used as a CSV file). However, I think I am doing the re-write wrong, as the file includes the file name of every file on the router as well as the results.
How should I be handling this re-writing?
#Initialising Variables
#Varible to set the frommaddress to the systemname@some.server
:global system [/system identity get name]
:global time [/system clock get time]
:global date [/system clock get date]
#Variable for user name
:local userno
#Variable for text file contents
:local filetext
:local headings
:set headings ("Date,Time,Users\n")
#check if file already exists, if not create it
:local check [/file find name=usersperhourreport.txt]
:if ( $check = "" ) do= {
#Create file
/file print file=usersperhourreport
#Set the headings for the columns (\n for new line)
/file set usersperhourreport.txt contents="$headings\n"
}
#Pull details out of user list and assign to relevant variables accordingly
:set userno [/ip hotspot active print count-only]
#Insert the values to the text file
:set filetext ("$date,$time,$userno\n")
/file set usersperhourreport.txt contents=([/file get [/file find name=usersperhourreport.txt] contents] . "\n$filetext");
:delay 5
/tool e-mail send to="me@some.server" subject=($system . " Users per Hour Usage Report") body="Attached is the hourly usage report for $system made at $time on $date \nNote: The attached file is a comma seperated value file, simply import into a spreadsheet" from=libraryreports@some.server file=usersperhourreport.txt server=999.999.999.999
:log info "Report Emailed"
why is it outputting all the files on the router with their details, along with router info? All I want is the last few lines. Also, why arent the headings output?
Yeah I tried that and kept getting an error…thought I might have been doing it wrong :S
[admin@Maroochy Library Test] > /file
[admin@Maroochy Library Test] /file> add
error - contact MikroTik support and send a supout file (3
[admin@Maroochy Library Test] /file> /file add
error - contact MikroTik support and send a supout file (3)
[admin@Maroochy Library Test] /file> /file add contents= header
error - contact MikroTik support and send a supout file (3)
[admin@Maroochy Library Test] /file> /file add contents="header"
error - contact MikroTik support and send a supout file (3)
[admin@Maroochy Library Test] /file> /file add contents=header
error - contact MikroTik support and send a supout file (3)
If you have any ideas that’d be great. I can get around it for now as I can make the file manually, but when this goes to production I will not be able to do it for multiple sites and would rather the script did it.
This works. I used the errors.txt file from the hotspot directory.
:put [/file get hotspot/errors.txt contents]
Just so you know…when your file gets to 4095 bytes, that is it. Add any more and the “contents” will not be editable/readable.
ADD: I posted a request for reading larger files in the next release. omega-00 posted a request for writing larger files also. We are still waiting…
And I will presume the reason the “/file add” doesn’t work is: there is no input available for the filename. A blank filename would/should generate an error, don’t you think? OK, MT team, get on it!!
Unless that is a typo, you are trying to print a file that may not exist.
/file print file=usersperhourreport
/file set usersperhourreport.txt contents=" "
These are NOT accessing the same file. If the file usersperhourreport (no extension) doesn’t exist, the script may abort at that command. It will not execute the “/file set” line.
EDIT: The command above does create the file usersperhourreport.txt. And it works ok here run from a command line. It sets even large files to a length of 1. Don’t know about running as a schedule tho…
Not certain what the purpose of this script is…it writes the file with a list of filenames, then immediately erases it. ???
My usersperhourreport.txt file is right at the limit for size. Almost 4000 bytes. Another 100 bytes of filenames, and it will fail the print command.
ADD: I SEE NOW!! If “/file add” doesn’t work, there is always another way of creating an empty file. VERY GOOD!
It doesn’t work, it just prints all the files on RB!
Obviously there is a problem with “/file set” command when running it inside scripts. When I create script file with something like this:
/file print file=test;
/file set test.txt content=miro;
it creates file ‘test.txt’, ie. executes first command, but changing the content of the file with second command isn’t successful. Soon after executing sript I tried to change the content of the file via console and it works. Creating another script file with just “/file set test.txt content=miro;”, and running it, doesn’t change the content of the file I created with first script…
MT versions I’m working with are 3.20 and 3.22…
I use this for autobackup…but it doesn’t work
Any solution?
Hmm, when I start script again without erasing the file (test.txt), it writes the content ‘lala’ in the text !
The problem exist only when creating the file first time.
I’ll try with newest ver.
One more question, why “/file add” command don’t work ?
Simply file add is not implemented and never will be.
v4.0 will have better support to work with files. In v3.x it is just a workaround with too many limitations.
/file print file=test
:delay 1
/file set test.txt contents="lala"
This works on v3.24. Seems this could be a ‘feature’ with timing. Without the delay, the contents ‘lala’ are not entered into the file. Presumably as the system hasn’t completed the task of closing the file after the initial write when creating it.
I’m on v4.1, and am still having an issue creating files larger than 4096 bytes.
Is this still a limitation in v4.x? BTW, don’t mention Lua, not sure when (or if) it’s going to be implemented.
Also, I am able to split a files into pieces, (ex. file1.txt, file2.txt, file3.txt) Each file is 4096 bytes. Can I make file.txt with contents of all three, and size would be 3 x 4096 bytes?
Lol, dang that’s going to be a long time! What can I do in the meantime? There has to be a way to get a file larger than 4096 bytes . Surely there must be a way…
Just curious, what script language is v3 and v4 currently? Is it custom?