Community discussions

MikroTik App
 
andrescamino
Member Candidate
Member Candidate
Topic Author
Posts: 198
Joined: Wed Aug 19, 2009 7:07 pm
Location: Guayaquil-Ecuador

problem with array scripting

Tue Sep 28, 2010 7:58 pm

Hello to all,

I would like to create an array to add users and password to hotspot, this is what i've done but it doesn't work
:global usuarios [andres;daniel;christian];
:global contrasena [andres;daniel;chritian];
:for i from 1 to 3 do={
/ip hotspot user add name=$usuarios($i) password=$contrasena($i)
}
it seems that it accept the script until name=...but after that it doesn't recognize it

what would be the problem here?
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: problem with array scripting

Tue Sep 28, 2010 10:00 pm

Try this:
:global usuarios [:toarray ("andres","daniel","chritian")];
:global contrasena [:toarray ("andrespw","danielpw","chritianpw")];

:for i from=0 to=([:len $usuarios]-1) do={
    /ip hotspot user add name=[:pick $usuarios $i] password=[:pick $contrasena $i];
}
 
andrescamino
Member Candidate
Member Candidate
Topic Author
Posts: 198
Joined: Wed Aug 19, 2009 7:07 pm
Location: Guayaquil-Ecuador

Re: problem with array scripting

Tue Sep 28, 2010 10:05 pm

Tks a lot SurferTim, what if I would like to import the username and password from a excel or notepad file...Can I do that?
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: problem with array scripting

Tue Sep 28, 2010 10:10 pm

I would say yes, but I haven't tried it. I have imported data from files for GPS position before. Requires parsing the file contents, and the largest file allowed will be 4095 characters. Is that enough?

If you are looking to add users to the hotspot dynamically, may I recommend a radius package, like User Manager or FreeRADIUS.
 
andrescamino
Member Candidate
Member Candidate
Topic Author
Posts: 198
Joined: Wed Aug 19, 2009 7:07 pm
Location: Guayaquil-Ecuador

Re: problem with array scripting

Tue Sep 28, 2010 10:18 pm

I have worked before with User Manager, and it works fine, but when you create a batch of users you can't assign the username and password for yourself, it generates it for you and that's not basically what I want...

4095 characters is more than enough for me...could you give some guidance in how to do this?...any turorial or manual?

Thanks a lot for your help
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: problem with array scripting

Tue Sep 28, 2010 10:31 pm

What do you mean "can't assign user and password for yourself"? You can enter any user name and password in User Manager and FreeRADIUS. The user can even create his/her own account in User Manager. FreeRADIUS requires a php script to interface with the radius MySQL database to allow customers to enter their own account.

If you are set on trying the text file parsing, here is part of a script I wrote for GPS that parses file data. You will need to figure out how to parse the user/password from each line. The "\n" in the find statements indicates a new line.
:global gpstext [/file get gps.txt contents];
:local longstart [:find $gpstext "longitude" -1];
:local longend [:find $gpstext "\n" $longstart];
:local latstart [:find $gpstext "latitude" -1];
:local latend [:find $gpstext "\n" $latstart];
:local validstart [:find $gpstext "valid" -1];
:local validend [:find $gpstext "\n" $validstart];
 
andrescamino
Member Candidate
Member Candidate
Topic Author
Posts: 198
Joined: Wed Aug 19, 2009 7:07 pm
Location: Guayaquil-Ecuador

Re: problem with array scripting

Tue Sep 28, 2010 10:42 pm

I know you can...but when you add a batch of users...like 100 users with one click...it just creates user for you like username:ujfs45 password:je454j3...

Ok I kind of get the code...but can you give me an example of the gps.txt? how would it look like? so i can understand how to pick the latitude, longitude etc. and apply a similar concept with my Users.txt that would look like this

User,Password
andres,andrespw
daniel,danielpw
christian,christianpw



thanks a lot for your help
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: problem with array scripting

Tue Sep 28, 2010 10:52 pm

I would call the file hsusers.txt, and format the user/passwords like this:

andres,andrespw
daniel,danielpw
christian,christianpw

Then a loop til the end of the file contents.

Check back here. I might try a simple parse script. It doesn't look too bad. I'm not promising anything tho. If I start getting phone calls from my customers, they get priority.
 
andrescamino
Member Candidate
Member Candidate
Topic Author
Posts: 198
Joined: Wed Aug 19, 2009 7:07 pm
Location: Guayaquil-Ecuador

Re: problem with array scripting

Tue Sep 28, 2010 11:05 pm

off course SurferTim I understand, customer is first.

Thanks in advance for the support of the parse script! :D
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: problem with array scripting

Tue Sep 28, 2010 11:53 pm

You were lucky. Only one call. Upload file "hsusers.txt" to the router. A line feed after every line, even after the last line. But no extra blank lines at the end!! No spaces, just the comma.
andres,andrespw
daniel,danielpw
christian,christianpw
:global filetext [/file get hsusers.txt contents];
:local filepos 0;

:global usuarios;
:global contrasena;
:local linend;
:local thisline;
:local userend;

:while ($filepos < ([:len $filetext]-1)) do={
    :set linend [:find $filetext "\n" $filepos];
    :set thisline [:pick $filetext $filepos $linend];
    :set userend [:find $thisline "," -1];
    :set usuarios [:pick $thisline 0 $userend];
    :set contrasena [:pick $thisline ($userend+1) [:len $thisline]];

    :put $usuarios;
    :put $contrasena;

    :set filepos ($linend+1);
    /ip hotspot user add name=$usuarios password=$contrasena
}
 
andrescamino
Member Candidate
Member Candidate
Topic Author
Posts: 198
Joined: Wed Aug 19, 2009 7:07 pm
Location: Guayaquil-Ecuador

Re: problem with array scripting

Wed Sep 29, 2010 12:26 am

Tks so much SurferTim!

it works great!

Who is online

Users browsing this forum: JDF and 30 guests