I have currently got some code that will do su via Expect.pm, but it is a little slow and hard to get the right output out of it. By hard I mean, Net::Telnet is much easier and by slow I mean it takes 9 seconds longer then just doing it by hand.
Is there a better way of doing this? I have to run a bunch of commands and really do not need a telnet server on the box (I have it setup to run on the loopback interface instead of the network), so I figured, hey why not switch it over to su...

any ideas as to how to boost the performence, or an easy way of getting only some of the output off of expect, or a different module to use for this?

This is what I've got, in a different program I need to read out several files, but not the commands to read them - expect provides both.
my $exp = Expect->spawn("su - $username") || die "Cannot spawn su: $!\ +n"; ####################################### $exp->debug(3); ####################################### $exp->raw_pty(1); my $spawn_ok; $exp->expect($timeout, [ 'Password: \$', sub { my $fh = shift; print $fh "$password\n"; $spawn_ok = "yes"; exp_continue; } ], [ eof => sub { if ($spawn_ok) { die "ERROR: premature EOF in login.\n"; } else { die "ERROR: could not spawn su.\n"; } } ], [ timeout => sub { die "Login process timed out\n"; } ], # ); #$exp->expect($timeout, # [ '-re', qr'[#>:] \$', sub { if($spawn_ok eq "yes") { if($on_off eq "on") { foreach $FILENAME (@list_of_files) { $exp->send("/bin/touch /home/$username/$FI +LENAME"); if ( $FILENAME eq ".forward") { #.forward must look like \username, + "|/usr/bin/vacation username" $set_fwd="\\$username, \"|/usr/bin/vac +ation $username\""; $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/$us +ername/$FILENAME"); } if($on_off eq "off") { foreach $FILENAME (@list_of_files) { $exp->send("/bin/rm -f /home/$username +/$FILENAME"); } } } } } # ], # [ # timeout => # sub { # die "Login process timed out\n"; # } # ], );

jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)

In reply to Faster way to do su by jcpunk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.