blob: f3d914adc632d2af765241838b49cdb3738da3a0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`telnetlib` --- Telnet client
2==================================
3
4.. module:: telnetlib
5 :synopsis: Telnet client class.
Christian Heimes895627f2007-12-08 17:28:33 +00006.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl116aa622007-08-15 14:28:22 +00007
8
9.. index:: single: protocol; Telnet
10
11The :mod:`telnetlib` module provides a :class:`Telnet` class that implements the
12Telnet protocol. See :rfc:`854` for details about the protocol. In addition, it
13provides symbolic constants for the protocol characters (see below), and for the
14telnet options. The symbolic names of the telnet options follow the definitions
15in ``arpa/telnet.h``, with the leading ``TELOPT_`` removed. For symbolic names
16of options which are traditionally not included in ``arpa/telnet.h``, see the
17module source itself.
18
19The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL,
20SE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP
21(Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase
22Character), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).
23
24
Georg Brandl7f01a132009-09-16 15:58:14 +000025.. class:: Telnet(host=None, port=0[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +000026
27 :class:`Telnet` represents a connection to a Telnet server. The instance is
28 initially not connected by default; the :meth:`open` method must be used to
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000029 establish a connection. Alternatively, the host name and optional port
Georg Brandlf78e02b2008-06-10 17:40:04 +000030 and timeout can be passed to the constructor, in which case the connection to
31 the server will be established before the constructor returns. The optional
32 *timeout* parameter specifies a timeout in seconds for the connection attempt (if
33 not specified, the global default timeout setting will be used).
34
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000035 number can be passed to the constructor, to, in which case the connection to
36 the server will be established before the constructor returns. The optional
37 *timeout* parameter specifies a timeout in seconds for blocking operations
38 like the connection attempt (if not specified, or passed as None, the global
39 default timeout setting will be used).
Georg Brandl116aa622007-08-15 14:28:22 +000040
41 Do not reopen an already connected instance.
42
43 This class has many :meth:`read_\*` methods. Note that some of them raise
44 :exc:`EOFError` when the end of the connection is read, because they can return
45 an empty string for other reasons. See the individual descriptions below.
46
Georg Brandl116aa622007-08-15 14:28:22 +000047
48.. seealso::
49
50 :rfc:`854` - Telnet Protocol Specification
51 Definition of the Telnet protocol.
52
53
54.. _telnet-objects:
55
56Telnet Objects
57--------------
58
59:class:`Telnet` instances have the following methods:
60
61
Georg Brandl7f01a132009-09-16 15:58:14 +000062.. method:: Telnet.read_until(expected, timeout=None)
Georg Brandl116aa622007-08-15 14:28:22 +000063
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000064 Read until a given byte string, *expected*, is encountered or until *timeout*
65 seconds have passed.
Georg Brandl116aa622007-08-15 14:28:22 +000066
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000067 When no match is found, return whatever is available instead, possibly empty
68 bytes. Raise :exc:`EOFError` if the connection is closed and no cooked data
69 is available.
Georg Brandl116aa622007-08-15 14:28:22 +000070
71
72.. method:: Telnet.read_all()
73
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000074 Read all data until EOF as bytes; block until connection closed.
Georg Brandl116aa622007-08-15 14:28:22 +000075
76
77.. method:: Telnet.read_some()
78
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000079 Read at least one byte of cooked data unless EOF is hit. Return ``b''`` if
80 EOF is hit. Block if no data is immediately available.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
83.. method:: Telnet.read_very_eager()
84
85 Read everything that can be without blocking in I/O (eager).
86
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000087 Raise :exc:`EOFError` if connection closed and no cooked data available.
88 Return ``b''`` if no cooked data available otherwise. Do not block unless in
89 the midst of an IAC sequence.
Georg Brandl116aa622007-08-15 14:28:22 +000090
91
92.. method:: Telnet.read_eager()
93
94 Read readily available data.
95
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000096 Raise :exc:`EOFError` if connection closed and no cooked data available.
97 Return ``b''`` if no cooked data available otherwise. Do not block unless in
98 the midst of an IAC sequence.
Georg Brandl116aa622007-08-15 14:28:22 +000099
100
101.. method:: Telnet.read_lazy()
102
103 Process and return data already in the queues (lazy).
104
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000105 Raise :exc:`EOFError` if connection closed and no data available. Return
106 ``b''`` if no cooked data available otherwise. Do not block unless in the
107 midst of an IAC sequence.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109
110.. method:: Telnet.read_very_lazy()
111
112 Return any data available in the cooked queue (very lazy).
113
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000114 Raise :exc:`EOFError` if connection closed and no data available. Return
115 ``b''`` if no cooked data available otherwise. This method never blocks.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117
118.. method:: Telnet.read_sb_data()
119
120 Return the data collected between a SB/SE pair (suboption begin/end). The
121 callback should access these data when it was invoked with a ``SE`` command.
122 This method never blocks.
123
Georg Brandl116aa622007-08-15 14:28:22 +0000124
Georg Brandl7f01a132009-09-16 15:58:14 +0000125.. method:: Telnet.open(host, port=0[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +0000126
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
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000129 specifies a timeout in seconds for blocking operations like the connection
Georg Brandlf78e02b2008-06-10 17:40:04 +0000130 attempt (if not specified, the global default timeout setting will be used).
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132 Do not try to reopen an already connected instance.
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Georg Brandl7f01a132009-09-16 15:58:14 +0000135.. method:: Telnet.msg(msg, *args)
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137 Print a debug message when the debug level is ``>`` 0. If extra arguments are
138 present, they are substituted in the message using the standard string
139 formatting operator.
140
141
142.. method:: Telnet.set_debuglevel(debuglevel)
143
144 Set the debug level. The higher the value of *debuglevel*, the more debug
145 output you get (on ``sys.stdout``).
146
147
148.. method:: Telnet.close()
149
150 Close the connection.
151
152
153.. method:: Telnet.get_socket()
154
155 Return the socket object used internally.
156
157
158.. method:: Telnet.fileno()
159
160 Return the file descriptor of the socket object used internally.
161
162
163.. method:: Telnet.write(buffer)
164
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000165 Write a byte string to the socket, doubling any IAC characters. This can
166 block if the connection is blocked. May raise :exc:`socket.error` if the
167 connection is closed.
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169
170.. method:: Telnet.interact()
171
172 Interaction function, emulates a very dumb Telnet client.
173
174
175.. method:: Telnet.mt_interact()
176
177 Multithreaded version of :meth:`interact`.
178
179
Georg Brandl7f01a132009-09-16 15:58:14 +0000180.. method:: Telnet.expect(list, timeout=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000181
182 Read until one from a list of a regular expressions matches.
183
184 The first argument is a list of regular expressions, either compiled
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000185 (:class:`re.RegexObject` instances) or uncompiled (byte strings). The
186 optional second argument is a timeout, in seconds; the default is to block
187 indefinitely.
Georg Brandl116aa622007-08-15 14:28:22 +0000188
189 Return a tuple of three items: the index in the list of the first regular
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000190 expression that matches; the match object returned; and the bytes read up
191 till and including the match.
Georg Brandl116aa622007-08-15 14:28:22 +0000192
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000193 If end of file is found and no bytes were read, raise :exc:`EOFError`.
194 Otherwise, when nothing matches, return ``(-1, None, data)`` where *data* is
195 the bytes received so far (may be empty bytes if a timeout happened).
Georg Brandl116aa622007-08-15 14:28:22 +0000196
197 If a regular expression ends with a greedy match (such as ``.*``) or if more
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000198 than one expression can match the same input, the results are
199 indeterministic, and may depend on the I/O timing.
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201
202.. method:: Telnet.set_option_negotiation_callback(callback)
203
204 Each time a telnet option is read on the input flow, this *callback* (if set) is
205 called with the following parameters : callback(telnet socket, command
206 (DO/DONT/WILL/WONT), option). No other action is done afterwards by telnetlib.
207
208
209.. _telnet-example:
210
211Telnet Example
212--------------
213
214.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
215
216
217A simple example illustrating typical use::
218
219 import getpass
Georg Brandl116aa622007-08-15 14:28:22 +0000220 import telnetlib
221
Georg Brandl116aa622007-08-15 14:28:22 +0000222 HOST = "localhost"
Georg Brandl8d5c3922007-12-02 22:48:17 +0000223 user = input("Enter your remote account: ")
Georg Brandl116aa622007-08-15 14:28:22 +0000224 password = getpass.getpass()
225
226 tn = telnetlib.Telnet(HOST)
227
Benjamin Peterson3de7fb82008-10-15 20:54:24 +0000228 tn.read_until(b"login: ")
229 tn.write(user.encode('ascii') + b"\n")
Georg Brandl116aa622007-08-15 14:28:22 +0000230 if password:
Benjamin Peterson3de7fb82008-10-15 20:54:24 +0000231 tn.read_until(b"Password: ")
232 tn.write(password.encode('ascii') + b"\n")
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Benjamin Peterson3de7fb82008-10-15 20:54:24 +0000234 tn.write(b"ls\n")
235 tn.write(b"exit\n")
Georg Brandl116aa622007-08-15 14:28:22 +0000236
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000237 print(tn.read_all().decode('ascii'))
Georg Brandl116aa622007-08-15 14:28:22 +0000238