Community discussions

MikroTik App
 
User avatar
CZFan
Forum Guru
Forum Guru
Topic Author
Posts: 2098
Joined: Sun Oct 09, 2016 8:25 pm
Location: South Africa, Krugersdorp (Home town of Brad Binder)
Contact:

Script to delete itself after executing...

Tue Oct 29, 2019 12:46 pm

I have read something about this a while ago on this forum, but cant seem to find it...

I have created a script to configure the devices using System-->Reset Configuration and enabling no backup / no default and then select script to run after reset.

My concern here is that this script also sets different levels of admin permissions and I want the script to auto delete itself after execution so that these credentials can not be accessed / viewed via the script file.

Is this possible, if so, how?
 
User avatar
cdiedrich
Forum Veteran
Forum Veteran
Posts: 997
Joined: Thu Feb 13, 2014 2:03 pm
Location: Basel, Switzerland // Bremen, Germany
Contact:

Re: Script to delete itself after executing...

Tue Oct 29, 2019 1:32 pm

If the script is a file on the device, the last line of the script should be
/file remove yourscript.rsc
If the script is in the internal script repository, the last line should be
/system script remove where name=yourscriptname

-Chris
 
User avatar
CZFan
Forum Guru
Forum Guru
Topic Author
Posts: 2098
Joined: Sun Oct 09, 2016 8:25 pm
Location: South Africa, Krugersdorp (Home town of Brad Binder)
Contact:

Re: Script to delete itself after executing...

Tue Oct 29, 2019 6:44 pm

Thx Chris,

Yes, it will be a file on router

I tried /file remove [find where name="mycript.rsc"] as last line and then the script fails / does not execute. Any reason why this will cause it to fail? If I remove that line, then script executes fine.
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Script to delete itself after executing...

Tue Oct 29, 2019 7:22 pm

Just a stupid guess... The code above is missing a "s" in the filename. That's not the reason for your failure, no?
 
User avatar
cdiedrich
Forum Veteran
Forum Veteran
Posts: 997
Joined: Thu Feb 13, 2014 2:03 pm
Location: Basel, Switzerland // Bremen, Germany
Contact:

Re: Script to delete itself after executing...

Tue Oct 29, 2019 7:41 pm

It's only a guess, but I wouldn't be surprised if the script file is locked during execution and can't be deleted because of that.
I could imagine a workaround:

in your config script, create a scheduler item that runs on startup and deletes that file. It then removes itself from scheduler with the same procedure (this is proven working).
The last line of your config script would now be a reboot.

-Chris
 
User avatar
CZFan
Forum Guru
Forum Guru
Topic Author
Posts: 2098
Joined: Sun Oct 09, 2016 8:25 pm
Location: South Africa, Krugersdorp (Home town of Brad Binder)
Contact:

Re: Script to delete itself after executing...

Wed Oct 30, 2019 12:02 am

@eworm, no, the file name used there is an arbitrary name.

@Chris, I think you are on the money, will test it
 
User avatar
vecernik87
Forum Veteran
Forum Veteran
Posts: 882
Joined: Fri Nov 10, 2017 8:19 am

Re: Script to delete itself after executing...  [SOLVED]

Wed Oct 30, 2019 3:48 am

It's only a guess, but I wouldn't be surprised if the script file is locked during execution and can't be deleted because of that.
File is not locked. I delete my deployment script with following command (written as a last part of the init.rsc file):
:do {
/file remove flash/init.rsc
} on-error={};
:do {
/file remove init.rsc
} on-error={};
This way it works for both ramdisk and non-ramdisk devices.

Obviously, it does not cover "arbitrary" name. I simply use the same name all the time because why would I rename it, right? Not sure if there is a way to detect script's own name
 
User avatar
CZFan
Forum Guru
Forum Guru
Topic Author
Posts: 2098
Joined: Sun Oct 09, 2016 8:25 pm
Location: South Africa, Krugersdorp (Home town of Brad Binder)
Contact:

Re: Script to delete itself after executing...

Wed Oct 30, 2019 2:01 pm

Thank you @vecernik87

Changed yours a bit and added to bottom of my script and will test with a new remote deployment later today:
:do {
/file remove [find where name="myscript.rsc" || name="flash/myscript.rsc"]
} on-error={:log error "Unable to delete Config Script File..."};
Just as a matter of interest, my script starts with "{" and ends again with "}" Is this required, what are the pro / cons if any?

Who is online

Users browsing this forum: quanbau and 6 guests