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