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
Is simpler that you alter the "program" that create that lines on the txt file...Thanks, but I want to script it as the picture I want the last line I specified
/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
It's not an easy thing, you need someone to do it from scratch and to follow you step by step...
: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
: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
[] > :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
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...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.
> :put ("test" ~ "\\S")
true
> :put ("" ~ "\\S")
false
: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
Ok , here is from my terminal:Still present unsupported ".*\\S.*", so never works.
> :put ("" ~ ".*\\S.*")
false
> :put ("afds" ~ ".*\\S.*")
true
> :put (" " ~ ".*\\S.*")
false
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 rangeNo 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...
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...& more.
I already know, as I told you it's not supported, the user is using RouterOS 6, not RouterOS 7.Ok , here is from my terminal:
Code: Select all> :put ("afds" ~ ".*\\S.*") true
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....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 rangeNo error check if on the username and password [...]
For sure, but I must report what I see.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...& more.
Is better to set on one variable or "\r\n" or only "\n" based if you [:find $xxx "\r\n"] is present or not....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.
Can't find he is mentioning that or you somehow see that from winbox screenshot.I already know, as I told you it's not supported, the user is using RouterOS 6, not RouterOS 7.
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 optimizedIs 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.....
: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
Np, you can check for invalid characters in user/pass in Telegram bot code and notify to user before adding into file.Thank you very much to both of you
optio - rextended
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.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