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