Script "nc-who" - similar to unix `who`, follow live who is logged in

  • Have you ever wanted to check who’s online right now, if there’s any suspicious movement happening on your cloud?

  • Similar to unix who, follow live who is logged in with

nc-who

I have completely reworked this script, which was originally created here

It has a completely revised output. Since it updates at an adjustable refresh rate with watch, the formatted output from mysql was no longer usable. The solution was to output as html and back with html2text and sed in a usable, nice and neat format.
Nextcloud All-in-One is supported, containerized solutions other than AIO are not (yet) supported

This tool is primarily intended for administrators of Nextcloud instances with many users. Of course, this is not intended for small servers used in familiary circles. But they can also want to take a look and then the tool is not far:

Restriction: only for MySQL/MariaDB Supported databases: MySQL/MariaDB, PostgreSQL

Read code here: https://global-social.net/script/nc-who

Install this way:

sudo wget -qO /usr/local/bin/nc-who https://global-social.net/script/nc-who
sudo chmod +x /usr/local/bin/nc-who
nc-who

Now simply run

nc-who monitor

or

nc-who last

much luck with it. :+1:


New in version:

  • 2023.06.28 05:13

    • PostgreSQL support
  • 2023.06.29 20:00

    • Auto-update with auto-backup: The script can now safely update itself. Backups are created that can be restored.
  • 2023.07.02 01:15

    • PostgreSQL output is now same as MySQL/MariaDB output.
    • Improvements and bug fixes.
  • 2023.08.11 16:00

    • Added realtime login status (online, offline or away) of user in monitor view
    • Added function ‘last_login’:
      • List users sorted by their last login time. This is equivalent to the output of
        occ user:lastseen --all
        Note: ‘--all’ is actually not available for ‘occ user:lastseen
  • 2023.08.12 23:25

    • Improved ‘last_login’ to also include users that never loged in.
4 Likes
  • Only mysql as database, pgsql is not yet supported by this script

What a pity …

@abyss02 try the newest version, I integrated PostgreSQL support for you.

1 Like

Well done!

I kinda like to move this thread to “howto”-category. What do you think about it, @ernolf ?

Aww and is there any chance. making this an app for appstore with kind of like a html (or whatever)-output for admins?

anyway… great work!

1 Like
  • :white_check_mark: done

Wooo, works great …

1 Like

Thank you for your kind feedback! I am happy that postgre - thanks to you - works now :wink:

I’ve increased the functionality of nc-who a bit again. Specialy the last_login function could be interesting for some admins but the realtime login status in monitor view as well.

Here the new help screen:

Help:

  nc-who - Version 2024-02-28 14:02 (latest version)

  This utility is similar to the Unix `who` command, allowing you to monitor
  live user logins and activities on your nextcloud server-instance.

  Usage:
    nc-who -h|--help
    nc-who monitor
    nc-who last
    nc-who integrity_check

  Options:

    -h|--help       Display this help message.

    monitor     Monitor live user logins, ordered by last activity at an
                adjustable refresh rate.
                * Login status is only available for cookie-based sessions
                  on a web frontend.
                * Last activity is shown for all clients.

    last        List users sorted by their last login time. This is equivalent
                to the output of `occ user:lastseen --all`
                * `--all` is actually not available for that occ command.

    integrity_check
                verify the integrity of this script with signature

Those of you who already have it installed (version 2023.06.29 or newer) only need to call the script and the update will be offered automatically.

Feedback welcome!

Continued luck

ernolf

Found a small bug in the current nc-who script.
If using nc dbpass with “$” sign, line 400 fails because “$” is used unescaped.

It must read:

eval '$DB_STRG' $DB_EXEC\"$db_query\"$

Thank you very much for your feedback!

Your Issue is verry good, I did realy not test it with passwords containing ‘$’

Could you please restart the script, it will ask to update and try if it now works.

ernolf

That was fast. It works now. Thanks.

1 Like

Unfortunately this breaks support for postgresql. I will have to develop a better solution. :wink:

Much luck,
ernolf

Now I hope it supports all kind of passwords for both, MySQL/MariaDB and PostgrSQL.

@atomic : does it still work?

ernolf

Hello @ernolf

Thanks for the link from this thread :arrow_down:

You doesn’t edit the bash script “minified”, right?
Is there a repository around with the code in a mode readable format?

What does it brings to “minify” a bash script? :thinking: is it obfuscation?

Thanks any way I’ll look at it. :+1:

Regards,
Sylvain.

Hi @Sylvain,

thank you for your interest,

I initially developed my scripts as monolithic standalone entities. However, given that many of the steps within most scripts remained consistent, I transitioned to modular scripting. This switch not only expedited development but also yielded significant enhancements applicable across my entire script collection.

Upon initiating the nc-who script, the necessary modules are loaded into the /usr/local/include/ernolf/prod/ directory. You’re welcome to explore the contents there.

Essentially, nc-who functions as a module loader with sequence control.

I am actually used to write my code “minified” this way because I can let it run better in my mind’s eye that way. I have consistently implemented this as an approach in the Module Loader, which I’ve optimized for compactness, ensuring minimal space consumption. The objective is to render it as “stable and solid as a rock,” given its foundational role in all of my scripts.
The loaded modules are more “human readable” formatted though :wink:

I host all my scripts on my personal server and have devised a custom build and commit framework. This framework handles all aspects of publication, including GPG signing, hashing, and versioning. I find platforms like Github or Codeberg too restrictive, as they lack the flexibility I require, I feel far too restricted in processes that I didn’t develop myself. :wink:
However, I’m happy to share the code if you specify the exact portion you’re interested in.

nc-who represents one of my simpler scripts. In addition to standard modules for color, text formatting, and interactive question/answer functionality etc., it relies on the “nextcloud” module for configuration data retrieval, the “nextcloud_db” module for interacting with the Nextcloud database, and the “nc-who/monitor” and “nc-who/last” modules, which execute the script’s tasks.


ernolf

Maybe this will help you:

nc-who monitor basically makes this database query that repeats itself at an adjustable time interval:

MySQL:

SELECT FROM_UNIXTIME(at.last_activity) AS last_activity,
                        CASE
                          WHEN at.type = '0' AND at.remember = '1' THEN (SELECT us.status FROM oc_user_status us WHERE us.user_id = at.uid)
                          ELSE 'n.a.'
                        END AS status, 
                        at.uid, at.name AS client
                      FROM oc_authtoken at WHERE at.type = '0' AND at.remember = '1'
                      ORDER BY at.last_activity DESC

PostgreSQL:

SELECT TO_TIMESTAMP(at.last_activity) AS last_activity,
                        CASE
                          WHEN at.type = '0' AND at.remember = '1' THEN (SELECT us.status FROM oc_user_status us WHERE us.user_id = at.uid)
                          ELSE 'n.a.'
                        END AS status, 
                        at.uid, at.name AS client
                      FROM oc_authtoken at WHERE at.type = '0' AND at.remember = '1'
                      ORDER BY at.last_activity DESC

All the rest of the code is to format it correctly on as many terminals as possible, including less wide TTYs, and to make it work out of the box on as many systems as possible.


Much and good luck,
ernolf

1 Like

Meanwhile there is:


ernolf

1 Like

Nextcloud All-in-One is now supported, containerized solutions other than AIO are not (yet) supported


ernolf

1 Like