Exercise: Parsing /etc/passwd
¶
Requirement¶
Write a program getuser.py
that takes a filename and one or more
usernames from the commandline, looks them up in /etc/passwd
, and
prints out the user records one after the other.
$ python getuser.py /etc/passwd root jfasch
User: root
UID: 0
GID: 0
Home: /root
Shell: /bin/bash
User: jfasch
UID: 1000
GID: 1000
Home: /home/jfasch
Shell: /bin/bash
Implementation Strategy¶
The /etc/passwd
(download sample
) is the UNIX
user database. The file is line-based (an old UNIX tradition), and
each line contains a user record:
...
root:x:0:0:root:/root:/bin/bash
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
...
Each line/record consists of seven fields, separated by colons
(':'
), with the following meaning:
Field index |
Field name |
Description |
---|---|---|
0 |
|
User name |
1 |
|
Encrypted password; |
2 |
|
User ID |
3 |
|
(Primary) group ID |
4 |
|
Arbitrary descriptive text |
5 |
|
Home directory |
6 |
|
Login shell |
The program should read /etc/passwd
only once. Each line is a user
record, and should be broken up into a dictionary. For example, the
line
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
Would make it into a dictionary
{ 'uid': 1000,
'gid': 1000,
'home': '/home/jfasch',
'shell': '/bin/bash'
}
All lines/dictionaries/records would be stored in a large user database dictionary that would look like so,
userdatabase = {
"root": {
"uid": 0,
"gid": 0,
"home": "/root",
"shell": "/bin/bash",
},
"jfasch": {
"uid": 1000,
"gid": 1000,
"home": "/home/jfasch",
"shell": "/bin/bash",
},
...
}
It is that userdatabase
that is finally used to lookup the
remaining username arguments to the program.