Community discussions

 
mcedeno
just joined
Topic Author
Posts: 4
Joined: Tue Oct 20, 2015 12:38 pm

Logged Users File Creation Script

Thu Sep 05, 2019 4:23 pm

#########################################################################################################
# User Manager Logged User List
#########################################################################################################
# Author: Meneleo Cedeno, Cebu, Philippines
# email: mcedeno.gm@gmail.com
# Username in MikroTik Forum is mcedeno
#########################################################################################################
# Features # #
# - List logged users in multiple files to avoid the 4095 length limit
# - Just change the variable 'entriesPerFile' to adjust the number of sessions per file
# - Logged user may be deleted by adding the command -> /tool user-manager user remove $i
# - checks if file with the same name is existing in the router and increments the index
#########################################################################################################

:local entriesPerFile 20
:local thisDate
:local thisYear
:local thisDay
:local thisMonth
:local fname ""
:local Header ""
:local fSearch ""
:local fQty 0
:local point

:local counter 0
:local lastSeen
:local dateUsed
:local timeUsed
:local user
:local pword
:local profile
:local download
:local upload
:local total
:local uptime
:local qtyExUser
:local oldContent ""
:local newContent ""
:local lenContent
:local lenHeader
:local entryCount 0
:local fileIndex 0
:local maxIndex 0
:local fIndex 0
:local serialNo

:set qtyExUser [/tool user-manager user print count-only where last-seen!="never"]
:local fileCount ($qtyExUser / $entriesPerFile)

:if ( ($fileCount * $entriesPerFile) != $qtyExUser) do={
:set fileCount ($fileCount + 1);
}

:put ("Total number of files required : " . $fileCount)

# Prepare date suffixes for the filename

:set thisDate [/system clock get date]
:set thisYear [:pick $thisDate 7 11]
:set thisDay [:pick $thisDate 4 6]
:set thisMonth [:pick $thisDate 0 3]
:put ("Today is " . $thisDate)
:put "\r\n"

:if ($thisMonth="jan") do { :set thisMonth 1}
:if ($thisMonth="feb") do { :set thisMonth 2}
:if ($thisMonth="mar") do { :set thisMonth 3}
:if ($thisMonth="apr") do { :set thisMonth 4}
:if ($thisMonth="may") do { :set thisMonth 5}
:if ($thisMonth="jun") do { :set thisMonth 6}
:if ($thisMonth="jul") do { :set thisMonth 7}
:if ($thisMonth="aug") do { :set thisMonth 8}
:if ($thisMonth="sep") do { :set thisMonth 9}
:if ($thisMonth="oct") do { :set thisMonth 10}
:if ($thisMonth="nov") do { :set thisMonth 11}
:if ($thisMonth="dec") do { :set thisMonth 12}

:set fSearch ("ExUsers_" . $thisMonth . $thisDay. [:pick $thisYear 2 4])

:set fQty (:tonum [/file print count-only where name~$fSearch])
:put ("Number of files with the same suffix : " . $fQty)

:foreach i in=[/file find where name~$fSearch] do {
:set fname [/file get $i name]
:set point [:find $fname "." 0]
:set fIndex [:pick $fname ($point-1) $point]
:if ($fIndex>$maxIndex) do {:set maxIndex $fIndex}
}
:put ("Maximum file index is : " . $maxIndex)



:for i from=1 to=$fileCount do={
#create the files
:set fname ("ExUsers_" . $thisMonth . $thisDay. [:pick $thisYear 2 4]. "_" . ($i+$maxIndex) . ".txt")
:put $fname
/file print file=$fname
:delay 2
/file set $fname contents=""

:set serialNo [/system routerboard get serial-number]

:set Header ("Ezi Wifi Kiosk Expired User List " )
:set Header ($Header . "\r\n" . "Date Printed : " . $thisDate)
:set Header ($Header . "\r\n" . "Serial No / Kiosk ID: " . $serialNo )
:set Header ($Header . "\r\n" . "Page " . $i . " of " . $fileCount )
:set Header ($Header . "\r\n" . "User, Password, Date, Time, Uptime, Download, Upload" )
/file set $fname contents=$Header
}

:set fname ""

