Hi James, Thanks for the pointer to the Expect module.
I'll set it up and hope I get it working...else I'll come running back to you :p Can I establish an SSH connection and do something like Sshexec and Scp tasks do in ant 1.6.x using Expect ? Thanks -aman -----Original Message----- From: Kipp, James [mailto:[EMAIL PROTECTED] Sent: 17 February 2004 19:35 To: 'Thind, Aman'; Perl Beginners Subject: RE: How to su in a script if su needs a pswd ? > 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>