Question: I have written a Python Code for Linux (Ubuntu) Can you comment on what is happening in the code I have provide some #, comment

I have written a Python Code for Linux (Ubuntu) Can you comment on what is happening in the code I have provide some "#", comment it like you are talking to a person that has basic python knowledge Also if you want can clean up my code a bit. (remember to tell "me" what's happening still")

#Project_1 #Provides information about OS users #Jan. 18/21

import re import pwd import grp import os import sys import socket

#

def printUserInfo(ID): print(namesList[ID], uIdList[ID], uGroupList[ID], uShell[ID]) def createLists(user): namesList.append(str(user.pw_name)) uIdList.append(str(user.pw_uid)) uGroupList.append(str(user.pw_gid)) uShellList.append(str(user.pw_shell))

# try: allUsers = pwd.getpwall()

except: print("Cannot get data!") sys.exit(1)

# namesList = [] uIdList=[] uGroupList=[] uShellList=[]

specList = ["processor", "vendor_id", "model", "model name", "cache size"] print() for user in allUsers:

# createLists(user)

#

maxLength = len(max(namesList,key=len)) maxLengthuID = len(max(uIdList,key=len)) maxLengthgID = len(max(uGroupList,key=len))

print(maxLength) print(maxLengthuID) print(maxLengthgID) print()

index = 0 for user in namesList: print(str(index) + '\t' + namesList[index] + ' '* (maxLength-len(namesList[index])+2) + uIdList[index] + ' '* (maxLengthuID-len(uIdList[index])+1) + uGroupList[index] + ' '* (maxLengthgID-len(uGroupList[index])+1) + uShellList[index]) index += 1

#Iterate through the user data for user in allUsers: createLists(user)

f = open("Project1.txt", "w")

os.system("service --status-all > Project1.txt")

#Possible extra information to print if you want #os.system("systemctl list-units --type service >> Project1.txt") #os.system("systemctl list-unit-files >> Project1.txt") #os.system("systemctl list-unit-files --type service >> Project1.txt")

#get machine name

f.write("Machine Name: "+socket.gethostname()+" ") f.write("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+" ")

#get cpu information

specList = ["processor", "vendor_id", "model", "model name", "cache size"]

# file = open ("/proc/cpuinfo", "r") lines = file.readlines()

# f = open("Project1.txt", "a") for spec in specList: for line in lines: if re.search(spec, line): f.writelines(line) break # for user in namesList: namesList = []

# gid = pwd.getpwnam(user).pw_gid userGroupName = grp.getgrgid(gid).gr_name

# currentUser = [user+":"] currentUser.append(userGroupName)

# for group in grp.getgrall(): if user in group.gr_mem: currentUser.append(group.gr_name) #

outputString = "" for item in currentUser: outputString = outputString + item outputString = outputString + ' ' f.write(str(outputString)) # index = 0

for entry in currentUser: if index == 0: f.write(entry+" ") else: print("\t"+entry) f.write("\t"+entry+" ") index +=1 # index = index + 1

f.close()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!