Respected Monks,

I have a task in which we need to connect to remote system using a normal user and then switch to a different priviledged user to execute some priviledge commands. I am using Net::Telnet module to connect to remote systems and then have to use "su" to switch to the other user using Expect module.

But I am able to connect to the system using the normal user but as when we enter the command on the general terminal

# su -root -c 'command' Password:
It prompts for a password for that trying to use Expect module but I am unable to do it. I think am doing wrong here Here is a small script that I am trying to use
my $t = Net::Telnet->new( -host => $ip , -timeout => 60, -errmode => +"return"); do{ print qq{Failed open Telnet Session for '$host' }; exit 1; } unless ( $t ); $t->login( $user,$passwd ); do { print qq{Login Failed for '$host'} ; exit 1 } if ( $t->errmsg ); my $cmd1 = "\$OMNIHOME/bin/nco_ping NCO_PA "; my @out1 = $t->cmd(String => $cmd1, Timeout => 10 ); print " Out ==> @out1 \n"; my $exp = Expect->exp_init($t); $timeout = 3; $exp->expect($timeout, [ 'Password: $', sub { my $fh = shift; print $fh "$password\n"; exp_continue; } ], ); #my $cmd2 = "su root -c ls ";
I am unable to find any examples to do that and searched a lot but could not find any. Please guide me so that I can switch to the other user and use that telnet channel to execute priviledged commands and get the output.

Thanks In Advance

Sushil Kumar

In reply to Using Expect module in Net::Telnet by msk_0984

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.