简体   繁体   中英

Linux passwd command : formatting the ouput

I have this script in which a user can change its password using passwd transparently. The script itself is executed by root, launching it with

su - <user> -c "script"

I know it might not be very safe a way to launch the script but that is how it is and I have no lattitude to change that part.

My problem is that when called, passwd displays the following:

Changing password for user <user>.
Changing password for <user>
current (UNIX) password:
New UNIX password:
Retype new UNIX password:

Several things to note here:

  1. Why does it even begin with two lines ? It seems the first is displayed when root calls passwd for and the second when calls passwd on himself. Can it be the start of an explanation ?
  2. I need to filter some words out of those prompts. I thought of using a combination of greps and seds piped one after the other but here is the trick: the two first lines seem to be outputed to stdout, but the others to stderr. When I try to redirect stderr to stdout to treat it, nothing gets displayed anymore.

Has anyone got any answer or tips regarding this situation ? Thanks a lot.

(First question here so do not hesitate to ask for more info.)

Try keying:

su - vartaghan -c passwd

onto the command line and then contrast that with keying:

passwd

onto the command line.

The answer is right there. Because you are using su to implement the command it requires the password to be keyed in and then the passwd command becomes active, which requires the password all over again.
Your best option would be to change the way that the menu which runs for your users, starts this password changing shell, by simply issuing the passwd command.

Edit: If you want to get rid of the I/O use something like:

(echo $1; echo $2; echo $2) | passwd &>/dev/null

Which requires that you run the script as myscript oldpassword newpassword

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM