1 """TELNET client class.
3 Based on RFC 854: TELNET Protocol Specification, by J. Postel and
8 >>> from telnetlib import Telnet
9 >>> tn = Telnet('www.python.org', 79) # connect to finger port
10 >>> tn.write('guido\r\n')
11 >>> print tn.read_all()
12 Login Name TTY Idle When Where
13 guido Guido van Rossum pts/2 <Dec 2 11:10> snag.cnri.reston..
17 Note that read_all() won't read until eof -- it just reads some data
18 -- but it guarantees to read at least one byte unless EOF is hit.
20 It is possible to pass a Telnet object to select.select() in order to
21 wait until more data is available. Note that in this case,
22 read_eager() may return '' even if there was data on the socket,
23 because the protocol negotiation may have eaten the data. This is why
24 EOFError is needed in some cases to distinguish between "no data" and
25 "connection closed" (since the socket also appears ready for reading
30 - timeout should be intrinsic to the connection object instead of an
31 option on one of the read calls only
46 # Telnet protocol defaults
49 # Telnet protocol characters (don't change)
50 IAC
= chr(255) # "Interpret As Command"
57 SE
= chr(240) # Subnegotiation End
58 NOP
= chr(241) # No Operation
59 DM
= chr(242) # Data Mark
60 BRK
= chr(243) # Break
61 IP
= chr(244) # Interrupt process
62 AO
= chr(245) # Abort output
63 AYT
= chr(246) # Are You There
64 EC
= chr(247) # Erase Character
65 EL
= chr(248) # Erase Line
66 GA
= chr(249) # Go Ahead
67 SB
= chr(250) # Subnegotiation Begin
70 # Telnet protocol options code (don't change)
71 # These ones all come from arpa/telnet.h
72 BINARY
= chr(0) # 8-bit data path
74 RCP
= chr(2) # prepare to reconnect
75 SGA
= chr(3) # suppress go ahead
76 NAMS
= chr(4) # approximate message size
77 STATUS
= chr(5) # give status
78 TM
= chr(6) # timing mark
79 RCTE
= chr(7) # remote controlled transmission and echo
80 NAOL
= chr(8) # negotiate about output line width
81 NAOP
= chr(9) # negotiate about output page size
82 NAOCRD
= chr(10) # negotiate about CR disposition
83 NAOHTS
= chr(11) # negotiate about horizontal tabstops
84 NAOHTD
= chr(12) # negotiate about horizontal tab disposition
85 NAOFFD
= chr(13) # negotiate about formfeed disposition
86 NAOVTS
= chr(14) # negotiate about vertical tab stops
87 NAOVTD
= chr(15) # negotiate about vertical tab disposition
88 NAOLFD
= chr(16) # negotiate about output LF disposition
89 XASCII
= chr(17) # extended ascii character set
90 LOGOUT
= chr(18) # force logout
91 BM
= chr(19) # byte macro
92 DET
= chr(20) # data entry terminal
93 SUPDUP
= chr(21) # supdup protocol
94 SUPDUPOUTPUT
= chr(22) # supdup output
95 SNDLOC
= chr(23) # send location
96 TTYPE
= chr(24) # terminal type
97 EOR
= chr(25) # end or record
98 TUID
= chr(26) # TACACS user identification
99 OUTMRK
= chr(27) # output marking
100 TTYLOC
= chr(28) # terminal location number
101 VT3270REGIME
= chr(29) # 3270 regime
102 X3PAD
= chr(30) # X.3 PAD
103 NAWS
= chr(31) # window size
104 TSPEED
= chr(32) # terminal speed
105 LFLOW
= chr(33) # remote flow control
106 LINEMODE
= chr(34) # Linemode option
107 XDISPLOC
= chr(35) # X Display Location
108 OLD_ENVIRON
= chr(36) # Old - Environment variables
109 AUTHENTICATION
= chr(37) # Authenticate
110 ENCRYPT
= chr(38) # Encryption option
111 NEW_ENVIRON
= chr(39) # New - Environment variables
112 # the following ones come from
113 # http://www.iana.org/assignments/telnet-options
114 # Unfortunately, that document does not assign identifiers
115 # to all of them, so we are making them up
116 TN3270E
= chr(40) # TN3270E
117 XAUTH
= chr(41) # XAUTH
118 CHARSET
= chr(42) # CHARSET
119 RSP
= chr(43) # Telnet Remote Serial Port
120 COM_PORT_OPTION
= chr(44) # Com Port Control Option
121 SUPPRESS_LOCAL_ECHO
= chr(45) # Telnet Suppress Local Echo
122 TLS
= chr(46) # Telnet Start TLS
123 KERMIT
= chr(47) # KERMIT
124 SEND_URL
= chr(48) # SEND-URL
125 FORWARD_X
= chr(49) # FORWARD_X
126 PRAGMA_LOGON
= chr(138) # TELOPT PRAGMA LOGON
127 SSPI_LOGON
= chr(139) # TELOPT SSPI LOGON
128 PRAGMA_HEARTBEAT
= chr(140) # TELOPT PRAGMA HEARTBEAT
129 EXOPL
= chr(255) # Extended-Options-List
134 """Telnet interface class.
136 An instance of this class represents a connection to a telnet
137 server. The instance is initially not connected; the open()
138 method must be used to establish a connection. Alternatively, the
139 host name and optional port number can be passed to the
142 Don't try to reopen an already connected instance.
144 This class has many read_*() methods. Note that some of them
145 raise EOFError when the end of the connection is read, because
146 they can return an empty string for other reasons. See the
147 individual doc strings.
149 read_until(expected, [timeout])
150 Read until the expected string has been seen, or a timeout is
151 hit (default is no timeout); may block.
154 Read all data until EOF; may block.
157 Read at least one byte or EOF; may block.
160 Read all data available already queued or on the socket,
164 Read either data already queued or some data available on the
165 socket, without blocking.
168 Read all data in the raw queue (processing it first), without
169 doing any socket I/O.
172 Reads all data in the cooked queue, without doing any socket
176 Reads available data between SB ... SE sequence. Don't block.
178 set_option_negotiation_callback(callback)
179 Each time a telnet option is read on the input flow, this callback
180 (if set) is called with the following parameters :
181 callback(telnet socket, command, option)
182 option will be chr(0) when there is no option.
183 No other action is done afterwards by telnetlib.
187 def __init__(self
, host
=None, port
=0):
190 When called without arguments, create an unconnected instance.
191 With a hostname argument, it connects the instance; a port
195 self
.debuglevel
= DEBUGLEVEL
203 self
.iacseq
= '' # Buffer for IAC sequence.
204 self
.sb
= 0 # flag for SB and SE sequence.
206 self
.option_callback
= None
208 self
.open(host
, port
)
210 def open(self
, host
, port
=0):
211 """Connect to a host.
213 The optional second argument is the port number, which
214 defaults to the standard telnet port (23).
216 Don't try to reopen an already connected instance.
224 msg
= "getaddrinfo returns an empty list"
225 for res
in socket
.getaddrinfo(host
, port
, 0, socket
.SOCK_STREAM
):
226 af
, socktype
, proto
, canonname
, sa
= res
228 self
.sock
= socket
.socket(af
, socktype
, proto
)
229 self
.sock
.connect(sa
)
230 except socket
.error
, msg
:
237 raise socket
.error
, msg
240 """Destructor -- close the connection."""
243 def msg(self
, msg
, *args
):
244 """Print a debug message, when the debug level is > 0.
246 If extra arguments are present, they are substituted in the
247 message using the standard string formatting operator.
250 if self
.debuglevel
> 0:
251 print 'Telnet(%s,%d):' % (self
.host
, self
.port
),
257 def set_debuglevel(self
, debuglevel
):
258 """Set the debug level.
260 The higher it is, the more debug output you get (on sys.stdout).
263 self
.debuglevel
= debuglevel
266 """Close the connection."""
274 def get_socket(self
):
275 """Return the socket object used internally."""
279 """Return the fileno() of the socket object used internally."""
280 return self
.sock
.fileno()
282 def write(self
, buffer):
283 """Write a string to the socket, doubling any IAC characters.
285 Can block if the connection is blocked. May raise
286 socket.error if the connection is closed.
290 buffer = buffer.replace(IAC
, IAC
+IAC
)
291 self
.msg("send %s", `
buffer`
)
292 self
.sock
.sendall(buffer)
294 def read_until(self
, match
, timeout
=None):
295 """Read until a given string is encountered or until timeout.
297 When no match is found, return whatever is available instead,
298 possibly the empty string. Raise EOFError if the connection
299 is closed and no cooked data is available.
304 i
= self
.cookedq
.find(match
)
307 buf
= self
.cookedq
[:i
]
308 self
.cookedq
= self
.cookedq
[i
:]
310 s_reply
= ([self
], [], [])
312 if timeout
is not None:
313 s_args
= s_args
+ (timeout
,)
314 while not self
.eof
and select
.select(*s_args
) == s_reply
:
315 i
= max(0, len(self
.cookedq
)-n
)
318 i
= self
.cookedq
.find(match
, i
)
321 buf
= self
.cookedq
[:i
]
322 self
.cookedq
= self
.cookedq
[i
:]
324 return self
.read_very_lazy()
327 """Read all data until EOF; block until connection closed."""
337 """Read at least one byte of cooked data unless EOF is hit.
339 Return '' if EOF is hit. Block if no data is immediately
344 while not self
.cookedq
and not self
.eof
:
351 def read_very_eager(self
):
352 """Read everything that's possible without blocking in I/O (eager).
354 Raise EOFError if connection closed and no cooked data
355 available. Return '' if no cooked data available otherwise.
356 Don't block unless in the midst of an IAC sequence.
360 while not self
.eof
and self
.sock_avail():
363 return self
.read_very_lazy()
365 def read_eager(self
):
366 """Read readily available data.
368 Raise EOFError if connection closed and no cooked data
369 available. Return '' if no cooked data available otherwise.
370 Don't block unless in the midst of an IAC sequence.
374 while not self
.cookedq
and not self
.eof
and self
.sock_avail():
377 return self
.read_very_lazy()
380 """Process and return data that's already in the queues (lazy).
382 Raise EOFError if connection closed and no data available.
383 Return '' if no cooked data available otherwise. Don't block
384 unless in the midst of an IAC sequence.
388 return self
.read_very_lazy()
390 def read_very_lazy(self
):
391 """Return any data available in the cooked queue (very lazy).
393 Raise EOFError if connection closed and no data available.
394 Return '' if no cooked data available otherwise. Don't block.
399 if not buf
and self
.eof
and not self
.rawq
:
400 raise EOFError, 'telnet connection closed'
403 def read_sb_data(self
):
404 """Return any data available in the SB ... SE queue.
406 Return '' if no SB ... SE available. Should only be called
407 after seeing a SB or SE command. When a new SB command is
408 found, old unread SB data will be discarded. Don't block.
415 def set_option_negotiation_callback(self
, callback
):
416 """Provide a callback function called after each receipt of a telnet option."""
417 self
.option_callback
= callback
419 def process_rawq(self
):
420 """Transfer from raw queue to cooked queue.
422 Set self.eof when connection is closed. Don't block unless in
423 the midst of an IAC sequence.
429 c
= self
.rawq_getchar()
436 buf
[self
.sb
] = buf
[self
.sb
] + c
440 elif len(self
.iacseq
) == 1:
441 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]'
442 if c
in (DO
, DONT
, WILL
, WONT
):
448 buf
[self
.sb
] = buf
[self
.sb
] + c
450 if c
== SB
: # SB ... SE start.
455 self
.sbdataq
= self
.sbdataq
+ buf
[1]
457 if self
.option_callback
:
458 # Callback is supposed to look into
460 self
.option_callback(self
.sock
, c
, NOOPT
)
462 # We can't offer automatic processing of
463 # suboptions. Alas, we should not get any
464 # unless we did a WILL/DO before.
465 self
.msg('IAC %d not recognized' % ord(c
))
466 elif len(self
.iacseq
) == 2:
470 if cmd
in (DO
, DONT
):
471 self
.msg('IAC %s %d',
472 cmd
== DO
and 'DO' or 'DONT', ord(opt
))
473 if self
.option_callback
:
474 self
.option_callback(self
.sock
, cmd
, opt
)
476 self
.sock
.sendall(IAC
+ WONT
+ opt
)
477 elif cmd
in (WILL
, WONT
):
478 self
.msg('IAC %s %d',
479 cmd
== WILL
and 'WILL' or 'WONT', ord(opt
))
480 if self
.option_callback
:
481 self
.option_callback(self
.sock
, cmd
, opt
)
483 self
.sock
.sendall(IAC
+ DONT
+ opt
)
484 except EOFError: # raised by self.rawq_getchar()
485 self
.iacseq
= '' # Reset on EOF
488 self
.cookedq
= self
.cookedq
+ buf
[0]
489 self
.sbdataq
= self
.sbdataq
+ buf
[1]
491 def rawq_getchar(self
):
492 """Get next char from raw queue.
494 Block if no data is immediately available. Raise EOFError
495 when connection is closed.
502 c
= self
.rawq
[self
.irawq
]
503 self
.irawq
= self
.irawq
+ 1
504 if self
.irawq
>= len(self
.rawq
):
510 """Fill raw queue from exactly one recv() system call.
512 Block if no data is immediately available. Set self.eof when
513 connection is closed.
516 if self
.irawq
>= len(self
.rawq
):
519 # The buffer size should be fairly small so as to avoid quadratic
520 # behavior in process_rawq() above
521 buf
= self
.sock
.recv(50)
522 self
.msg("recv %s", `buf`
)
524 self
.rawq
= self
.rawq
+ buf
526 def sock_avail(self
):
527 """Test whether data is available on the socket."""
528 return select
.select([self
], [], [], 0) == ([self
], [], [])
531 """Interaction function, emulates a very dumb telnet client."""
532 if sys
.platform
== "win32":
536 rfd
, wfd
, xfd
= select
.select([self
, sys
.stdin
], [], [])
539 text
= self
.read_eager()
541 print '*** Connection closed by remote host ***'
544 sys
.stdout
.write(text
)
547 line
= sys
.stdin
.readline()
552 def mt_interact(self
):
553 """Multithreaded version of interact()."""
555 thread
.start_new_thread(self
.listener
, ())
557 line
= sys
.stdin
.readline()
563 """Helper for mt_interact() -- this executes in the other thread."""
566 data
= self
.read_eager()
568 print '*** Connection closed by remote host ***'
571 sys
.stdout
.write(data
)
575 def expect(self
, list, timeout
=None):
576 """Read until one from a list of a regular expressions matches.
578 The first argument is a list of regular expressions, either
579 compiled (re.RegexObject instances) or uncompiled (strings).
580 The optional second argument is a timeout, in seconds; default
583 Return a tuple of three items: the index in the list of the
584 first regular expression that matches; the match object
585 returned; and the text read up till and including the match.
587 If EOF is read and no text was read, raise EOFError.
588 Otherwise, when nothing matches, return (-1, None, text) where
589 text is the text received so far (may be the empty string if a
592 If a regular expression ends with a greedy match (e.g. '.*')
593 or if more than one expression can match the same input, the
594 results are undeterministic, and may depend on the I/O timing.
599 indices
= range(len(list))
601 if not hasattr(list[i
], "search"):
603 list[i
] = re
.compile(list[i
])
607 m
= list[i
].search(self
.cookedq
)
610 text
= self
.cookedq
[:e
]
611 self
.cookedq
= self
.cookedq
[e
:]
615 if timeout
is not None:
616 r
, w
, x
= select
.select([self
.fileno()], [], [], timeout
)
620 text
= self
.read_very_lazy()
621 if not text
and self
.eof
:
623 return (-1, None, text
)
627 """Test program for telnetlib.
629 Usage: python telnetlib.py [-d] ... [host [port]]
631 Default host is localhost; default port is 23.
635 while sys
.argv
[1:] and sys
.argv
[1] == '-d':
636 debuglevel
= debuglevel
+1
643 portstr
= sys
.argv
[2]
647 port
= socket
.getservbyname(portstr
, 'tcp')
649 tn
.set_debuglevel(debuglevel
)
654 if __name__
== '__main__':