> Hello, > > I am running a command as part of my script on a Unix box for > which I need > to su to a different username. > > This su requires a password as well. > > I could not find any option of passing the password in su. > > Tried the shell script solution of here docs : > > su <<TillEnd > admin > test > TillEnd >
Take a look at the Expect module. Here is some code from a script i did a long time ago. It was a wrapper script for su. It may help: -- use strict; use Expect; use IO:Socket; # grab the user if entered my $user = $ARGV[0] if $ARGV[0]; my $pw; # might fail if it's su - if ($user && $user !~ /root/) { real_su(); } # &real_su unless $user =~ /root/; print "Password: "; $pw = <STDIN>; chomp $pw; # send the captured password to be cyphered cypher($pw); # tunnel($string); # now that we cypher and sent the pw to us go ahead and run the &real_su(); sub real_su { my $su = Expect->spawn('/bin/su',$user) or die "Can't start Program: $!\n"; # stop the output from going to STDOUT $su->log_stdout(0); # capture pw and die with appropriate string if pw is wrong my $error = ($su->expect(10, 'password:'))[1]; if ($error) { print $su "Sorry.\r"; print STDOUT "Sorry.\r"; $su->soft_close(); } print $su "$pw\r"; $su->soft_close(); exit 0; } ....... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>