Solved

piping input to passwd

Posted on 1998-09-17
2
1,027 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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now