blob: 925eccc63b006766655fd33b0a6566687d2c7ad5 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`telnetlib` --- Telnet client
3==================================
4
5.. module:: telnetlib
6 :synopsis: Telnet client class.
Skip Montanaro54662462007-12-08 15:26:16 +00007.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl8ec7f652007-08-15 14:28:01 +00008
9
10.. index:: single: protocol; Telnet
11
12The :mod:`telnetlib` module provides a :class:`Telnet` class that implements the
13Telnet protocol. See :rfc:`854` for details about the protocol. In addition, it
14provides symbolic constants for the protocol characters (see below), and for the
15telnet options. The symbolic names of the telnet options follow the definitions
16in ``arpa/telnet.h``, with the leading ``TELOPT_`` removed. For symbolic names
17of options which are traditionally not included in ``arpa/telnet.h``, see the
18module source itself.
19
20The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL,
21SE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP
22(Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase
23Character), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).
24
25
26.. class:: Telnet([host[, port[, timeout]]])
27
28 :class:`Telnet` represents a connection to a Telnet server. The instance is
29 initially not connected by default; the :meth:`open` method must be used to
Georg Brandlab756f62008-05-11 11:09:35 +000030 establish a connection. Alternatively, the host name and optional port
31 number can be passed to the constructor, to, in which case the connection to
Georg Brandldd59f1b2010-05-21 21:30:47 +000032 the server will be established before the constructor returns. The optional
Georg Brandlab756f62008-05-11 11:09:35 +000033 *timeout* parameter specifies a timeout in seconds for blocking operations
Georg Brandldd59f1b2010-05-21 21:30:47 +000034 like the connection attempt (if not specified, the global default timeout
35 setting will be used).
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
37 Do not reopen an already connected instance.
38
39 This class has many :meth:`read_\*` methods. Note that some of them raise
40 :exc:`EOFError` when the end of the connection is read, because they can return
41 an empty string for other reasons. See the individual descriptions below.
42
43 .. versionchanged:: 2.6
44 *timeout* was added.
45
46
47.. seealso::
48
49 :rfc:`854` - Telnet Protocol Specification
50 Definition of the Telnet protocol.
51
52
53.. _telnet-objects:
54
55Telnet Objects
56--------------
57
58:class:`Telnet` instances have the following methods:
59
60
61.. method:: Telnet.read_until(expected[, timeout])
62
63 Read until a given string, *expected*, is encountered or until *timeout* seconds
64 have passed.
65
66 When no match is found, return whatever is available instead, possibly the empty
67 string. Raise :exc:`EOFError` if the connection is closed and no cooked data is
68 available.
69
70
71.. method:: Telnet.read_all()
72
73 Read all data until EOF; block until connection closed.
74
75
76.. method:: Telnet.read_some()
77
78 Read at least one byte of cooked data unless EOF is hit. Return ``''`` if EOF is
79 hit. Block if no data is immediately available.
80
81
82.. method:: Telnet.read_very_eager()
83
84 Read everything that can be without blocking in I/O (eager).
85
86 Raise :exc:`EOFError` if connection closed and no cooked data available. Return
87 ``''`` if no cooked data available otherwise. Do not block unless in the midst
88 of an IAC sequence.
89
90
91.. method:: Telnet.read_eager()
92
93 Read readily available data.
94
95 Raise :exc:`EOFError` if connection closed and no cooked data available. Return
96 ``''`` if no cooked data available otherwise. Do not block unless in the midst
97 of an IAC sequence.
98
99
100.. method:: Telnet.read_lazy()
101
102 Process and return data already in the queues (lazy).
103
104 Raise :exc:`EOFError` if connection closed and no data available. Return ``''``
105 if no cooked data available otherwise. Do not block unless in the midst of an
106 IAC sequence.
107
108
109.. method:: Telnet.read_very_lazy()
110
111 Return any data available in the cooked queue (very lazy).
112
113 Raise :exc:`EOFError` if connection closed and no data available. Return ``''``
114 if no cooked data available otherwise. This method never blocks.
115
116
117.. method:: Telnet.read_sb_data()
118
119 Return the data collected between a SB/SE pair (suboption begin/end). The
120 callback should access these data when it was invoked with a ``SE`` command.
121 This method never blocks.
122
123 .. versionadded:: 2.3
124
125
126.. method:: Telnet.open(host[, port[, timeout]])
127
128 Connect to a host. The optional second argument is the port number, which
129 defaults to the standard Telnet port (23). The optional *timeout* parameter
Georg Brandlab756f62008-05-11 11:09:35 +0000130 specifies a timeout in seconds for blocking operations like the connection
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000131 attempt (if not specified, the global default timeout setting will be used).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132
133 Do not try to reopen an already connected instance.
134
135 .. versionchanged:: 2.6
136 *timeout* was added.
137
138
139.. method:: Telnet.msg(msg[, *args])
140
141 Print a debug message when the debug level is ``>`` 0. If extra arguments are
142 present, they are substituted in the message using the standard string
143 formatting operator.
144
145
146.. method:: Telnet.set_debuglevel(debuglevel)
147
148 Set the debug level. The higher the value of *debuglevel*, the more debug
149 output you get (on ``sys.stdout``).
150
151
152.. method:: Telnet.close()
153
154 Close the connection.
155
156
157.. method:: Telnet.get_socket()
158
159 Return the socket object used internally.
160
161
162.. method:: Telnet.fileno()
163
164 Return the file descriptor of the socket object used internally.
165
166
167.. method:: Telnet.write(buffer)
168
169 Write a string to the socket, doubling any IAC characters. This can block if the
170 connection is blocked. May raise :exc:`socket.error` if the connection is
171 closed.
172
173
174.. method:: Telnet.interact()
175
176 Interaction function, emulates a very dumb Telnet client.
177
178
179.. method:: Telnet.mt_interact()
180
181 Multithreaded version of :meth:`interact`.
182
183
184.. method:: Telnet.expect(list[, timeout])
185
186 Read until one from a list of a regular expressions matches.
187
188 The first argument is a list of regular expressions, either compiled
189 (:class:`re.RegexObject` instances) or uncompiled (strings). The optional second
190 argument is a timeout, in seconds; the default is to block indefinitely.
191
192 Return a tuple of three items: the index in the list of the first regular
193 expression that matches; the match object returned; and the text read up till
194 and including the match.
195
196 If end of file is found and no text was read, raise :exc:`EOFError`. Otherwise,
197 when nothing matches, return ``(-1, None, text)`` where *text* is the text
198 received so far (may be the empty string if a timeout happened).
199
200 If a regular expression ends with a greedy match (such as ``.*``) or if more
Georg Brandl09302282010-10-06 09:32:48 +0000201 than one expression can match the same input, the results are
202 non-deterministic, and may depend on the I/O timing.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000203
204
205.. method:: Telnet.set_option_negotiation_callback(callback)
206
207 Each time a telnet option is read on the input flow, this *callback* (if set) is
208 called with the following parameters : callback(telnet socket, command
209 (DO/DONT/WILL/WONT), option). No other action is done afterwards by telnetlib.
210
211
212.. _telnet-example:
213
214Telnet Example
215--------------
216
217.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
218
219
220A simple example illustrating typical use::
221
222 import getpass
223 import sys
224 import telnetlib
225
226 HOST = "localhost"
227 user = raw_input("Enter your remote account: ")
228 password = getpass.getpass()
229
230 tn = telnetlib.Telnet(HOST)
231
232 tn.read_until("login: ")
233 tn.write(user + "\n")
234 if password:
235 tn.read_until("Password: ")
236 tn.write(password + "\n")
237
238 tn.write("ls\n")
239 tn.write("exit\n")
240
241 print tn.read_all()
242