Python Forum

Full Version: Query number of Logged in users?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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...e-in-linux
Good point. They are a mix of server 2003 (cringe) and the majority 2008.
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!
This sounds sort of like ...
If you'd like to press one do so now
If you'd like to press 2, fine
(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?