blob: 134ddb68f80671912c98e28e8d972b1a7b075b52 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`telnetlib` --- Telnet client
3==================================
4
5.. module:: telnetlib
6 :synopsis: Telnet client class.
Christian Heimes895627f2007-12-08 17:28:33 +00007.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl116aa622007-08-15 14:28:22 +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
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000030 establish a connection. Alternatively, the host name and optional port
Georg Brandlf78e02b2008-06-10 17:40:04 +000031 and timeout can be passed to the constructor, 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 the connection attempt (if
34 not specified, the global default timeout setting will be used).
35
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000036 number can be passed to the constructor, to, in which case the connection to
37 the server will be established before the constructor returns. The optional
38 *timeout* parameter specifies a timeout in seconds for blocking operations
39 like the connection attempt (if not specified, or passed as None, the global
40 default timeout setting will be used).
Georg Brandl116aa622007-08-15 14:28:22 +000041
42 Do not reopen an already connected instance.
43
44 This class has many :meth:`read_\*` methods. Note that some of them raise
45 :exc:`EOFError` when the end of the connection is read, because they can return
46 an empty string for other reasons. See the individual descriptions below.
47
Georg Brandl116aa622007-08-15 14:28:22 +000048
49.. seealso::
50
51 :rfc:`854` - Telnet Protocol Specification
52 Definition of the Telnet protocol.
53
54
55.. _telnet-objects:
56
57Telnet Objects
58--------------
59
60:class:`Telnet` instances have the following methods:
61
62
63.. method:: Telnet.read_until(expected[, timeout])
64
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000065 Read until a given byte string, *expected*, is encountered or until *timeout*
66 seconds have passed.
Georg Brandl116aa622007-08-15 14:28:22 +000067
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000068 When no match is found, return whatever is available instead, possibly empty
69 bytes. Raise :exc:`EOFError` if the connection is closed and no cooked data
70 is available.
Georg Brandl116aa622007-08-15 14:28:22 +000071
72
73.. method:: Telnet.read_all()
74
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000075 Read all data until EOF as bytes; block until connection closed.
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
78.. method:: Telnet.read_some()
79
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000080 Read at least one byte of cooked data unless EOF is hit. Return ``b''`` if
81 EOF is hit. Block if no data is immediately available.
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
84.. method:: Telnet.read_very_eager()
85
86 Read everything that can be without blocking in I/O (eager).
87
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000088 Raise :exc:`EOFError` if connection closed and no cooked data available.
89 Return ``b''`` if no cooked data available otherwise. Do not block unless in
90 the midst of an IAC sequence.
Georg Brandl116aa622007-08-15 14:28:22 +000091
92
93.. method:: Telnet.read_eager()
94
95 Read readily available data.
96
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +000097 Raise :exc:`EOFError` if connection closed and no cooked data available.
98 Return ``b''`` if no cooked data available otherwise. Do not block unless in
99 the midst of an IAC sequence.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101
102.. method:: Telnet.read_lazy()
103
104 Process and return data already in the queues (lazy).
105
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000106 Raise :exc:`EOFError` if connection closed and no data available. Return
107 ``b''`` if no cooked data available otherwise. Do not block unless in the
108 midst of an IAC sequence.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110
111.. method:: Telnet.read_very_lazy()
112
113 Return any data available in the cooked queue (very lazy).
114
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000115 Raise :exc:`EOFError` if connection closed and no data available. Return
116 ``b''`` if no cooked data available otherwise. This method never blocks.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
119.. method:: Telnet.read_sb_data()
120
121 Return the data collected between a SB/SE pair (suboption begin/end). The
122 callback should access these data when it was invoked with a ``SE`` command.
123 This method never blocks.
124
Georg Brandl116aa622007-08-15 14:28:22 +0000125
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
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000130 specifies a timeout in seconds for blocking operations like the connection
Georg Brandlf78e02b2008-06-10 17:40:04 +0000131 attempt (if not specified, the global default timeout setting will be used).
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133 Do not try to reopen an already connected instance.
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136.. method:: Telnet.msg(msg[, *args])
137
138 Print a debug message when the debug level is ``>`` 0. If extra arguments are
139 present, they are substituted in the message using the standard string
140 formatting operator.
141
142
143.. method:: Telnet.set_debuglevel(debuglevel)
144
145 Set the debug level. The higher the value of *debuglevel*, the more debug
146 output you get (on ``sys.stdout``).
147
148
149.. method:: Telnet.close()
150
151 Close the connection.
152
153
154.. method:: Telnet.get_socket()
155
156 Return the socket object used internally.
157
158
159.. method:: Telnet.fileno()
160
161 Return the file descriptor of the socket object used internally.
162
163
164.. method:: Telnet.write(buffer)
165
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000166 Write a byte string to the socket, doubling any IAC characters. This can
167 block if the connection is blocked. May raise :exc:`socket.error` if the
168 connection is closed.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170
171.. method:: Telnet.interact()
172
173 Interaction function, emulates a very dumb Telnet client.
174
175
176.. method:: Telnet.mt_interact()
177
178 Multithreaded version of :meth:`interact`.
179
180
181.. method:: Telnet.expect(list[, timeout])
182
183 Read until one from a list of a regular expressions matches.
184
185 The first argument is a list of regular expressions, either compiled
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000186 (:class:`re.RegexObject` instances) or uncompiled (byte strings). The
187 optional second argument is a timeout, in seconds; the default is to block
188 indefinitely.
Georg Brandl116aa622007-08-15 14:28:22 +0000189
190 Return a tuple of three items: the index in the list of the first regular
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000191 expression that matches; the match object returned; and the bytes read up
192 till and including the match.
Georg Brandl116aa622007-08-15 14:28:22 +0000193
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000194 If end of file is found and no bytes were read, raise :exc:`EOFError`.
195 Otherwise, when nothing matches, return ``(-1, None, data)`` where *data* is
196 the bytes received so far (may be empty bytes if a timeout happened).
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198 If a regular expression ends with a greedy match (such as ``.*``) or if more
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000199 than one expression can match the same input, the results are
200 indeterministic, and may depend on the I/O timing.
Georg Brandl116aa622007-08-15 14:28:22 +0000201
202
203.. method:: Telnet.set_option_negotiation_callback(callback)
204
205 Each time a telnet option is read on the input flow, this *callback* (if set) is
206 called with the following parameters : callback(telnet socket, command
207 (DO/DONT/WILL/WONT), option). No other action is done afterwards by telnetlib.
208
209
210.. _telnet-example:
211
212Telnet Example
213--------------
214
215.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
216
217
218A simple example illustrating typical use::
219
220 import getpass
Georg Brandl116aa622007-08-15 14:28:22 +0000221 import telnetlib
222
Georg Brandl116aa622007-08-15 14:28:22 +0000223 HOST = "localhost"
Georg Brandl8d5c3922007-12-02 22:48:17 +0000224 user = input("Enter your remote account: ")
Georg Brandl116aa622007-08-15 14:28:22 +0000225 password = getpass.getpass()
226
227 tn = telnetlib.Telnet(HOST)
228
Benjamin Peterson3de7fb82008-10-15 20:54:24 +0000229 tn.read_until(b"login: ")
230 tn.write(user.encode('ascii') + b"\n")
Georg Brandl116aa622007-08-15 14:28:22 +0000231 if password:
Benjamin Peterson3de7fb82008-10-15 20:54:24 +0000232 tn.read_until(b"Password: ")
233 tn.write(password.encode('ascii') + b"\n")
Georg Brandl116aa622007-08-15 14:28:22 +0000234
Benjamin Peterson3de7fb82008-10-15 20:54:24 +0000235 tn.write(b"ls\n")
236 tn.write(b"exit\n")
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Benjamin Petersonaaebe1c2008-10-15 22:28:54 +0000238 print(tn.read_all().decode('ascii'))
Georg Brandl116aa622007-08-15 14:28:22 +0000239