Community discussions

MikroTik App
 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

hello i need help

Thu Apr 06, 2023 4:51 am

Hello, I don't speak English very well and I use Google Translate
I want to create a script to create a hotspot user in a telst file that can identify the word preceded by a symbol or name, for example, like the picture I put
You do not have the required permissions to view the files attached to this post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 11:27 am

Use a text editor,
open the file,

replace
" "
with
<nothong>

replace
"username="
with
"/ip hotspot user add name="

replace
"&password="
with
" profile=all password=" (notice the space at the start)


Simply alter the "program" that create that lines on the txt file...
and yuou can import the file with /import command on terminal.
 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

Re: hello i need help

Thu Apr 06, 2023 6:27 pm

Thanks, but I want to script it as the picture I want the last line I specified
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 6:29 pm

Thanks, but I want to script it as the picture I want the last line I specified
Is simpler that you alter the "program" that create that lines on the txt file...
Or is done by "excel"?
 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

Re: hello i need help

Thu Apr 06, 2023 7:35 pm

Thanks I want to do the work on txt I have a special reason if possible thanks again
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 7:41 pm

It's not an easy thing, you need someone to do it from scratch and to follow you step by step...

Can't you alter the file with powershell before uploading it to routeros? (is more easy)

You download it from internet or do you drag the file there by hand?
 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

Re: hello i need help

Thu Apr 06, 2023 8:40 pm

I'm downloading it from the Internet
 /tool fetch mode=https url="https://example.com/get_user_credentials.php" dst-path=user_credentials.txt
:local username [/file get user_credentials.txt contents | find "username" | :pick [ :find "]"] | :pick [1 [:len $result - 1]]
:local password [/file get user_credentials.txt contents | find "password" | :pick [ :find "]"] | :pick [1 [:len $result - 1]]
:local profile "hotspot"

/ip hotspot user add name=$username password=$password profile=$profile

/file remove user_credentials.txt
I say like and that but I don't know if that won't work
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 8:45 pm

I do not know if you try to apply your knowledge from linux or you ask ChatGPT for novels,
but that is not RouterOS script: for example "|" do not exist as intended on that scipt.
Why is present "]"? Is a json output?

You'd better fix the php for have the correct output...
just have it write the data I wrote before instead of "username=" and "password=" strings....

Sorry, but I stop here, as I already wrote:
It's not an easy thing, you need someone to do it from scratch and to follow you step by step...
 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

Re: hello i need help

Thu Apr 06, 2023 8:50 pm

I understand, thank you very much. I want to create a user hotspot through the Telegram bot. Is there any way? That's right, I got that from Chat GPT4
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 8:54 pm

 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

Re: hello i need help

Thu Apr 06, 2023 9:01 pm

thanks
 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: hello i need help

Thu Apr 06, 2023 9:43 pm

ChatGPT response :):
:local profile "hotspot"
:local fileName "user_credentials.txt"

:local fileCnt [/file get $fileName contents]
:local fileLen [:len $fileCnt]
:local lineLen 0
:local prevLineLen 0
:local lineCnt 0
:local line ""
:local notBlank ".*\\S.*"
:local uTokenLen [:len "username= "]
:local pTokenLen [:len "passowrd= "]
:local sepLen [:len " & "]

:local usernames
:local passwords

:while ($lineLen < $fileLen) do={
  :set lineLen [:find $fileCnt "\n" $prevLineLen]
  :if ([:len $lineLen] = 0) do={ :set lineLen $fileLen }
  :set line [:pick $fileCnt $prevLineLen $lineLen]
  :set prevLineLen ($lineLen + 1)

  :if ($line != "\r") do={
    :local username [:pick $line $uTokenLen [:find $line " &" ]]

    if ($username ~ $notBlank) do= {
      :local password [:pick $line ($uTokenLen + [:len $username] + $sepLen + $pTokenLen) $lineLen]
      :set ($usernames->$lineCnt) $username
      :set ($passwords->$lineCnt) $password
      :set lineCnt ($lineCnt + 1)
    }
  }
}

:for i from=0 to=([:len $usernames] - 1) do={
  :local username [:pick $usernames $i]
  :local password [:pick $passwords $i]
  /ip/hotspot/user add name=$username password=$password profile=$profile
}

/file remove $fileName
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 9:56 pm

Just a bullshit at the start...
:local notBlank ".*\\S.*"

The \s metacharacter matches whitespace character.

Whitespace characters can be:

A space character
A tab character
A carriage return character
A new line character
A vertical tab character
A form feed character

RouterOS do not support \S (neither \s)
[] > :put ("test" ~ "\\S") 
false
[] > :put ("test" ~ ".*\\S.*")
false
[] > :put ("" ~ ".*\\S.*")    
false
[] > :put ([:nothing] ~ ".*\\S.*")  
false
[] > :put (" " ~ ".*\\S.*")        
false
[] > :put ("x x" ~ ".*\\S.*")
false
[] > :put ([:find in=""] ~ ".*\\S.*")
false
Last edited by rextended on Thu Apr 06, 2023 10:07 pm, edited 1 time in total.
 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: hello i need help

Thu Apr 06, 2023 10:03 pm

Lol, rage again...
You really think that is a ChatGPT script?
I admit that I did not tested this regex with blanks on MT, but on https://regex101.com/ it is valid (and I think that is only mistake in script).
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 10:07 pm

Sorry, but when I see commands invented or not supported, it's a ChatGPT attitude...

I do not like the script, if you wait the time I write, I explain why.
 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: hello i need help

Thu Apr 06, 2023 10:15 pm

Sorry, but when I see commands invented or not supported, it's a ChatGPT attitude...

I do not like the script, if you wait the time I write, I explain why.
I have unessery adding usernames and passwors into arrays and iteration at the end, hotspot users can be added in main parsing loop where usernames and passwors are added into arrays...

P.S.
you are wrong about regex (at least for ROS 7.8 ), just tested it:
> :put ("test" ~ "\\S") 
true
> :put ("" ~ "\\S") 
false
Last edited by optio on Thu Apr 06, 2023 10:22 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 10:17 pm

(see next post)

EDIT: is useless back edit if you do not read next post first.....
Last edited by rextended on Thu Apr 06, 2023 10:57 pm, edited 3 times in total.
 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: hello i need help

Thu Apr 06, 2023 10:21 pm

Here is optimized version without additional arrays and loop.
Feel free to correct it :)
:local profile "hotspot"
:local fileName "user_credentials.txt"

