Skip to content

Instantly share code, notes, and snippets.

@jamesgarfield
Created June 7, 2013 18:49
Show Gist options
  • Save jamesgarfield/5731488 to your computer and use it in GitHub Desktop.
Save jamesgarfield/5731488 to your computer and use it in GitHub Desktop.
import subprocess
import os
class WindowsUser:
def __init__(self, name, session, state, idle):
self.name = name
self.session = session
self.state = state
self.idle = idle
def get_users_on_server(serverName):
"""Perform a "query user" command to the specified server and map the results to a list of WindowsUsers"""
filePath = serverName + 'userdump'
#Call the commandline program to list users on a server, dump the results to our file
with open(filePath, "w") as fileHandle:
statusCode = subprocess.call(['query', 'user', '/server:'+serverName], stdout=fileHandle)
#Read that same file back in to get the raw user data
with open(filePath, "r") as fileHandle:
rawData = fileHandle.readlines()
if os.path.isfile(filePath):
os.remove(filePath)
#No users is an empty file
if len(rawData) == 0: return []
#remove the header row
rawData.pop(0)
#Split each row on general whitespace
rows = [line.split() for line in rawData]
#each row is in the format: [0:username, 1:sessionname, 2:id, 3:state, 4:idleminutes, 5:logondate, 6:logontime, 7:logonAM/PM]
users = [WindowsUser(row[0], row[1], row[3], row[4]) for row in rows]
return users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment