ziutek / telnet

Package to handle a telnet connection
Other
141 stars 60 forks source link

How to remove control characters on the output of telnet? #11

Closed vinllen closed 6 years ago

vinllen commented 6 years ago

Hi, Ziutek. Maybe it's not proper to ask this question here, but it really confused me for several days. I want to remove all control characters in the telnet output because there are too many special characters over there. For example, the telnet output is:

ae23        Enabled     Down    Disabled      Auto    Auto                                                    ^M^@^M
ae24        Enabled     Down    Disabled      Auto    Auto                                                    ^M^@^M
^[[J^M^@ --More-- (END) ^[[J^M^@ --More-- (END) ^[[J^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^[[J^[[J^[[J^M^@test.Int> exit

There are some extra characters including non-printable and printable like ^[, ^H, ^M, [J, --More-- (END) and so on. But this output is normal when printing on the screen:

ae23        Enabled     Down    Disabled      Auto    Auto
ae24        Enabled     Down    Disabled      Auto    Auto
test.Int> exit

All the control character are not shown. I know there is a way that uses col -b bash command to remove them. But it's not an effective way because telnet is an interactive way. Using os.exec to filter control characters for every command is not good. So do you have any idea to remove all the control characters on the telnet output?

ziutek commented 6 years ago

These characters are terminal control symbols (see: https://en.wikipedia.org/wiki/Control_character). You can simply filter them in your application.

Sometimes an application that produces such formatting can detect that its standard output isn't a terminal and can suppress control symbols. In such case, in Linux/Unix, you can run this application this way:

application | cat

to avoid control characters.

vinllen commented 6 years ago

Thanks, Ziutek, you're so nice. I move this question to my workmate who is downstream of me. He tries to parse all the control characters and solve this problem.