:local fileCnt [/file get $fileName contents]
:local fileLen [:len $fileCnt]
:local lineLen 0
:local prevLineLen 0
:local lineCnt 0
:local line ""
:local notBlank ".*\\S.*"
:local uTokenEnd " &"
:local uTokenLen [:len "username= "]
:local pTokenLen [:len "passowrd= "]
:local sepLen [:len " & "]

:while ($lineLen < $fileLen) do={
  :set lineLen [:find $fileCnt "\n" $prevLineLen]
  :if ([:len $lineLen] = 0) do={ :set lineLen $fileLen }
  :set line [:pick $fileCnt $prevLineLen $lineLen]
  :set prevLineLen ($lineLen + 1)

  :if ($line != "\r") do={
    :local username [:pick $line $uTokenLen [:find $line $uTokenEnd]]

    if ($username ~ $notBlank) do= {
      :local password [:pick $line ($uTokenLen + [:len $username] + $sepLen + $pTokenLen) $lineLen]
      /ip/hotspot/user add name=$username password=$password profile=$profile
    }
  }
}

/file remove $fileName
Last edited by optio on Thu Apr 06, 2023 10:24 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 10:22 pm

Still present unsupported ".*\\S.*", so never works.

The "new line" method used is not initially checked.
It is almost certainly consistent throughout the file, it is quite unlikely that it is mixed.
So first check if there is "\r\n" in the file.
If there is, it's sure to be that, otherwise it's sure to be "\n".
It also seems pretty unlikely to me that it's just \r,
so at that point there's no need to recalculate it every single line.

The script doesn't check if the line actually contains "username="
and if indeed after the " &" there is password=, it's just based on the length.

The " &" value is encoded in the script (like the others) instead of in variables at the start.

No error check if on the username and password are present only valid characters (like 0x20-0x7E only)
because HotSpot / RouterOS do not support UTF-8 or other 8-bit codepages... and what is, for example "д" in one codepage, is "è" on another

The user have RouterOS v6 and your syntax is valid only on v7, but I have deduced this from the image, it is not said that you knew how to interpret.

& more.
 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: hello i need help

Thu Apr 06, 2023 10:35 pm

Still present unsupported ".*\\S.*", so never works.
Ok :), here is from my terminal:
> :put ("" ~ ".*\\S.*") 
false
> :put ("afds" ~ ".*\\S.*") 
true
> :put ("  " ~ ".*\\S.*") 
false
No error check if on the username and password are present only valid characters (like 0x20-0x7E only) because HotSpot / RouterOS do not support UTF-8 or other 8-bit codepages...
Ok I did not know that, but if you are mentioning pretty unlikely for "\r" I can say that in the file will pretty unlikely be characters for user/pass outside 0x20-0x7E range :)
& more.
Feel free to write your own, I was trying to help the guy and I think it is pretty much helpful vs ChatGPT like scripts...

PS.
I created parsing lines like that to support both \n and \r\n line endings in file to be more robust. Trimming usernames can be added and checking character set but dont't have more time to add that.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: hello i need help

Thu Apr 06, 2023 10:54 pm

