string operations (ctrl characters)

DR dean at valtronics.co.za
Thu Aug 16 02:26:42 EDT 2001


Hi all.

I have a problem with the using the telnetlib and string modules together. I
am telnetting to a host, getting the screen output with read_until(), which
is fine, but it prints out some kind of weird control characters, which I
guess are escape codes or something to do with telnet itself.

This is a sample of what I'm talking about:

[[

[[[nfo: [rriving:


Now, those characters (the block-like thing and the [) weren't the only
characters I was getting, but I managed to replace the rest using
string.replace and re.escape

first I converted the escape codes to \'s with re.escape

        test2 = re.escape(test1)

then replaced the \ with a blank

        test15 = string.replace(test14,"\\",'')

which worked great. There were also some "0m-39m" which I replaced with
blanks, too.

So all I'm left with is [ and . I tried to do the same for those
characters, but I can't seem to get rid of them.

        test15 = string.replace(test14,"\\",'')
        test16 = string.replace(test15,"[[" ,'')

The above attempts didn't work.

If anyone could offer any advice or suggest a better way of doing this, it
would be great.  Is there a better way of doing this ??

the greater context is below:
#######################################

tn = telnetlib.Telnet(HOST,5000)

tn.read_until("Name:")
tn.write(user + "\n")
tn.read_until("Password:")
tn.write(password + "\n")



tn.write(' ')

aaa = 1
while aaa == 1:

 test1 = tn.read_until('says:')
        test2 = re.escape(test1)
 test3 = string.replace(test2,"0m",'')
 test4 = string.replace(test3,"30m",'')
 test5 = string.replace(test4,"31m",'')
        test6 = string.replace(test5,"32m",'')
        test7 = string.replace(test6,"33m",'')
        test8 = string.replace(test7,"34m",'')
        test9 = string.replace(test8,"35m",'')
        test10 = string.replace(test9,"36m",'')
        test11 = string.replace(test10,"37m",'')
        test12 = string.replace(test11,"38m",'')
        test13 = string.replace(test12,"39m",'')
        test14 = string.replace(test13,"1m",'')
        test15 = string.replace(test14,"\\",'')
        test16 = string.replace(test15,"[[" ,'')
        test17 = string.replace(test16,"" ,'')

 print test17

Thanks a lot,
D.R.





More information about the Python-list mailing list