Community discussions

MikroTik App
 
vodokotlic
just joined
Topic Author
Posts: 16
Joined: Thu Mar 29, 2018 11:13 pm

Script to create directory (Wiki update)

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:
: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"
and here is my update:
#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"]
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: Script to create directory (Wiki update)

Thu Sep 13, 2018 8:46 pm

Easier and safer
[/tool fetch dst-path="/dir1/dir2/ " url="http://127.0.0.1:80/mikrotik_logo.png" keep-result=no]
 
mikhed
just joined
Posts: 4
Joined: Fri May 17, 2019 11:07 am

Re: Script to create directory (Wiki update)

Tue Jul 02, 2019 4:01 pm

much more easy and safier:
- no enabled ftp/tftp services
- only internet access
- one command:

create folder "newfolder":
/tool fetch dst-path="/newfoler/filename-not-saved" url="https://ya.ru/" keep-result=no mode=https
or
/tool fetch dst-path="/newfoler/filename-not-saved" url="https://www.google.com/" keep-result=no mode=https

Who is online

Users browsing this forum: Google [Bot] and 31 guests