blob: b0c16833da1e7a851d6b15146ac8da2fc253e296 [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
32 the server will be established before the constructor returns. The optional
33 *timeout* parameter specifies a timeout in seconds for blocking operations
34 like the connection attempt (if not specified, or passed as None, the global
35 default timeout 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
131 attempt (if not specified, or passed as None, the global default timeout
132 setting will be used).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000133
134 Do not try to reopen an already connected instance.
135
136 .. versionchanged:: 2.6
137 *timeout* was added.
138
139
140.. method:: Telnet.msg(msg[, *args])
141
142 Print a debug message when the debug level is ``>`` 0. If extra arguments are
143 present, they are substituted in the message using the standard string
144 formatting operator.
145
146
147.. method:: Telnet.set_debuglevel(debuglevel)
148
149 Set the debug level. The higher the value of *debuglevel*, the more debug
150 output you get (on ``sys.stdout``).
151
152
153.. method:: Telnet.close()
154
155 Close the connection.
156
157
158.. method:: Telnet.get_socket()
159
160 Return the socket object used internally.
161
162
163.. method:: Telnet.fileno()
164
165 Return the file descriptor of the socket object used internally.
166
167
168.. method:: Telnet.write(buffer)
169
170 Write a string to the socket, doubling any IAC characters. This can block if the
171 connection is blocked. May raise :exc:`socket.error` if the connection is
172 closed.
173
174
175.. method:: Telnet.interact()
176
177 Interaction function, emulates a very dumb Telnet client.
178
179
180.. method:: Telnet.mt_interact()
181
182 Multithreaded version of :meth:`interact`.
183
184
185.. method:: Telnet.expect(list[, timeout])
186
187 Read until one from a list of a regular expressions matches.
188
189 The first argument is a list of regular expressions, either compiled
190 (:class:`re.RegexObject` instances) or uncompiled (strings). The optional second
191 argument is a timeout, in seconds; the default is to block indefinitely.
192
193 Return a tuple of three items: the index in the list of the first regular
194 expression that matches; the match object returned; and the text read up till
195 and including the match.
196
197 If end of file is found and no text was read, raise :exc:`EOFError`. Otherwise,
198 when nothing matches, return ``(-1, None, text)`` where *text* is the text
199 received so far (may be the empty string if a timeout happened).
200
201 If a regular expression ends with a greedy match (such as ``.*``) or if more
202 than one expression can match the same input, the results are indeterministic,
203 and may depend on the I/O timing.
204
205
206.. method:: Telnet.set_option_negotiation_callback(callback)
207
208 Each time a telnet option is read on the input flow, this *callback* (if set) is
209 called with the following parameters : callback(telnet socket, command
210 (DO/DONT/WILL/WONT), option). No other action is done afterwards by telnetlib.
211
212
213.. _telnet-example:
214
215Telnet Example
216--------------
217
218.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
219
220
221A simple example illustrating typical use::
222
223 import getpass
224 import sys
225 import telnetlib
226
227 HOST = "localhost"
228 user = raw_input("Enter your remote account: ")
229 password = getpass.getpass()
230
231 tn = telnetlib.Telnet(HOST)
232
233 tn.read_until("login: ")
234 tn.write(user + "\n")
235 if password:
236 tn.read_until("Password: ")
237 tn.write(password + "\n")
238
239 tn.write("ls\n")
240 tn.write("exit\n")
241
242 print tn.read_all()
243