Solved

piping input to passwd

Posted on 1998-09-17
2
1,064 Views
Last Modified: 2012-08-14
I'm using Redhat 5.1.
I want to set a user's password from a shell script without and human intervention.

I have tried this (and various others):

echo -e "foo123\nfoo123" | passwd foobar

But I get this:
New UNIX password: Retype new UNIX password:
New UNIX password:
New UNIX password:
passwd: Conversation error

Any ideas?

Many Thanks!

0
Comment
Question by:mag062397
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
marcelofr earned 50 total points
ID: 1638062
passwd command reads from the tty DIRECTLY, not from stdin so you need at least a pseudo tty. The following expect script may help you:

#!/usr/bin/expect
set oldpass [lindex $argv 0]
set newpass [lindex $argv 1]
spawn /usr/bin/passwd
expect word:
sleep 1
send $oldpass\r
expect word:
sleep 1
send $newpass\r
expect word:
sleep 1
send $newpass\r

Put this stuff in a script, chmod +x it and call it passing old and new passwords as parameters.

Good Luck
0
 

Author Comment

by:mag062397
ID: 1638063
Thank you I got it working.  The only thing I had to add was either a "sleep 1" or an "expect success" at the end of the script.  For some reason the script would end and I think the passwd program would think it was killed or something so it would not make the change.  By putting in one more expect string the script waits until passwd completes its task.  A sleep 1 also worked; however I'm going to stick with the expect success.

Thanks again.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question