hi, The output from my Perl-Expect-SSH program is all jumbled and prints characters in random manner.. Is there any terminal/stty settings I need to set ? Thanks so much for help..

Why does my program print output like this.. ----------- $ ./new.pl LOGIN : PASS COMMAND= grep -i 00:25:B5:11:11:12 /var/log/messages | grep DHCPACK | +awk {'print $8'} |uniq OUTPUT = 50.6.0.135 IP = 50.6.0.135 | awk '{print $2}' 50.6.0.135 1 (50.6.0.11) 2999.637 ms !H 2999.639 ms !H 2999.635 ms !Hs max +, 40 byte packets Trace route : FAIL ----------- Whereas, I was expecting output like this.. $ ./new.pl LOGIN : PASS COMMAND= grep -i 00:25:B5:11:11:12 /var/log/messages | grep DHCPACK | +awk {'print $8'} |uniq OUTPUT = 50.6.0.135 IP = 50.6.0.135 TRACEROUTE OUTPUT = 1 (50.6.0.11) 2999.637 ms !H 2999.639 ms !H + 2999.635 ms !Hs max, 40 byte packets Trace route : PASS ------------ Here is my code.. ---------------------------------------- #!/usr/bin/perl use strict; use Expect; # globals my $hostname ='50.10.0.11'; my $username ='root'; my $password ="mypassword"; my $timeout = 15; my $prompt = '.*@.*\s+.*'; my $mac = "00:25:B5:11:11:12"; find_dhcpip($mac); sub find_dhcpip { my $mac = shift; my ($exp,$dhcpip);; my ($cmd,@output); # login return 0 if(! ($exp = login22($hostname,$username,$password))); # run commands $cmd = "grep -i $mac /var/log/messages | grep DHCPACK | awk {'print +\$8'} |uniq"; @output = executeCmd($exp,$cmd); print("COMMAND= $cmd\n"); print("OUTPUT = @output\n"); $dhcpip = $output[0]; print("IP = $dhcpip\n"); # check output of 'traceroute' my @tr_output; $cmd = "traceroute $dhcpip | awk '{print \$2}'"; print("COMMAND= $cmd\n"); @tr_output = executeCmd($exp,$cmd); print "TRACEROUTE OUTPUT = @tr_output\n"; if(grep(/$dhcpip/,@tr_output)) { print "Trace route Success.. DHCP-IP = $dhcpip\n"; } else { print "Trace route : FAIL\n"; } # logout return 0 if(! logout22($exp)); } sub login22 { my ($hostname,$username,$password) = @_; my ($cmd,$output); my $exp; # get an Expect object $exp = new Expect; $exp->slave->clone_winsize_from(\*STDIN); # Set the terminal size $exp->raw_pty(1); # dont change this - it must be 1, otherwise you +'ll see cmd echo in output $exp->log_stdout(0); $exp->debug(0); # spawn ssh command and wait for password $cmd = "ssh $username@"."$hostname"; $exp->spawn($cmd) or die "Cannot spawn ssh: $!"; $exp->expect($timeout, '-re' ,"[Pp]assword: ") or die "Could not log +in into the system"; $output = $exp->before(); chomp($output); # enter password and wait for a prompt $cmd = "$password\n"; $exp->send($cmd); $exp->expect($timeout, '-re' ,$prompt) or die "prompt not found"; $output = $exp->before(); chomp($output); print "LOGIN : PASS\n"; return $exp; } sub executeCmd { my ($exp,$cmd) = @_; my ($output,@output); $exp->clear_accum(); sleep 1; $exp->send("$cmd\n"); $exp->expect($timeout, '-re' ,$prompt) or die "prompt not found"; $output = $exp->before(); chomp($output); @output = split("\n",$output); # Remove command echo from output, normally in the 1st line if($cmd =~ $output[0]) { #delete $output[0] my $last = $#output; @output = @output[1..$last]; } return @output; } sub logout22 { my $exp = shift; $exp->send("logout\n"); $exp->hard_close(); }

In reply to Output from Expect-SSH session is all jumbled by ksudheer123

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.