Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`telnetlib` --- Telnet client |
| 3 | ================================== |
| 4 | |
| 5 | .. module:: telnetlib |
| 6 | :synopsis: Telnet client class. |
Christian Heimes | 895627f | 2007-12-08 17:28:33 +0000 | [diff] [blame] | 7 | .. sectionauthor:: Skip Montanaro <skip@pobox.com> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | .. index:: single: protocol; Telnet |
| 11 | |
| 12 | The :mod:`telnetlib` module provides a :class:`Telnet` class that implements the |
| 13 | Telnet protocol. See :rfc:`854` for details about the protocol. In addition, it |
| 14 | provides symbolic constants for the protocol characters (see below), and for the |
| 15 | telnet options. The symbolic names of the telnet options follow the definitions |
| 16 | in ``arpa/telnet.h``, with the leading ``TELOPT_`` removed. For symbolic names |
| 17 | of options which are traditionally not included in ``arpa/telnet.h``, see the |
| 18 | module source itself. |
| 19 | |
| 20 | The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL, |
| 21 | SE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP |
| 22 | (Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase |
| 23 | Character), 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 Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 30 | establish a connection. Alternatively, the host name and optional port |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 31 | 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 Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 36 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
| 49 | .. seealso:: |
| 50 | |
| 51 | :rfc:`854` - Telnet Protocol Specification |
| 52 | Definition of the Telnet protocol. |
| 53 | |
| 54 | |
| 55 | .. _telnet-objects: |
| 56 | |
| 57 | Telnet Objects |
| 58 | -------------- |
| 59 | |
| 60 | :class:`Telnet` instances have the following methods: |
| 61 | |
| 62 | |
| 63 | .. method:: Telnet.read_until(expected[, timeout]) |
| 64 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 65 | Read until a given byte string, *expected*, is encountered or until *timeout* |
| 66 | seconds have passed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 68 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | .. method:: Telnet.read_all() |
| 74 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 75 | Read all data until EOF as bytes; block until connection closed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | .. method:: Telnet.read_some() |
| 79 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 80 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | .. method:: Telnet.read_very_eager() |
| 85 | |
| 86 | Read everything that can be without blocking in I/O (eager). |
| 87 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 88 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | .. method:: Telnet.read_eager() |
| 94 | |
| 95 | Read readily available data. |
| 96 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 97 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | .. method:: Telnet.read_lazy() |
| 103 | |
| 104 | Process and return data already in the queues (lazy). |
| 105 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 106 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | .. method:: Telnet.read_very_lazy() |
| 112 | |
| 113 | Return any data available in the cooked queue (very lazy). |
| 114 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 115 | Raise :exc:`EOFError` if connection closed and no data available. Return |
| 116 | ``b''`` if no cooked data available otherwise. This method never blocks. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
| 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 Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 130 | specifies a timeout in seconds for blocking operations like the connection |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 131 | attempt (if not specified, the global default timeout setting will be used). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 132 | |
| 133 | Do not try to reopen an already connected instance. |
| 134 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
| 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 Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 166 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 186 | (: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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | |
| 190 | Return a tuple of three items: the index in the list of the first regular |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 191 | expression that matches; the match object returned; and the bytes read up |
| 192 | till and including the match. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 194 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 197 | |
| 198 | If a regular expression ends with a greedy match (such as ``.*``) or if more |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 199 | than one expression can match the same input, the results are |
| 200 | indeterministic, and may depend on the I/O timing. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 201 | |
| 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 | |
| 212 | Telnet Example |
| 213 | -------------- |
| 214 | |
| 215 | .. sectionauthor:: Peter Funk <pf@artcom-gmbh.de> |
| 216 | |
| 217 | |
| 218 | A simple example illustrating typical use:: |
| 219 | |
| 220 | import getpass |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 221 | import telnetlib |
| 222 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | HOST = "localhost" |
Georg Brandl | 8d5c392 | 2007-12-02 22:48:17 +0000 | [diff] [blame] | 224 | user = input("Enter your remote account: ") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | password = getpass.getpass() |
| 226 | |
| 227 | tn = telnetlib.Telnet(HOST) |
| 228 | |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 229 | tn.read_until(b"login: ") |
| 230 | tn.write(user.encode('ascii') + b"\n") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | if password: |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 232 | tn.read_until(b"Password: ") |
| 233 | tn.write(password.encode('ascii') + b"\n") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 235 | tn.write(b"ls\n") |
| 236 | tn.write(b"exit\n") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
Benjamin Peterson | aaebe1c | 2008-10-15 22:28:54 +0000 | [diff] [blame] | 238 | print(tn.read_all().decode('ascii')) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | |