Python Forum
Query number of Logged in users?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query number of Logged in users?
#1
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?
Say NO to TROLLs!!  **naughty**
Reply
#2
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Good point. They are a mix of server 2003 (cringe) and the majority 2008.
Say NO to TROLLs!!  **naughty**
Reply
#4
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!
Say NO to TROLLs!!  **naughty**
Reply
#5
This sounds sort of like ...
If you'd like to press one do so now
If you'd like to press 2, fine
Reply
#6
(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?
Say NO to TROLLs!!  **naughty**
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Logged On User - Remote Machines mickrobinson567812 0 1,540 Apr-20-2020, 11:43 AM
Last Post: mickrobinson567812

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020