:foreach i in=[/tool user-manager user find where last-seen!="never"] do {
:set user [/tool user-manager user get $i username]
:set pword [/tool user-manager user get $i password]
:set lastSeen [/tool user-manager user get $i last-seen]
:set dateUsed [:pick $lastSeen 0 6]
:set timeUsed [:pick $lastSeen 12 17]
:set profile [/tool user-manager user get $i actual-profile]
:set download (:tonum[/tool user-manager user get $i download-used])
:set upload (:tonum[/tool user-manager user get $i upload-used])
:set uptime [/tool user-manager user get $i uptime-used]
:set uptime [:pick $uptime 0 5]
:set total (download + upload)
:put ($user . " last login" . " - ". $lastSeen)
:put ($user . " password" . " - ". $pword)
:put ($user . " profile" . " - ". $profile)
:put ($user . " download" . " - ". $download)
:put ($user . " upload" . " - ". $upload)
:put ($user . " total" . " - ". $total)
:put ($user . " uptime" . " - ". $uptime)
:put "************************"
:put "\r\n"
:set entryCount ($entryCount+1)

:set newContent ($newContent . "\r\n" . $user . ", " . $pword . ", " . $dateUsed . ", " . $timeUsed . ", " . $uptime . ", " . $download . ", " . $upload)

# Check length of the content
:set lenContent [:len $newContent]
:put ("Length of the contents is " . $lenContent)
:put ("Entry count is : " . $entryCount)
:put "\r\n"

# Start writing the file
:if ($entryCount=$entriesPerFile || ($counter+1)=$qtyExUser) do {
:set fileIndex ($fileIndex+1)
:put ("File Index is " . $fileIndex)

# Get contents of filename with index
:set fname ("ExUsers_" . $thisMonth . $thisDay. [:pick $thisYear 2 4]. "_" . ($fileIndex+$maxIndex) . ".txt")
:set oldContent [/file get $fname contents]
:put $oldContent
/file set $fname contents=($oldContent . $newContent)
:set entryCount 0
:set oldContent ""
:set newContent ""
}

:set counter ($counter+1)

}

:put ("Qty of logged-in users that may be deleted : " . $counter)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1302
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Logged Users File Creation Script

Fri Sep 06, 2019 8:06 am

Use Code tags on your code. Mark test an click </> above your post.
Also post with tabs to make script more readable.
And write some more what this is, whey to use it and when.

Here is a repost with code tags and tab.
#########################################################################################################
# User Manager Logged User List
#########################################################################################################
# Author: Meneleo Cedeno, Cebu, Philippines
# email: mcedeno.gm@gmail.com
# Username in MikroTik Forum is mcedeno
#########################################################################################################
# Features # #
# - List logged users in multiple files to avoid the 4095 length limit
# - Just change the variable 'entriesPerFile' to adjust the number of sessions per file
# - Logged user may be deleted by adding the command -> /tool user-manager user remove $i
# - checks if file with the same name is existing in the router and increments the index
#########################################################################################################

:local entriesPerFile 20
:local thisDate
:local thisYear
:local thisDay
:local thisMonth 
:local fname ""
:local Header ""
:local fSearch ""
:local fQty 0
:local point

:local counter 0
:local lastSeen
:local dateUsed
:local timeUsed
:local user
:local pword
:local profile
:local download
:local upload
:local total
:local uptime
:local qtyExUser
:local oldContent ""
:local newContent ""
:local lenContent
:local lenHeader
:local entryCount 0
:local fileIndex 0
:local maxIndex 0
:local fIndex 0
:local serialNo

:set qtyExUser [/tool user-manager user print count-only where last-seen!="never"]
:local fileCount ($qtyExUser / $entriesPerFile)

:if ( ($fileCount * $entriesPerFile) != $qtyExUser) do={
  :set fileCount ($fileCount + 1);
}

:put ("Total number of files required : " . $fileCount)

# Prepare date suffixes for the filename

:set thisDate [/system clock get date]
:set thisYear [:pick $thisDate 7 11]
:set thisDay [:pick $thisDate 4 6]
:set thisMonth [:pick $thisDate 0 3]
:put ("Today is " . $thisDate)
:put "\r\n"

:if ($thisMonth="jan") do { :set thisMonth 1}
:if ($thisMonth="feb") do { :set thisMonth 2}
:if ($thisMonth="mar") do { :set thisMonth 3}
:if ($thisMonth="apr") do { :set thisMonth 4}
:if ($thisMonth="may") do { :set thisMonth 5}
:if ($thisMonth="jun") do { :set thisMonth 6}
:if ($thisMonth="jul") do { :set thisMonth 7}
:if ($thisMonth="aug") do { :set thisMonth 8}
:if ($thisMonth="sep") do { :set thisMonth 9}
:if ($thisMonth="oct") do { :set thisMonth 10}
:if ($thisMonth="nov") do { :set thisMonth 11}
:if ($thisMonth="dec") do { :set thisMonth 12}

