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.
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)
Run the above script like:
$ python userfinderv1.py
Here example output is given below:
sudip@sudip-mint userfinder $ (master) python userfinderv1.py root syslog usbmux saned sudip
This program will read /etc/passwd info and output user who can do aproper login.
$ python user_finder.py
A link to the source code.
['root', 'm0rin09ma3']
In the main function, use regular expression to filter out users who has /bin/false or /sbin/nologin default shell. Then output the rest of users.
all_user = pwd.getpwall() #print all_user notvalid = re.compile('.*/(nologin|false)') daemon = re.compile('.*daemon', re.IGNORECASE) for user in all_user: if not (notvalid.match(user.pw_shell) or \ daemon.match(user.pw_gecos)): print user.pw_name, #print [user.pw_name for user in all_user if not notvalid.match(user.pw_ shell)] #print [user.pw_name for user in all_user if not daemon.match(user.pw_ge cos)]
improvement to user finder
#!/usr/bin/env python import os import pwd import grp for p in pwd.getpwall(): if p[3] >= 1000: if p[3] <= 2000: print grp.getgrgid(p[3])[0]
This program will read /etc/passwd info and output user who can do aproper login.
$ python user_finder.py
A link to the source code.
['root', 'm0rin09ma3']
In the main function, use regular expression to filter out users who has /bin/false or /sbin/nologin default shell. Then output the rest of users.
all_user = pwd.getpwall() #print all_user notvalid = re.compile('.*/(nologin|false)') daemon = re.compile('.*daemon', re.IGNORECASE) print [user.pw_name for user in all_user if \ (not notvalid.match(user.pw_shell)) and \ (not daemon.match(user.pw_gecos))] #print [user.pw_name for user in all_user if not notvalid.match(user.pw_ shell)] #print [user.pw_name for user in all_user if not daemon.match(user.pw_ge cos)]
Write a code that will print the list of available user to log in.
Use of pwd module and choose the user that have proper right to log in, which are those having home directory and root. We achieve this with pwd module, and its function getpwall(), which returns a list of all users passwd database, which contains several informations, as name, password, and else. As all users can't be used to login, not all user returned by pwd.getpwall() are valid. We have to filter them. The only ones which we want are those which have it's own directory in /home, and root, which has its own home in /root. Thus we can filter them by finding wether their home directory is in /home or /root, by searching in each user database the information about the home dir, which is in the index 5.
#!/usr/bin/env python from pwd import getpwall from sys import exit def get_users_list (): """ This function prints the list of users that are able to login into the system. To achieve this, we use the pwd module and its function getpwall(), which returns a list of each user database. Then we filter it to print just the users that can login into the system. """ user_list = getpwall() for x in range(len(user_list)): if user_list[x][5].find("home") != 0: print user_list[x][0] elif user_list[x][5].find("root") != 0: print user_list[x][0] if __name__ == "__main__": get_users_list() exit(0)
This program will read /etc/passwd info and output user who can do aproper login.
$ python user_finder.py
A link to the source code.
['root', 'm0rin09ma3', 'git']
In the main function, use regular expression to filter out users who has /bin/false or /sbin/nologin default shell. Then output the rest of users.
all_user = pwd.getpwall() #print all_user notvalid = re.compile('.*/(nologin|false)') print [user.pw_name for user in all_user if not notvalid.match(user.pw_s hell)]
In this assignment, we write a program which will return a list of all users present in the system who can do a proper login.
The code for the above problem can be found here
Here is the solution to the problem
!/usr/bin/env python import pwd total_list = pwd.getpwall() # a list of all password database entries for i in total_list: # iterating throgh the list if i[5].find("home") == 1: # if 'home' is present in the user home directory print i[0] # print the user name if i[5].find("root") == 1: # if 'root' is present in the user home directory print i[0] # print the user name
We shall save the above code as "user finder.py". To run the script, follow the steps.
Change the file's permissions and make it executable:
$ chmod +x user\ finder.py
Execute the file:
$ ./user\ finder.py
Alternatively, try:
$ python user\ finder.py
#!/usr/bin/env python import pwd import os import pwd, grp print os.getenv("USER") print os.getenv("SUDO_USER") for p in pwd.getpwall(): print p[0], grp.getgrgid(p[3])[0]
Write a code that will print the list of available user to log in.
Use of pwd module and choose the user that have proper right to log in, which are those having home directory and root.
#!/usr/bin/env python import pwd import sys def get_users_list (): user_list = pwd.getpwall() for x in range(len(user_list)): if user_list[x][5].find("home") >= 0: print user_list[x][0] elif user_list[x][5].find("root") >= 0: print user_list[x][0] if __name__ == "__main__": get_users_list() sys.exit()
This script will parse Planet Fedora and output the information from the RSS page in a human readable way to the terminal. You can find the script at the following link.
#!/usr/bin/env python """ This script will parse the RSS feed from Planet Fedora and will print out the Authors and the Post Titles Module dependencies: 1. feedparser """ from sys import exit import re import feedparser def parse_feed(feed_): """ The following function will parse the feed and return a list of tuples, Author Post, to be used later. """ author = range(len(feed_.entries)) post = range(len(feed_.entries)) for i in range(len(feed_.entries)): author[i] = re.findall("(.*):\s.*", feed_.entries[i].title) post[i] = re.findall(".*:\s(.*)", feed_.entries[i].title) author = [item for sublist in author for item in sublist] post = [item for sublist in post for item in sublist] parsed_info = zip(author, post) return parsed_info def print_feed(parsed_info): """ This function will print out information from a list of tuples """ print "RSS Planet Parser" for author, post in parsed_info: print "Author: %s\nPost: %s\n" % (author, post) if __name__ == '__main__': """ The main function will try to connect to the xml RSS link. If connecting fails, the script will exit with an error 1. Otherwise, it will read the link and send it to be parsed and then printed out. """ try: feed_ = feedparser.parse("http://planet.fedoraproject.org/rss20.xml") except IOERROR: print "Could not connect to server" exit(1) else: parsed_info = parse_feed(feed_) print_feed(parsed_info) exit(0)