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

Re: Using Expect module in Net::Telnet

by msk_0984 (Friar)
on Jun 27, 2008 at 21:09 UTC ( [id://694452]=note: print w/replies, xml ) Need Help??


in reply to Using Expect module in Net::Telnet

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

Replies are listed 'Best First'.
Re^2: Using Expect module in Net::Telnet
by msk_0984 (Friar) on Jun 28, 2008 at 13:12 UTC
    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: note [id://694452]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found