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