:set fSearch ("ExUsers_" . $thisMonth . $thisDay. [:pick $thisYear 2 4])

:set fQty (:tonum [/file print count-only where name~$fSearch])
:put ("Number of files with the same suffix : " . $fQty)

:foreach i in=[/file find where name~$fSearch] do {
  :set fname [/file get $i name]
  :set point [:find $fname "." 0]
  :set fIndex [:pick $fname ($point-1) $point]
  :if ($fIndex>$maxIndex) do {:set maxIndex $fIndex}
}
:put ("Maximum file index is : " . $maxIndex)



:for i from=1 to=$fileCount do={
  #create the files
  :set fname ("ExUsers_" . $thisMonth . $thisDay. [:pick $thisYear 2 4]. "_" . ($i+$maxIndex) . ".txt")
  :put $fname
  /file print file=$fname
  :delay 2
  /file set $fname contents=""

  :set serialNo [/system routerboard get serial-number]

  :set Header ("Ezi Wifi Kiosk Expired User List " )
  :set Header ($Header . "\r\n" . "Date Printed : " . $thisDate)
  :set Header ($Header . "\r\n" . "Serial No / Kiosk ID: " . $serialNo )
  :set Header ($Header . "\r\n" . "Page " . $i . " of " . $fileCount )
  :set Header ($Header . "\r\n" . "User, Password, Date, Time, Uptime, Download, Upload" )
  /file set $fname contents=$Header
}

:set fname ""

:foreach i in=[/tool user-manager user find where last-seen!="never"] do {
  :set user [/tool user-manager user get $i username]
  :set pword [/tool user-manager user get $i password]
  :set lastSeen [/tool user-manager user get $i last-seen]
  :set dateUsed [:pick $lastSeen 0 6]
  :set timeUsed [:pick $lastSeen 12 17]
  :set profile [/tool user-manager user get $i actual-profile]
  :set download (:tonum[/tool user-manager user get $i download-used])
  :set upload (:tonum[/tool user-manager user get $i upload-used])
  :set uptime [/tool user-manager user get $i uptime-used]
  :set uptime [:pick $uptime 0 5]
  :set total (download + upload)
  :put ($user . " last login" . " - ". $lastSeen)
  :put ($user . " password" . " - ". $pword)
  :put ($user . " profile" . " - ". $profile)
  :put ($user . " download" . " - ". $download)
  :put ($user . " upload" . " - ". $upload)
  :put ($user . " total" . " - ". $total)
  :put ($user . " uptime" . " - ". $uptime)
  :put "************************"
  :put "\r\n"
  :set entryCount ($entryCount+1)	

  :set newContent ($newContent . "\r\n" . $user . ", " . $pword . ", " . $dateUsed . ", " . $timeUsed . ", " . $uptime . ", " . $download . ", " . $upload)

  # Check length of the content 
  :set lenContent [:len $newContent]
  :put ("Length of the contents is " . $lenContent)
  :put ("Entry count is : " . $entryCount)
  :put "\r\n"

  # Start writing the file	
  :if ($entryCount=$entriesPerFile || ($counter+1)=$qtyExUser) do {
    :set fileIndex ($fileIndex+1)
    :put ("File Index is " . $fileIndex)

    # Get contents of filename with index
    :set fname ("ExUsers_" . $thisMonth . $thisDay. [:pick $thisYear 2 4]. "_" . ($fileIndex+$maxIndex) . ".txt")
    :set oldContent [/file get $fname contents]
    :put $oldContent
    /file set $fname contents=($oldContent . $newContent)
    :set entryCount 0
    :set oldContent ""
    :set newContent ""
  }

  :set counter ($counter+1)

}

:put ("Qty of logged-in users that may be deleted : " . $counter)
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
mcedeno
just joined
Topic Author
Posts: 4
Joined: Tue Oct 20, 2015 12:38 pm

Re: Logged Users File Creation Script

Mon Sep 09, 2019 5:11 am

Thanks! I have been searching the forum on how to do that </> method.

Who is online

Users browsing this forum: No registered users and 13 guests