Python Forum
Query number of Logged in users? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Query number of Logged in users? (/thread-2041.html)



Query number of Logged in users? - S0undwav3 - Feb-13-2017

Hi,

I am trying to write an applet that scans a number of servers, and returns the number of logged in users per server.

Background : I have a cirtix environment, and we have instances that allow up to 30 users per machine. 

I have started to get somewhere with the "getpass" function.

>>> import getpass
>>> getpass.getuser()
'UserXYZ'

Basically what i intend to do is write an applet that counts how many users are logged in (by counting usernames possibly?)

How do I query a remote machine using python, to retrieve the list of logged in users?


RE: Query number of Logged in users? - sparkz_alot - Feb-13-2017

You don't specify if these are Linux, Windows, xBSD or a combination.  Once you've logged in, you could use Python's subprocess module and use the OS's tools to gather that information.  For instance, a quick search for "how can i find the number of users online in linux" resulted in this first entry http://superuser.com/questions/13043/how-can-i-find-the-number-of-users-online-in-linux


RE: Query number of Logged in users? - S0undwav3 - Feb-13-2017

Good point. They are a mix of server 2003 (cringe) and the majority 2008.


RE: Query number of Logged in users? - S0undwav3 - Feb-13-2017

OK got it.

SO when i got home i played around with this and am getting closer to what i want to do, but essentially, this question is answered. So here is the solution I came up with in case anyone is interested :

Windows CMD/Powerhshell has :

query user /server:<hostname>

which return all logged in users from the command line/powershell prompt.

So using this in conjuction with the rest goes something like this...

Dropping into a python box :

>>> import os
>>> os.system('query user /server:myservername')
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
john.doe ica-tcp#70 1 Active 7 13/02/2017 8:11

now all i need to do is put this into a count loop, maybe counting each line and dropping the first one.

Hope this helps someone else. Please feel free to post another solution. Always happy to learn!


RE: Query number of Logged in users? - Larz60+ - Feb-13-2017

This sounds sort of like ...
If you'd like to press one do so now
If you'd like to press 2, fine


RE: Query number of Logged in users? - S0undwav3 - Feb-15-2017

(Feb-13-2017, 10:16 PM)Larz60+ Wrote: This sounds sort of like ...
If you'd like to press one do so now
If you'd like to press 2, fine

Huh?