#!/usr/local/bin/perl -w use strict; use Expect; my $username = $ARGV[0]; my $password = $ARGV[1]; my $on_off = $ARGV[2]; my $message; my $set_fwd; my $timeout = 15; my $exp = new Expect; my $FILENAME; my @list_of_files; @list_of_files=(".forward", ".vacation.msg", ".vacation.pag", ".vacati +on.dir"); if($#ARGV > 2) { $message = join (" ",@ARGV); $message =~ s/$username//; $message =~ s/$password//; $message =~ s/$on_off//; } ####################################### print $username; print "\n"; print $password; print "\n"; print $on_off; print "\n"; print $message; print "\n#######################################\n"; ####################################### $exp->raw_pty(1); $exp->spawn("su - $username") or die "Cannot Begin Login Process\n";; $exp->expect($timeout, [ 'assword', sub { $exp->send("$password"); exp_continue; } ], [ '-re', qr'[#>:%] $', sub { if($on_off eq "on") { foreach $FILENAME (@list_of_files) { $exp->send("/bin/touch /home/$username/$FILENAME"); if ( $FILENAME eq ".forward") { #.forward must look like \username, "|/usr/bin/ +vacation username" $set_fwd="\\$username, \"|/usr/bin/vacation $usern +ame\""; $exp->send("/bin/echo $set_fwd > /home/$username/$ +FILENAME"); } if ( $FILENAME eq ".vacation.msg") { $message .= '\n'; $exp->send("/bin/echo $message > /home/$username/$ +FILENAME"); } $exp->send("/bin/chmod 644 /home/$username/$FILENAME" +); $exp->send("/bin/chown $username /home/$username/$FIL +ENAME"); } if($on_off eq "off") { foreach $FILENAME (@list_of_files) { $exp->send("/bin/rm -f /home/$username/$FILENAME"); } } exp_continue; } } ], [ timeout => sub { print "Process Timed Out.\n"; } ] );
In reply to Expect module & su by jcpunk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |