Script to create directory (Wiki update)
Posted: Tue Sep 11, 2018 11:20 pm
Hello!
First time posting something like this and hoping I at least got the right forum.
I've needed some auto directory creation in my scripts, and found this ancient script on Mikrotik Wiki https://wiki.mikrotik.com/wiki/Script_t ... _directory
Of course, a lot has happened since 2010 and the old script doesn't work "out of the box" anymore.
I'd like to submit my updated version for review and as a possible replacement on the Wiki.
Script was modified on and for v.6.40.9 (bugfix), I don't know if there are problems with other/newer versions.
Here is the original:
and here is my update:
First time posting something like this and hoping I at least got the right forum.
I've needed some auto directory creation in my scripts, and found this ancient script on Mikrotik Wiki https://wiki.mikrotik.com/wiki/Script_t ... _directory
Of course, a lot has happened since 2010 and the old script doesn't work "out of the box" anymore.
I'd like to submit my updated version for review and as a possible replacement on the Wiki.
Script was modified on and for v.6.40.9 (bugfix), I don't know if there are problems with other/newer versions.
Here is the original:
Code: Select all
:local input "mydir/test/dir"
:local passwd ([/system resource get cpu-load] . \
[/system identity get name] . \
[/system resource get free-memory])
# Create group
:if ([:len [/user group find name=("dircreate")]] > 0) do={
/user group remove "dircreate" }
/user group add name=dircreate policy=ftp comment="dircreate group"
# Create user
# Note: this user is restricted to 127.0.0.1 (no outside logins allowed)
:if ([:len [/user find name=("dircreate")]] > 0) do={
/user remove "dircreate" }
/user add name=dircreate group=dircreate address=127.0.0.1 netmask=255.255.255.255 \
comment="dircreate user" password=[:tostr $passwd] disabled=no
# Create tmp file
/system identity export file=dircreate
# Copy tmp file to new location (creating directories as needed)
/tool fetch address=127.0.0.1 mode=ftp user=dircreate password=[:tostr $passwd] \
src-path=dircreate.rsc dst-path=($input . "/dircreate.rsc")
# Clean up
/file remove "dircreate.rsc"
/file remove ($input . "/dircreate.rsc")
/user remove "dircreate"
/user group remove "dircreate"
Code: Select all
#Absolute path to your desired directory, will not overwrite files in it if allready exists
:local input "mydir/test/dir"
:local passwd ([/system resource get cpu-load] . \
[/system identity get name] . \
[/system resource get free-memory])
#Check for existing "dircreate" user and remove, user must be removed before the group can
:if ((:len [/user find name="dircreate"]) !=nil) do={
/user remove [find name="dircreate"]
}
#Check for existing "dircreate" group and remove
:if ((:len [/user group find name="dircreate"]) !=nil) do={
/user group remove [find name="dircreate"]
}
#Check for existing "dircreate" temp file and remove
:if ((:len [/file find name="dircreate.rsc"]) !=nil) do={
/file remove [find name="dircreate.rsc"]
}
# Create group
/user group add name=dircreate policy=ftp,read,write comment="dircreate group"
# Create user
# Note: this user is restricted to 127.0.0.1 (no outside logins allowed)
/user add name=dircreate group=dircreate address=127.0.0.1/32 \
comment="dircreate user" password=[:tostr $passwd]
# Create tmp file
/system identity export file=dircreate
# Copy tmp file to new location (creating directories as needed)
/tool fetch address=127.0.0.1 mode=ftp user=dircreate password=[:tostr $passwd] \
src-path=dircreate.rsc dst-path=($input . "/dircreate.rsc")
# Clean up
/file remove ($input . "/dircreate.rsc")
/file remove "dircreate.rsc"
/user remove [find name="dircreate"]
/user group remove [find name="dircreate"]