Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to remove escape sequece from string while telnet to HP procurve devices

How to remove escape sequece from string while telnet to HP procurve devices

Scheduled Pinned Locked Moved C#
comhelptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    superselector
    wrote on last edited by
    #1

    I am trying to telnet a HProCurve J9279A Switch but i am getting some unknown characters in the login prompt when i am writing the response in text file . Please let me know how to handle this and remove these junk characters from string . I can say these are some escape sequense which in not recognized by string . Please register your products now at: www.ProCurve.com Username: [?25h[?25h[?25h Please help to remove thse unknown characters in above strings and command outputs. any help will be appreciated...

    OriginalGriffO M 3 Replies Last reply
    0
    • S superselector

      I am trying to telnet a HProCurve J9279A Switch but i am getting some unknown characters in the login prompt when i am writing the response in text file . Please let me know how to handle this and remove these junk characters from string . I can say these are some escape sequense which in not recognized by string . Please register your products now at: www.ProCurve.com Username: [?25h[?25h[?25h Please help to remove thse unknown characters in above strings and command outputs. any help will be appreciated...

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Ok, those are VT100 / VT220 terminal control sequences and then consist of ASCII characters:

      ESC [ n ; n x

      Where:

      ESC is Hex 1B
      [ is [
      n is a number, in ASCII digits: '0' (hex 30) to '9' (hex 39) repeated
      ; is ;
      x is a command code which tells the terminal what to do

      Some parts are optional, and question marks can be included as well! The command code is an ASCII character which (IIRC) will be upper or lower case 'a' to 'z'. The command code tells the system what to do with the whole command: r is a "Set scrolling region" command, H is cursor positioning, and so forth. (You can find out what they do here: http://epsfiles.intermec.com/eps_files/eps_man/977047037c.pdf[^] - section 8) To remove them, you will have to parse the string yourself, and find the start and end of the sequence: it may be possible to do it with a regex, but I've never tried (And am not about to! :laugh:)

      You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • S superselector

        I am trying to telnet a HProCurve J9279A Switch but i am getting some unknown characters in the login prompt when i am writing the response in text file . Please let me know how to handle this and remove these junk characters from string . I can say these are some escape sequense which in not recognized by string . Please register your products now at: www.ProCurve.com Username: [?25h[?25h[?25h Please help to remove thse unknown characters in above strings and command outputs. any help will be appreciated...

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        A quick thought (it's amazing what else you can think of when in a boring meeting) gives me this regex:

        \x1B\[\??\d+(;\d+)?[a-zA-Z]

        That with a regex Replace operation replacing with a empty string should get rid of them. Probably. :laugh:

        You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        S 1 Reply Last reply
        0
        • S superselector

          I am trying to telnet a HProCurve J9279A Switch but i am getting some unknown characters in the login prompt when i am writing the response in text file . Please let me know how to handle this and remove these junk characters from string . I can say these are some escape sequense which in not recognized by string . Please register your products now at: www.ProCurve.com Username: [?25h[?25h[?25h Please help to remove thse unknown characters in above strings and command outputs. any help will be appreciated...

          M Offline
          M Offline
          Member 10284633
          wrote on last edited by
          #4

          I run into the same problem and have figured out a workaround that works for as. It is not a perfect solution. I use a regular expressions substituions between the TCP buffer and the Telnet protocol because the firmware sometimes use unknow esc sequnences that bother the telnet/terminal protocol. I use Perl for switch automation but i try to translate it to C# (The orginal is a Perl module

          Regexp::Common::ANSIescape

          ). I use this expression

          \x1BE/\n

          before the big one and this after

          \[[0-9]+;[0-9]+H

          because the firmware use sometimes esc secuences that does not exist.

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Text.RegularExpressions;
          using System.Threading.Tasks;

          namespace ProcurevAnsiFilter {
          class Program {
          const string CHAR_STR = @"(.|\n)*?";

          const string CSI\_7BIT    = @"\\x1B\\\\\\x5B";
          const string CSI\_8BIT    = @"\\x9B";
          
          const string CSI\_7OR8    = @"(?:" + CSI\_7BIT + "|" + CSI\_8BIT + ")";
          
          const string C1\_NST\_8BIT = @"\[\\x80-\\x8F\\x91-\\x97\\x99\\x9A\\x9C\]";
          const string C1\_NST\_7BIT = @"\\x1B\[\\x40-\\x4F\\x51-\\x57\\x59\\x5A\\\\\\x5C\]";
          
          const string C1\_STR\_7BIT = @"\\x1B\[\\x5D\\x50\\x58\\x5E\\x5F\]";
          const string C1\_STR\_8BIT = @"\[\\x9D\\x90\\x98\\x9E\\x9F\]";
          
          const string C1\_STR\_7OR8 = @"(?:" + C1\_STR\_7BIT + "|" + C1\_STR\_8BIT + ")";
          
          const string ST\_7BIT     = @"\\x1B\\\\\\\\";
          const string ST\_8BIT     = @"\\x9C";
          const string ST\_7OR8     = @"(?:" + ST\_7BIT     + "|" + ST\_8BIT     + ")";
          
          
          static void Main(string\[\] args) {
          string\[\] tmp = new string\[\] {
            @"(?:\[\\x30-\\x3F\]\*)(?:\[\\x20-\\x2F\]\*\[\\x40-\\x7E\])",
            C1\_NST\_7BIT,
            C1\_NST\_8BIT,
            C1\_STR\_7OR8 + CHAR\_STR + ST\_7OR8
          };
            string regex = "(?:" + string.Join("|", tmp) + ")";
          
            Regex reg = new Regex(regex);
          }
          

          }
          }

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            A quick thought (it's amazing what else you can think of when in a boring meeting) gives me this regex:

            \x1B\[\??\d+(;\d+)?[a-zA-Z]

            That with a regex Replace operation replacing with a empty string should get rid of them. Probably. :laugh:

            You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)

            S Offline
            S Offline
            superselector
            wrote on last edited by
            #5

            Thanks.... Its working :)

            OriginalGriffO 1 Reply Last reply
            0
            • S superselector

              Thanks.... Its working :)

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              You're welcome!

              You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups