Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Using Expect module in Net::Telnet

by msk_0984 (Friar)
on Jun 27, 2008 at 17:42 UTC ( [id://694402]=perlquestion: print w/replies, xml ) Need Help??

msk_0984 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Using Expect module in Net::Telnet
by msk_0984 (Friar) on Jun 27, 2008 at 21:09 UTC
    Heyy

    I could make get it work and could switch to the user with the help of expect and here is the code

    my $ssh = Net::SSH::Expect->new(host=>$ip, user=>$user, password=>$pass, raw_pty=>1 ) or die " Cannot O +pen "; $login_check=$ssh->login(); print "LOGIN for $ip -- $login_check \n"; my $who = $ssh->exec('who am i '); print "\n OUT1 $who \n"; my $ls = $ssh->exec('touch /tmp/YES_net '); print "\n OUT2 - $ls \n"; $ssh->exec("stty raw -echo"); $passwd = "sprintdream"; $tmp1 = $ssh->send("su root -c 'touch /tmp/YESIDIDIT' "); $tmp2 = $ssh->waitfor('Password:\s*\z', 3) or die "prompt 'pas +sword' not found after 1 second"; #$tmp3 = $ssh->send("su root -c whoami "); $tmp3 = $ssh->send("$passwd"); print " Output ==>1 $tmp1 2 $tmp2 3 $tmp3 \n"; #while ($chunk = $ssh->peek(1)) { # grabs chunks of output eac +h 1 second # #print $ssh->eat($chunk); # print " Chunk => $chunk \n"; #} while ( defined ($line = $ssh->read_line()) ) { print " Line => $line " . "\n"; }
    Thanks & Regards
    Sushil Kumar
      Hey Monks,

      Another thing where I have got stuck. Please advice me atleast in this case and need you sincere suggestion to move ahead and perform the tasks.

      As you must have seen the above logic to login using normal user and switch to different user and perform operations. I need to enhance this more by adding threads to the existing code, when did the same I am facing problems. I feel Net::SSH::Expect module is not supporting the threads facility, may because as we use exec()/send() function.

      foreach $line ( @info ) { next if ( $line =~ /^#/ or $line eq "" ); ($ip, $host, $user, $passwd, $suser, $spasswd ) = spli +t (/:/, $line ); print " INFO ==> $ip , $host, $user, $passwd, $suser, +$spasswd \n\n"; ### - Creation of SSH object here itself -- ### my $ssh = Net::SSH::Expect->new(host=>$ip, user=>$user +, password=>$passwd, raw_pty=>1 ) or die " Cannot Open "; $login_check=$ssh->login(); my $hs = $ssh->exec('hostname'); print " HOST of $host --> $hs \n"; #&nsecon($ip, $user, $passwd, $suser, $spasswd); ## &nsecon( $ssh ); push @threads, threads->create(\&nsecon, $ssh ); } while( my $thread = shift @threads ) { $thread->join(); print " Thread $counter exiting \n"; $counter++; } sub nsecon() { #($ip, $user, $passwd, $suser, $spasswd) = ( shift, shift,shif +t,shift,shift); $ssh = shift; #print "In Thread => $ip, $user, $passwd, $suser, $spasswd \n" +; ### -- Commented this part as threads were exiting -- ### ### So created the ssh object in the main thread and passing it to the + function ### ### my $ssh = Net::SSH::Expect->new(host=>$ip, #user=>$user, #password=>$passwd, #raw_pty=>1 ) or die " Cannot +Open "; ### ) or die " Cannot Open "; #$login_check=$ssh->login(); print "LOGIN for $ip -- $login_check \n"; my $who = $ssh->send('ls'); #print "\n OUT1 $who \n"; while ( defined ($line = $ssh->read_line()) ) { print " Line => $line " . "\n"; } my $ls = $ssh->send('who am i '); #print "\n OUT2 - $ls \n"; while ( defined ($line = $ssh->read_line()) ) { print " Line => $line " . "\n"; } }
      When I am executing this script only the first thread returns the output and all the next threads are connecting to the systems and not returning nor the commands are getting executed.

      Please help me out in this case.

      Thanks In Advance.

      Sushil Kumar
        I am trying to connect to my QNX m/c from the host PC, which haswindows xp.I want to kill two processes which run on my QNX PC from my host PC.For this purpose I am using a perl script which uses Net::Telnet....Although the perl script seems to be proper and according to the guidelines given by ppl below, my perl script is not entering the login name.It opens the command prompt and stops there.Which version of perl supports Net::Telnet .Please help me out...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://694402]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found