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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |