blob: a371e0cb5ed6d8082fa027c6a85665bd5a5c1b42 [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
65 Read until a given string, *expected*, is encountered or until *timeout* seconds
66 have passed.
67
68 When no match is found, return whatever is available instead, possibly the empty
69 string. Raise :exc:`EOFError` if the connection is closed and no cooked data is
70 available.
71
72
73.. method:: Telnet.read_all()
74
75 Read all data until EOF; block until connection closed.
76
77
78.. method:: Telnet.read_some()
79
80 Read at least one byte of cooked data unless EOF is hit. Return ``''`` if EOF is
81 hit. Block if no data is immediately available.
82
83
84.. method:: Telnet.read_very_eager()
85
86 Read everything that can be without blocking in I/O (eager).
87
88 Raise :exc:`EOFError` if connection closed and no cooked data available. Return
89 ``''`` if no cooked data available otherwise. Do not block unless in the midst
90 of an IAC sequence.
91
92
93.. method:: Telnet.read_eager()
94
95 Read readily available data.
96
97 Raise :exc:`EOFError` if connection closed and no cooked data available. Return
98 ``''`` if no cooked data available otherwise. Do not block unless in the midst
99 of an IAC sequence.
100
101
102.. method:: Telnet.read_lazy()
103
104 Process and return data already in the queues (lazy).
105
106 Raise :exc:`EOFError` if connection closed and no data available. Return ``''``
107 if no cooked data available otherwise. Do not block unless in the midst of an
108 IAC sequence.
109
110
111.. method:: Telnet.read_very_lazy()
112
113 Return any data available in the cooked queue (very lazy).
114
115 Raise :exc:`EOFError` if connection closed and no data available. Return ``''``
116 if no cooked data available otherwise. This method never blocks.
117
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
166 Write a string to the socket, doubling any IAC characters. This can block if the
167 connection is blocked. May raise :exc:`socket.error` if the connection is
168 closed.
169
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
186 (:class:`re.RegexObject` instances) or uncompiled (strings). The optional second
187 argument is a timeout, in seconds; the default is to block indefinitely.
188
189 Return a tuple of three items: the index in the list of the first regular
190 expression that matches; the match object returned; and the text read up till
191 and including the match.
192
193 If end of file is found and no text was read, raise :exc:`EOFError`. Otherwise,
194 when nothing matches, return ``(-1, None, text)`` where *text* is the text
195 received so far (may be the empty string if a timeout happened).
196
197 If a regular expression ends with a greedy match (such as ``.*``) or if more
198 than one expression can match the same input, the results are indeterministic,
199 and may depend on the I/O timing.
200
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
Collin Winterc79461b2007-09-01 23:34:30 +0000237 print(tn.read_all())
Georg Brandl116aa622007-08-15 14:28:22 +0000238