Telnet session

Yannick Turgeon nobody at nowhere.com
Tue Aug 3 13:10:27 EDT 2004


Hello all,



I'm currently trying to pass commands to a telnet session and get the texte
generated (stdin + stdout) by the session. The problem I get is that the
Telnet.read_until() function seems to freeze after a couple of command. I
did a simplify script that reproduce the problem each time (I'm using 2.3.4
on W2K):

-------------------- Begin of script --------------------
import sys
import re
from string import *
from telnetlib import Telnet


TELNET_USER = "myUser"
TELNET_PWD = "myPassword"
TELNET_HOST = "localhost"

TELNET_PROMPT = "C:\\>"

NEWLINE = "\r\n"
TIMEOUT = 1
REMOVEJUNK = 1   #Remove Telnet output junk (formating chars?) before
printing


def printRead(texte):
 if not REMOVEJUNK:
  print texte
  return

 p = re.compile( '(.\[[0-9]{1,2};[0-9]{1,2}H[0-9]?|.\[K)+')
 print p.sub('\n', texte)



# Loging in
tn = Telnet("localhost")
printRead(tn.read_until("login: ", TIMEOUT))
tn.write(TELNET_USER + NEWLINE)
printRead(tn.read_until("password: ", TIMEOUT))
tn.write(TELNET_PWD + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))

# Send commands
tn.write("echo foo" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar1" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar2" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar3" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar4" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar5" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar6" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar7" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar8" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar9" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))

# Closing the connextion
tn.write("exit" + NEWLINE)
tn.close()
-------------------- End of script --------------------

The output looks like (command sent in read. Not included in the script
output.):
echo foo
echo foo
foo
C:\>

echo bar1
echo bar1
bar1
C:\>

echo bar2
echo bar2
bar2
C:\>

echo bar3
echo bar3
bar3
C:\>

echo bar4
echo bar4
bar4
C:\>

echo bar5
echo bar5
bar5
C:\>

echo bar6
echo bar6
bar6
C:\>

echo bar7     <----- From here read_until() is freezing forever without a
TIMEOUT
C:\>

echo bar8 echo foo
foo
bar1
bar1
bar1
bar1


echo bar9
---------------   End of output -----------------

It not related to the number of command sent. If your replace one of the
firsts "echo bar" by "dir", you'll get the error immediatly. It seems to be
related to the number of line generated by Telnet.

Anybody know why? How to correct this?

Thanks.

Yannick





More information about the Python-list mailing list