danmcb has asked for the wisdom of the Perl Monks concerning the following question:
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 ...
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!
[00mtotal 24 drwxrwxr-x 2 testrobot testrobot 4096 Oct 11 12:33 [00;34mbin[00m drwxrwxr-x 3 testrobot testrobot 4096 Oct 11 12:33 [00;34mtesting[0 +0m drwxrwxr-x 3 testrobot testrobot 4096 Oct 11 11:53 [00;34mwork[00m [m]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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: odd escape chars in strings obtained by SSH
by Fletch (Bishop) on Oct 11, 2007 at 14:41 UTC | |
by dah (Sexton) on Oct 11, 2007 at 16:51 UTC | |
by lykich (Initiate) on Jun 25, 2021 at 18:27 UTC | |
|
Re: odd escape chars in strings obtained by SSH
by graff (Chancellor) on Oct 11, 2007 at 17:50 UTC | |
by Fletch (Bishop) on Oct 11, 2007 at 18:44 UTC | |
by danmcb (Monk) on Oct 12, 2007 at 08:24 UTC | |
by toma (Vicar) on Oct 12, 2007 at 16:17 UTC | |
by danmcb (Monk) on Oct 12, 2007 at 07:46 UTC |