How to remove escape sequece from string while telnet to HP procurve devices
-
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 [1;24r[1;1H[24;1HUsername: [?25h[24;1H[?25h[24;11H[24;11H[?25h[24;11H Please help to remove thse unknown characters in above strings and command outputs. any help will be appreciated...
-
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 [1;24r[1;1H[24;1HUsername: [?25h[24;1H[?25h[24;11H[24;11H[?25h[24;11H Please help to remove thse unknown characters in above strings and command outputs. any help will be appreciated...
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 doSome 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 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 [1;24r[1;1H[24;1HUsername: [?25h[24;1H[?25h[24;11H[24;11H[?25h[24;11H Please help to remove thse unknown characters in above strings and command outputs. any help will be appreciated...
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 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 [1;24r[1;1H[24;1HUsername: [?25h[24;1H[?25h[24;11H[24;11H[?25h[24;11H Please help to remove thse unknown characters in above strings and command outputs. any help will be appreciated...
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); }
}
} -
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)
Thanks.... Its working :)
-
Thanks.... Its working :)
You're welcome!
You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)