Ok :), here is from my terminal:
> :put ("afds" ~ ".*\\S.*") 
true
I already know, as I told you it's not supported, the user is using RouterOS 6, not RouterOS 7.

No error check if on the username and password [...]
Ok I did not know that, but if you are mentioning pretty unlikely for "\r" I can say that in the file will pretty unlikely be characters for user/pass outside 0x20-0x7E range :)
Yes, near for sure, but is better check... Is not the first time that I have this problem if the app write the characters as UTF-8 instead of ASCII-7bit....
(instead "password" is writed "<NULL>p<NULL>a<NULL>s<NULL>s<NULL>w<NULL>o<NULL>r<NULL>d"....

& more.
Feel free to write your own, I was trying to help the guy and I think it is pretty much helpful vs ChatGPT like scripts...
For sure, but I must report what I see.

PS.
I created parsing lines like that to support both \n and \r\n line endings in file to be more robust.
Trimming usernames can be added and checking character set but dont't have more time to add that.
Is better to set on one variable or "\r\n" or only "\n" based if you [:find $xxx "\r\n"] is present or not....
Less calcs if the list is huge.....
 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: hello i need help

Thu Apr 06, 2023 11:29 pm

I already know, as I told you it's not supported, the user is using RouterOS 6, not RouterOS 7.
Can't find he is mentioning that or you somehow see that from winbox screenshot.
Is better to set on one variable or "\r\n" or only "\n" based if you [:find $xxx "\r\n"] is present or not....
Less calcs if the list is huge.....
I will leave it like that to cover both line endings robustly unless this script needs to executed in some infinite loop to be more optimized :)
Here my final script with username trim and small correction for proper handling \r at the end, checking for unsupported characters is still needed if someone is willing to do that and correcting regex if really needs to be executed on RouterOS 6:
:local profile "all"
:local fileName "user_credentials.txt"

:local fileCnt [/file get $fileName contents]
:local fileLen [:len $fileCnt]
:local lineLen 0
:local prevLineLen 0
:local lineCnt 0
:local line ""
:local notBlank ".*\\S.*"
:local uTokenEnd " &"
:local uTokenLen [:len "username= "]
:local pTokenLen [:len "passowrd= "]
:local sepLen [:len " & "]
:local nl "\n"
:local cr "\r"
:local space " "
:local empty ""

:while ($lineLen < $fileLen) do={
  :set lineLen [:find $fileCnt $nl $prevLineLen]
  :if ([:len $lineLen] = 0) do={ :set lineLen $fileLen }
  :set line [:pick $fileCnt $prevLineLen $lineLen]
  :set prevLineLen ($lineLen + 1)

  :if ($line != $cr) do={
    :local username [:pick $line $uTokenLen [:find $line $uTokenEnd]]

    if ($username ~ $notBlank) do={
      :local spPos [:find $username $space]
      if ($spPos > 0) do={ :set username [:pick $username 0 $space] }
      :local password [:pick $line ($uTokenLen + [:len $username] + $sepLen + $pTokenLen) $lineLen]
      :local retPos [:find $password $cr]
      if ($retPos > 0) do={ :set password [:pick $password 0 $retPos] }

      if ($password != $empty) do={
        /ip/hotspot/user add name=$username password=$password profile=$profile
      }
    }
  }
}

/file remove $fileName
 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

Re: hello i need help

Fri Apr 07, 2023 12:38 am

Thank you very much to both of you
optio - rextended
 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: hello i need help

Fri Apr 07, 2023 12:45 am

Thank you very much to both of you
optio - rextended
Np, you can check for invalid characters in user/pass in Telegram bot code and notify to user before adding into file.
Regarding regex in script, as rextended mentioned, if you have ROS 6 this needs correction or just adapt bot code so that cannot hapen and remove that check in script.
 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

Re: hello i need help

Fri Apr 07, 2023 1:04 am

Well, thank you very much, but I'm not very good at writing scripts and I don't speak English I use Google Translate and it doesn't support my language very well And the script you put for you I put but still did not work again thanks for taking your time
 
optio
Long time Member
Long time Member
Posts: 655
Joined: Mon Dec 26, 2022 2:57 pm

Re: hello i need help

Fri Apr 07, 2023 2:11 pm

Well, thank you very much, but I'm not very good at writing scripts and I don't speak English I use Google Translate and it doesn't support my language very well And the script you put for you I put but still did not work again thanks for taking your time
It's not working I guess because either you are using RouterOS 6.x (regex issue) or input file is not in exact format as in OP screenshot. I don't own ROS 6 device so I cannot properly test it if ROS version is the issue.
Good luck.
 
aramaa
just joined
Topic Author
Posts: 9
Joined: Thu Apr 06, 2023 4:06 am

Re: hello i need help

Fri Apr 07, 2023 8:31 pm

i understand thanks A lot optio

Who is online

Users browsing this forum: Amazon [Bot], cyrq and 20 guests