Skip to content
Related Articles

Related Articles

spwd module in Python

Improve Article
Save Article
  • Last Updated : 19 Jul, 2019
Improve Article
Save Article

spwd module in Python provides access to the Unix shadow password database. The entries stored in the database is tuple-like objects whose attributes are similar as the members of spwd structure defined in <shadow.h> header file.

Following are the attributes of tuple-like object which represents the entries stored in Unix shadow password database:

IndexAttributesMeaning
0sp_nampLogin name
1sp_pwdpEncrypted password
2sp_lstchgDate of last change
3sp_minMinimal number of days between changes
4sp_maxMaximum number of days between changes
5sp_warnNumber of days before password expires to warn user about it
6sp_inactNumber of days after password expires until account is disabled
7sp_expireNumber of days since 1970-01-01 when account expires
8sp_flagReserved

spwd module in Python defines following two methods:

  • spwd.getspnam() method
  • spwd.getspall() method

Note: spwd module is UNIX specific service. So, all methods of this module is available on UNIX versions only.

spwd.getspnam() method -

spwd.getspnam() method in Python is used to get the entry stored in Unix shadow password database for the specified user name.
This method requires the user to have enough privileges to access the shadow password database. PermissionError exception will be raised in case user do not have enough privileges.

Syntax: spwd.getspnam(name)

Parameter:
name: A string value representing the user name for which shadow password database entry is required.

Return type: This method returns a tuple-like object of class ‘spwd.struct_spwd’ which represents the shadow password database entry associated with the specified user name.

Code: Use of spwd.getspnam() method




# Python program to explain spwd.getspnam() method
    
# importing spwd module 
import spwd
  
# User name
name = "ihritik"
  
# Get the shadow password
# database entry for the
# specified user name
# using spwd.getspnam() method
entry = spwd.getspnam(name)
  
# Print the retrieved entry
print("Shadow password database entry for the user name '%s':" %name)
print(entry)
  
# User name
name = "root"
  
# Get the shadow password
# database entry for the
# specified user name
# using spwd.getspnam() method
entry = spwd.getspnam(name)
  
# Print the retrieved entry
print("\nShadow password database entry for the user name '%s':" %name)
print(entry)

Output:
os.getspnam() method output

spwd.getspall() method -

spwd.getspall() method in Python is used to get the all available entries stored in shadow password database. This method also requires the user to have enough privileges to access the shadow password database.

Syntax: spwd.getspall()

Parameter: No parameter is required.

Return type: This method returns a list of tuple-like object of class ‘spwd.struct_spwd’ whose elements represent the shadow password database entries.

Code: Use of spwd.getspall() method




# Python program to explain spwd.getspall() method
    
# importing spwd module 
import spwd
  
# Get the all available 
# shadow password database entries
# using spwd.getspall() method
entries = spwd.getspall()
  
  
# Print the retrieved entries
print("Shadow password database entries:")
for row in entries:
    print(row)

Output:

Shadow password database entries:
spwd.struct_spwd(sp_namp=’root’, sp_pwdp=’!’, sp_lstchg=17677, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’daemon’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’bin’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’sys’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’sync’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’games’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’man’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’lp’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’mail’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’news’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’uucp’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’proxy’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’www-data’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’backup’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’list’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’irc’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’gnats’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’nobody’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’systemd-timesync’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’systemd-network’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’systemd-resolve’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’systemd-bus-proxy’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’syslog’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’messagebus’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’_apt’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’uuidd’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’rtkit’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’avahi-autoipd’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’usbmux’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’dnsmasq’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’whoopsie’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’kernoops’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’speech-dispatcher’, sp_pwdp=’!’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’avahi’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’saned’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’pulse’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’colord’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’hplip’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’geoclue’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’gdm’, sp_pwdp=’*’, sp_lstchg=17536, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’ihritik’, sp_pwdp=’$6$xOnnKlQr$yiKkE4XS1zT9mUj9NARHUTLZ8ibWurTx4pjQYOC58YHGGGBWTI3GIFKU4RUWC99JmPZIsfSlSvtm1GAyJL1G41′, sp_lstchg=17677, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’sshd’, sp_pwdp=’*’, sp_lstchg=17692, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
spwd.struct_spwd(sp_namp=’master’, sp_pwdp=’$6$oMUtyit0$zCrJ3K5XEWEhqclJW.rsL9SP3zWHkLhT8rQ75fhJ4P6zZlhF3aOfymQT8A/VRqkJrutWCuWXvaekMUP/IIgFG1′, sp_lstchg=17972, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!