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

Simple code to connect and login to HP Procurve use(ing) Expect below, generates following output to STDOUT and PATH/FILE

[?7l[?6l[?25h[?6l[?7lConnecting to Tacacs server[?25h [?25h[?25hUsername: [?25h[?25h[?25h[?25l [?25h

Question is how do I get rid of what looks to be non-printable flow-control? I reviewed IO::Stty and have either not understood or implemented incorrectly as I have tried $object->('sane') with no luck, nor gotten a response from $object->stty('-a')

######### Start up an Expect session ################ my $exp=Expect->new; $exp->raw_pty(0); $exp->stty('sane'); print "exp settings:".$exp->stty('-a')."<<<----\n"; $exp->spawn("telnet $dev->{IP}"); $exp->log_file("$dev->{DEVICE_CONF_PATH}/$dev->{IP}.log"); if($DEBUG){ $exp->log_stdout(1); }

Replies are listed 'Best First'.
Re: Expecting non-print chars from HP Procurve
by roboticus (Chancellor) on Nov 04, 2011 at 00:16 UTC

    sgrey:

    It looks like typical ANSI escape codes for controlling terminals (setting colors, coordinates, etc.). Generally they start with an ESC (0x1b) character and end with an upper-case letter[1]. So you could simply remove all character sequences starting with ESC until the first upper-case character, like so:

    use strict; use warnings; # The ugly bit of text from your post my $text = <<EOTXT; <p>Simple code to connect and login to HP Procurve use(ing) Expect bel +ow, generates following output to STDOUT and PATH/FILE</p><p>^[[2J^[[ +?7l^[[3;23r^[[?6l^[[24;27H^[[?25h^[[24;27H^[[?6l^[[1;24r^[[?7l^[[2J^[ +[24;27H^[[1;24r^[[24;27H^[[24;1HConnecting to Tacacs server^[[?25h^[[24;1H^[[24;1H ^[[?25h^[[24;1H^[[?25h^[[24;1H^[[24;1HUsername: ^[[?25h^[[24;1H^[[?25h^[[24;11H^[[24;11H^[[?25h^[[24;11H^[[1;1H^[[?25l +^[[24;11H^[[24;1H ^[[?25h^[[24;11H</p> EOTXT # Remove all bits of text from an ESC character to the first uppercase + alphabetic $text =~ s/\x1b[^A-Z]+[A-Z]//g; print "Remaining text is '$text'\n";

    When I run this, I get:

    $ perl foobar.pl Remaining text is '<p>Simple code to connect and login to HP Procurve +use(ing) Expect below, generates following output to STDOUT and PATH/ +FILE</p><p>Connecting to Tacacs server Username: </p> '

    Notes

    [1]: The wikipedia article I linked to says the end character is from ASCII 64 to 126 (@ to ~), so I'm clearly wrong here. But since the code gives me the desired (i.e., what I expected to see) results, so I'll just note the discrepancy here, rather than update the code.

    [2]: Since the code above is a cut-and-paste from my screen, the escapes are turned into the sequence '^['. So you may want to add the line:

        $text =~ s/^[/\x1b/g;

    just after setting the value if you want to download the above code & execute it.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks for the info re: what was going on, but how can I remove them in the Expect method so I never see them at all?

        sgrey:

        I don't use Expect, so take this with a grain of salt.

        If it's only happening during login, perhaps you could start logging *after* you've logged in. (If you need to debug logins, perhaps log the logins to one file, then switch to a new log file for normal operations. That way, you could ignore the sequence.)

        If it's happening all the time, perhaps you could either modify your log file viewer application to ignore the escape sequences, or even pipe them through a quick script that will squash them out for you.

        If there's an Expect method (such as using a filtered file handle or such) we'll have to wait for someone a bit more experienced/knowledgeable to chime in.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.