Script to delete itself after executing...

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?

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

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.

Just a stupid guess… The code above is missing a “s” in the filename. That’s not the reason for your failure, no?

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

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

@Chris, I think you are on the money, will test it

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

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?