Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expect help

Status
Not open for further replies.

jewel464g

Technical User
Jul 18, 2001
198
US
I first asked this question in the perl forum and was told about expect. So I'm hoping someone here can help me better. I have a perl script that runs this command at the command line.

/sbin/passwd $user

where $user contains the user name, when this is run it prompts for the password, my problem is that since it doesn't return control to the program until after a password is typed in, I can't pass it the password. I was told in the perl forum that I could use expect, but I have no idea how to do that. Can anyone help? Please keep in mind that I have never used this before and didn't even know it existed until yesterday. Below is my script.
Thanks,
Jewel


#!/usr/bin/perl -w

use FileHandle;

open (FILE, "smusers.txt") || die "opening smusers.txt";

my @file = <FILE>;
for($index=0; $index<@file; $index++){
$str = $file[$index];
my ($user,$password) = $str =~ /^(.*?):.*:(.*?)$/;
`/usr/sbin/adduser $user -g pop -s /bin/false`;
`/sbin/passwd $user`;
`su - $user`;
`/var/qmail/bin/maildirmake $\HOME/Maildir`;
`exit`;
$SIG{CHLD} = 'IGNORE';
}

close (FILE); #close the old file
When faced with a decision, always ask, 'Which would be the most fun?'
 
Hi Jewel,

This is easy stuff for expect of course ;)..
I'm not a perl guy however so I'll have to guess what
you want..
Here is what I get.
You are trapping sigchld.
You are reading a plaintext file that contains the names
and passwords of users to add exec'ing adduser, exec'ing passwd, then su to the newuser and adding a maildir. Right so far?

I don't know how much of this you want in expect.
You could do it all, part or parts..


#!/usr/bin/expect

set timeout 3000
#don't wait forever on processes terminate after a while.
exp_internal 1
#debugging, comment out if you want.
log_user 0
#don't echo every thing.
set ctr 0
#counter var
#do your trap
trap {
send_user &quot;Caught signal [trap -name]\n&quot;
send_user &quot;Ignoring..\n&quot;
} SIGCHLD

#open file..save error in err_f
if {![catch {set fd [open &quot;smusers.txt&quot; r]} err_f]} {
#load file read into variable contents
set contents [read $fd]
}
close $fd
#close your file after array loaded

#start processing
if {[info exists contents]} {
foreach line [split $contents \n] {
#create vars for the parsed user and pass
regexp &quot;^(.*?):.*:(.*?)$&quot; $line user pass
#spawn passwd
spawn -noecho passwd [string trim $user]
set id $spawn_id

expect {
#look for passwd prompt
-re &quot;.*(P|p)assword.*&quot; {
#automate the process
send &quot;[string trim $pass]\r&quot;
#secondary prompt
expect -re &quot;.*enter new password.* &quot;
send &quot;[string trim $pass]\r&quot;
#passwd confirmation
expect -re &quot;.*assword.*ange.*&quot;
send_user &quot;All done for $user\n&quot;
#not really necessary with passwd
catch {close $id}
}
#end of passwd spawn, alert user
eof { send_user &quot;Caught passwd end for [incr ctr] iteration\n&quot;
send_user &quot;$expect_out(buffer)\n&quot;
}
} ;#end expect
} ;#end for loop
} else {
#errors.
error &quot;Could not process array: $err_f occurred.&quot;
} ;#endif
 
A few corrections and comments..
The regexp line will definitely have to be tweaked
depending on your file content and format, those are perl
regexps I notice now with the .*?, I don't know how tcl
will look at these...Also since tcl's regexp command
looks at all matches, first match in parens, second in
parens, etc.. the regexp should look like:
regexp &quot;(pat)patstuff(pat)&quot; $line all user pass

A suggestion:
Check for autoexpect on your machine and then run a sample
passwd change for a user and look at the output of the
autoexpect script to see how he interaction looks to expect.
> autoexpect passwd username

Expect will talk to you about what it sees and create a script for you, just like the *nix script command.

If you have any questions or need help write back.
Good Luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top