iamsudip userfinder v1 20130717-173559


Posted:

The assignment was to display all the users who can login. Recommended to use the python module pwd but instead of using the module I have done in it different way, output wise one can say both are same but I have opened /etc/passwd manually which is not recommended. I just wanted to post a differnt solution.

So, here I considered that login users means those users who have a home directory. For that we are checking if any home directory for any user present or not. The code snippet is given below.

Code

 1 #!usr/bin/env python
 2 
 3 from sys import exit
 4 
 5 def display_users():
 6     """
 7     Function to display those usernames who can login
 8     """
 9     #Opening the file /etc/passwd in read mode
10     f = open('/etc/passwd')
11     #Accessing the instance 'f' line by line
12     for i in f:
13         #Splitting the string
14         data = i.split(':')
15         #Finding the name of the home directories if present then print
16         if data[5].find('/root') != -1 or data[5].find('/home') != -1:
17             print data[0]
18     #Closing the file handle
19     f.close()
20 
21 if __name__ == '__main__':
22     display_users()
23     exit(0)

How to execute code

Run the above script like:

$ python userfinderv1.py

Example output

Here example output is given below:

sudip@sudip-mint userfinder $ (master) python userfinderv1.py
root
syslog
usbmux
saned
sudip
Contents © 2013 dgplug - Powered by Nikola
Share
UA-42392315-1