Additionally, using various examples, we will go over the three key methods found in this module: pwd. getpwuid(), pwd.getpwnam(), and pwd. pwall(). To put it briefly, this tutorial will cover every aspect and technique found in the Python pwd package that you need to know in order to begin functioning on this module.
Example 1: Program of Using The pwd.getpwuid() Method
The password database record for the given user id can be obtained using Python’s pwd.getpwnam() function. The following is the syntax for this approach:
Syntax: pwd.getpwuid(user_id)
It takes the parameter user_id which is a number that represents the user id to which entering a password into the password database is necessary. This method gives back a tuple-like object of class “pwd.struct passwd” that reflects the supplied user password id’s database entry. Let’s take an example where we have implemented the pwd.getpwuid method.
user_id = 100
entry = pwd.getpwuid(user_id)
print("Database entry password for user id : % d:" % user_id)
print(entry)
user_id = 0
entry = pwd.getpwuid(user_id)
print("Database entry password for user id : % d:" % user_id)
print(entry)
Begin with the implementation of the code. First, we have imported the pwd module provided by Python. Then, we have set the variable which is represented as user_id. The given variable is initialized with the value of the number at the same time. It is the user id set for this script.
After that, we have created the variable as an entry where we have called the pwd.getpwuid() method to get the password of the database entry for the given user id. The entry is retrieved by the print function. We have also created another variable for user id to which we have assigned the value 0 and applied the pwd.getpwuid method on this user-id to get the password. We have also printed the password generated for this particular user id.
You can see the password generated for both the user ids as follows:
Example 2: Program of Using The pwd.getpwnam() Method
The password database record for the supplied user name can be obtained using Python’s pwd.getpwnam() function. The general syntax of this method is as follows:
Syntax: pwd.getpwnam(name)
It takes the name as an argument which is a string value that represents the username of the user to which a password database record is necessary. The method’s output is an object that resembles a tuple and belongs to the class “pwd.struct passwd,” which contains the password database record for the given name.
Let’s take a look at an example of the getpwnam() method to see how it functions now that we’ve already talked about it. Check out the Python program below:
name = "kalsoom"
entry = pwd.getpwnam(name)
print("Database entry Password for '% s':" % name)
print(entry)
name = "root"
entry = pwd.getpwnam(name)
print("\nDatabase entry Password for '% s':" % name)
print(entry)
Here, in the first step, we have included the pwd package of python. In the next step, we have defined the user name by creating the variable with the title name By initializing the user name field with a string value, we have set it. Then, we have set another variable ‘entry’ where we have invoked the method pwd.getpwnam() and passed the user name variable inside it as an argument. The password for the user name is provided by using the pwd.getpwnam() method.
With the print method, we have printed the entry fetched from the pwd.getpwnam() method. We have defined another user name which is set with the value ‘root’ inside the variable name. We have passed this user name inside the pwd.getpwnam method to get the password for this user name. Then, we have a print method to display the password entry retrieved.
The following entry password is generated for both the user name by using the pwd.getpwnam method. The console screen snapshot shows this:
Example 3: Program of Using The pwd.getpwall() Method
The list of all items saved in the password database can be obtained using the Python pwd.getpwall() method. It has the following syntax, which we utilize in the python script.
Syntax: pwd.getpwall()
Basically, this method doesn’t take any parameter value. This method provides a list of objects of the tuple-like pwd.struct passwd class whose elements are all of the entries that can be found in the password database. The getwall() method’s operation will now be demonstrated using an example. Check out the Python application below.
db_entries = pwd.getpwall()
print("Database entries password:")
for wall in db_entries:
print(wall)
After importing the pwd module into our program, we have declared a variable with the name db_entries which has the pwd.getpwall method. As discussed above, this pwd.getpwall method doesn’t take any parameters.
After that, we printed the retrieved password. For this, we have a for loop which cycled over each database entry’s password and was assigned to the variable wall. The variable wall here represents the rows. The print method takes this wall variable and prints the password list.
You can see the password list generated by the pwd.getpwall method as follows:
Conclusion
The password database can be accessed using the Python pwd package. This module gives us access to the database of user accounts and passwords. The entries in the database for passwords resemble type objects. The Python pwd module was covered in this tutorial. We gained knowledge of the various attributes that are offered in this module. In addition, we covered all three of the accessible methods in this module by using various examples. To sum up, the information in this article includes everything you need to know to begin utilizing the Python pwd module.