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

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(); }

Replies are listed 'Best First'.
Re: Output from Expect-SSH session is all jumbled
by salva (Canon) on May 20, 2013 at 08:10 UTC
    be lazy!
    use Net::OpenSSH; my $hostname ='50.10.0.11'; my $username ='root'; my $password ="mypassword"; my $timeout = 15; my $mac = "00:25:B5:11:11:12"; my $ssh = Net::OpenSSH->new($hostname, timeout => $timeout, user => $username, password => $password); $ssh->error and die "unable to connect to remote host: " . $ssh->error +; my ($dhcpip) = $ssh->capture("grep -i $mac /var/log/messages | grep DH +CPACK | awk {'print \$8'} |uniq"); $ssh->error and die "remote command failed: " . $ssh->error; chomp $dhcpip; my @tr_output = $ssh->capture("traceroute $dhcpip | awk '{print \$2}'" +); $ssh->error and die "remote command failed: " . $ssh->error; 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"; }

    update: BTW, the problem in your script is that you are using the traceroute command, which contains metacharacters (specifically, |), as a regular expression. Quote it using quotemeta or just use eq instead of a regex match operation.

Re: Output from Expect-SSH session is all jumbled
by ksudheer123 (Initiate) on May 21, 2013 at 10:21 UTC
    Hi, thanks for the code.. this is a simpler solution. I wanted to specifically use Expect so i tried the code I had sent. I found the problem with that though.. Expect was returning extra control characters in the returned output so pattern matching was failing.. The solution was to 'extract the required pattern from the output// but will check the openSSH to see if it can make my whole framework simpler.. many thanks..