Here's a little secure shell puzzler for you monks.

I'm using Net::SSH::Expect to connect to a remote machine, run commands and grab their output, for testing purposes. I wrapped the whole thing in an object, which has an "exec" method which is like that of N:S:E, except that it will remove the prompt, return either an array ref or a string depending on context - and chomp newlines.

Ah, new lines. Chomp just doesn't seem to work here. I check the value of $\, and then start looking at the output using unpack to hexdump. There is some odd stuff here - I guess this is to do with ssh operation, butI can't figure it, or find anything about it online ...

Here's some code and what I get. My main module:
use TestRobot; my $tr = TestRobot->new( 'config.yml' ); $tr->open; my $s = $tr->exec('ls -l'); print $s; #print "\n***\n"; #print unpack('H*', $/); #print "\n***\n"; #print unpack('H*', 'abc'); #print "\n***\n"; #print unpack('H*', $s); #print "\n***\n"; $tr->close;

and here is the exec method in TestRobot:

sub exec { my ( $self, $cmd ) = @_; croak 'cannot exec unless state is open' unless $self->is_open; croak 'need a command to execute' unless defined $cmd && $cmd ne ''; my $r = $self->{ssh}->exec( $cmd ); #my $q = chomp $r; print "$q gone\n"; # remove trailing prompt and surrounding whitespace from output my $u = $self->{config}{test_server}{username}; $r =~ s/\[$u@.*$//g; if ( wantarray ) { my @a = split "\n", $r; # chomp( @a ); return \@a; } else { return $r; } }

(the chomp removes zero chars, by the way). Finally, here is what I get when I uncomment those prints, and redirect the output to a file. Not that the odd ^[[ sequences don't show up when you run it in a terminal!

total 24 drwxrwxr-x 2 testrobot testrobot 4096 Oct 11 12:33 bin drwxrwxr-x 3 testrobot testrobot 4096 Oct 11 12:33 testing[0 +0m drwxrwxr-x 3 testrobot testrobot 4096 Oct 11 11:53 work ]0;testrobot@svnsepia:~ *** 0a *** 616263 *** 1b5b30306d746f74616c2032340a64727778727778722d782020322074657374726f62 +6f742074657374726f626f742034303936204f63742031312031323a3333201b5b303 +03b33346d62696e1b5b30306d0a64727778727778722d782020332074657374726f62 +6f742074657374726f626f742034303936204f63742031312031323a3333201b5b303 +03b33346d74657374696e671b5b30306d0a64727778727778722d7820203320746573 +74726f626f742074657374726f626f742034303936204f63742031312031313a35332 +01b5b30303b33346d776f726b1b5b30306d0a1b5b6d1b5d303b74657374726f626f74 +4073766e73657069613a7e07 ***

What I would like to do is filter this down do just raw ASCII, because otherwise it is going to make running tests on the output very messy. But how? options to ssh? (I do call "stty raw -echo" after connecting, as suggested in the docs.

As always, all help is gratefully received.


In reply to odd escape chars in strings obtained by SSH by danmcb

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.