Skip to content

Instantly share code, notes, and snippets.

@gmastrokostas
Created November 25, 2015 09:29
Show Gist options
  • Select an option

  • Save gmastrokostas/89cf582f6ee761933e3e to your computer and use it in GitHub Desktop.

Select an option

Save gmastrokostas/89cf582f6ee761933e3e to your computer and use it in GitHub Desktop.
Find logged in black listed users
import psutil
#Generate a list of logged users.
users = psutil.users()
#list of banned users
banned_users = ['root','gmastrokostas', 'test', 'test1', 'test3']
#empty list to insert the logged users.
logged_users = []
#Will used to insert the results between the banned_users and logged_users.
#This will distinguish users who are in the banned list Vs those who are not.
banned_logged_users = []
#The loop goe through logged in users and inserts elements in the empty logged_users
for loop in users:
logged_users.append(loop.name)
#This checks for common elements (compares lists) between the two lists logged_users and banned_users.
#A list is mutable and thus we need to use "set"
#Look for info about set here https://docs.python.org/2/library/stdtypes.html#set.intersection
report = set(logged_users).intersection(banned_users)
print report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment