Solved

Shell scripts

Posted on 1998-09-16
10
389 Views
Last Modified: 2010-04-22
How do you get a bash shell script to run commands such as passwd where the user is prompted for input?  I want to run the command all in one go, with the relevant inputs provided in the script itself.

Thanks in advance.
0
Comment
Question by:bsufs
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:JYoungman
ID: 1638041
You have to use expect(1) to do that.

0
 

Author Comment

by:bsufs
ID: 1638042
Could you tell me a bit more about Expect - what is it, how do I use it, etc.

Thanks very much.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1638043
man expect

;-)
0
 

Author Comment

by:bsufs
ID: 1638044
Is this the only way to do it, because we don't appear to have expect.

What I am really looking for is a way of changing a password from a script.  The passwd command prompts you for inputs so it is no good calling this from a script.
0
 
LVL 2

Expert Comment

by:JYoungman
ID: 1638045
Did you install the package?  It comes with most Linux distributions.

And yes, this is basically the only technique for doing it, unless you are root in which case you can just manipulate /etc/passwd directly.

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 1638046
What's exactly your problem calling passwd from within a script?
0
 
LVL 2

Expert Comment

by:richrussell
ID: 1638047
Expect is in /usr/bin.

Alternatively write a small C program to get your input along the lines of
"scanf "%s",&name; return name" - and use this in your script. Not very portable, but a quick fix anyway...
0
 

Author Comment

by:bsufs
ID: 1638048
I can manipulate /etc/passwd directly but this doesn't help when setting the password because it needs to be encrypted.  The idea was that I would use a script to insert a new entry directly in /etc/passwd and then change the password.

The problem with calling passwd from within a script is that it needs inputs from the user.  I want to call it from within a script and complete the whole process, i.e. change the password, using information from the script and not prompting for inpts.  Basically I just want to run a script telling it to change the password of user xyz to abc and let it run on its own without requiring extra input.
0
 
LVL 3

Expert Comment

by:marcelofr
ID: 1638049
Your problem is that you DO need a tty (or pseudo tty) because passwd command reads directly from it and not from stdin... I can send you a little sample if you want...

0
 
LVL 3

Accepted Solution

by:
marcelofr earned 200 total points
ID: 1638050
Here's my sample:

#!/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

You get the idea...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

757 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

17 Experts available now in Live!

Get 1:1 Help Now