0

I have a list of usernames which i need to sync their passwords over to a new server.

How would I go about extracting the username and password hash from the /etc/security/passwd file into the below format

username:MGURSj.F056Dj 

The passwd file is in stanza format

username: password = MGURSj.F056Dj lastupdate = 623078865 
1
  • 1
    That file and syntax hints at an AIX system; is that true? Commented May 5, 2020 at 13:01

2 Answers 2

3

You could parse the file with awk:

awk ' /^[[:alnum:]]*:/ { user=substr($0, 1, index($0, ":") - 1) } /^ *password *=/ { split($0, elements, " *= *"); print user ":" elements[2] }' \ /etc/security/passwd 

The idea behind the script is to first find the username line -- one starting with and containing alphanumeric characters, followed by a colon -- and extract that username with the "substr" function. On lines that start with zero or more spaces followed by the string "password", followed by zero or more spaces and an equals-sign, we split the line across the equals-sign and print the saved username with the password portion of the current line.

1

In case of an AIX system and the usernames are know, I would use another way like:

HASH=$(grep -p $USERNAME /etc/security/passwd | awk '/password/ {print $3}') echo "${USERNAME}:$HASH" 

Certainly the search for "password" could be coded even more strictly.

2
  • Welcome to the site, and thank you for your contribution. Please note however that thisseems to only print the password hash and doesn't seem to produce the desired output format username:hash. Commented Oct 9, 2024 at 14:16
  • maybe a $ missing between = and ( ? Commented Oct 14, 2024 at 7:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.