Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 1 | \section{\module{telnetlib} --- |
| 2 | Telnet client} |
| 3 | |
| 4 | \declaremodule{standard}{telnetlib} |
| 5 | \modulesynopsis{Telnet client class.} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 6 | \sectionauthor{Skip Montanaro}{skip@mojam.com} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 7 | |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 8 | \index{protocol!Telnet} |
| 9 | |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 10 | The \module{telnetlib} module provides a \class{Telnet} class that |
| 11 | implements the Telnet protocol. See \rfc{854} for details about the |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 12 | protocol. In addition, it provides symbolic constants for the protocol |
Martin v. Löwis | 22610da | 2002-11-04 17:41:18 +0000 | [diff] [blame] | 13 | characters (see below), and for the telnet options. The |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 14 | symbolic names of the telnet options follow the definitions in |
| 15 | \code{arpa/telnet.h}, with the leading \code{TELOPT_} removed. For |
| 16 | symbolic names of options which are traditionally not included in |
| 17 | \code{arpa/telnet.h}, see the module source itself. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 18 | |
Martin v. Löwis | 22610da | 2002-11-04 17:41:18 +0000 | [diff] [blame] | 19 | The symbolic constants for the telnet commands are: IAC, DONT, DO, |
| 20 | WONT, WILL, SE (Subnegotiation End), NOP (No Operation), DM (Data |
| 21 | Mark), BRK (Break), IP (Interrupt process), AO (Abort output), AYT |
| 22 | (Are You There), EC (Erase Character), EL (Erase Line), GA (Go Ahead), |
| 23 | SB (Subnegotiation Begin). |
| 24 | |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 26 | \begin{classdesc}{Telnet}{\optional{host\optional{, port\optional{, timeout}}}} |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 27 | \class{Telnet} represents a connection to a Telnet server. The |
Fred Drake | ae08853 | 2000-05-03 15:11:47 +0000 | [diff] [blame] | 28 | instance is initially not connected by default; the \method{open()} |
| 29 | method must be used to establish a connection. Alternatively, the |
| 30 | host name and optional port number can be passed to the constructor, |
| 31 | to, in which case the connection to the server will be established |
| 32 | before the constructor returns. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 33 | The optional \var{timeout} parameter specifies a timeout in seconds for the |
| 34 | connection attempt (if not specified, or passed as None, the global default |
| 35 | timeout setting will be used). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 36 | |
| 37 | Do not reopen an already connected instance. |
| 38 | |
| 39 | This class has many \method{read_*()} methods. Note that some of them |
| 40 | raise \exception{EOFError} when the end of the connection is read, |
| 41 | because they can return an empty string for other reasons. See the |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 42 | individual descriptions below. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 43 | \end{classdesc} |
| 44 | |
| 45 | |
Fred Drake | ac308d0 | 2000-04-26 18:20:04 +0000 | [diff] [blame] | 46 | \begin{seealso} |
| 47 | \seerfc{854}{Telnet Protocol Specification}{ |
| 48 | Definition of the Telnet protocol.} |
| 49 | \end{seealso} |
| 50 | |
| 51 | |
| 52 | |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 53 | \subsection{Telnet Objects \label{telnet-objects}} |
| 54 | |
| 55 | \class{Telnet} instances have the following methods: |
| 56 | |
| 57 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 58 | \begin{methoddesc}[Telnet]{read_until}{expected\optional{, timeout}} |
Fred Drake | 83c19ee | 2003-04-29 13:39:05 +0000 | [diff] [blame] | 59 | Read until a given string, \var{expected}, is encountered or until |
| 60 | \var{timeout} seconds have passed. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 61 | |
| 62 | When no match is found, return whatever is available instead, |
| 63 | possibly the empty string. Raise \exception{EOFError} if the connection |
| 64 | is closed and no cooked data is available. |
| 65 | \end{methoddesc} |
| 66 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 67 | \begin{methoddesc}[Telnet]{read_all}{} |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 68 | Read all data until \EOF; block until connection closed. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 69 | \end{methoddesc} |
| 70 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 71 | \begin{methoddesc}[Telnet]{read_some}{} |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 72 | Read at least one byte of cooked data unless \EOF{} is hit. |
| 73 | Return \code{''} if \EOF{} is hit. Block if no data is immediately |
| 74 | available. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 75 | \end{methoddesc} |
| 76 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 77 | \begin{methoddesc}[Telnet]{read_very_eager}{} |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 78 | Read everything that can be without blocking in I/O (eager). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 79 | |
| 80 | Raise \exception{EOFError} if connection closed and no cooked data |
| 81 | available. Return \code{''} if no cooked data available otherwise. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 82 | Do not block unless in the midst of an IAC sequence. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 83 | \end{methoddesc} |
| 84 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 85 | \begin{methoddesc}[Telnet]{read_eager}{} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 86 | Read readily available data. |
| 87 | |
| 88 | Raise \exception{EOFError} if connection closed and no cooked data |
| 89 | available. Return \code{''} if no cooked data available otherwise. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 90 | Do not block unless in the midst of an IAC sequence. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 91 | \end{methoddesc} |
| 92 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 93 | \begin{methoddesc}[Telnet]{read_lazy}{} |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 94 | Process and return data already in the queues (lazy). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 95 | |
| 96 | Raise \exception{EOFError} if connection closed and no data available. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 97 | Return \code{''} if no cooked data available otherwise. Do not block |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 98 | unless in the midst of an IAC sequence. |
| 99 | \end{methoddesc} |
| 100 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 101 | \begin{methoddesc}[Telnet]{read_very_lazy}{} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 102 | Return any data available in the cooked queue (very lazy). |
| 103 | |
| 104 | Raise \exception{EOFError} if connection closed and no data available. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 105 | Return \code{''} if no cooked data available otherwise. This method |
| 106 | never blocks. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 107 | \end{methoddesc} |
| 108 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 109 | \begin{methoddesc}[Telnet]{read_sb_data}{} |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 110 | Return the data collected between a SB/SE pair (suboption begin/end). |
| 111 | The callback should access these data when it was invoked with a |
| 112 | \code{SE} command. This method never blocks. |
| 113 | |
| 114 | \versionadded{2.3} |
| 115 | \end{methoddesc} |
| 116 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 117 | \begin{methoddesc}[Telnet]{open}{host\optional{, port\optional{, timeout}}} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 118 | Connect to a host. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 119 | The optional second argument is the port number, which |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 120 | defaults to the standard Telnet port (23). |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 121 | The optional \var{timeout} parameter specifies a timeout in seconds for the |
| 122 | connection attempt (if not specified, or passed as None, the global default |
| 123 | timeout setting will be used). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 124 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 125 | Do not try to reopen an already connected instance. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 126 | \end{methoddesc} |
| 127 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 128 | \begin{methoddesc}[Telnet]{msg}{msg\optional{, *args}} |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 129 | Print a debug message when the debug level is \code{>} 0. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 130 | If extra arguments are present, they are substituted in the |
| 131 | message using the standard string formatting operator. |
| 132 | \end{methoddesc} |
| 133 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 134 | \begin{methoddesc}[Telnet]{set_debuglevel}{debuglevel} |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 135 | Set the debug level. The higher the value of \var{debuglevel}, the |
| 136 | more debug output you get (on \code{sys.stdout}). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 137 | \end{methoddesc} |
| 138 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 139 | \begin{methoddesc}[Telnet]{close}{} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 140 | Close the connection. |
| 141 | \end{methoddesc} |
| 142 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 143 | \begin{methoddesc}[Telnet]{get_socket}{} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 144 | Return the socket object used internally. |
| 145 | \end{methoddesc} |
| 146 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 147 | \begin{methoddesc}[Telnet]{fileno}{} |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 148 | Return the file descriptor of the socket object used internally. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 149 | \end{methoddesc} |
| 150 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 151 | \begin{methoddesc}[Telnet]{write}{buffer} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 152 | Write a string to the socket, doubling any IAC characters. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 153 | This can block if the connection is blocked. May raise |
| 154 | \exception{socket.error} if the connection is closed. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 155 | \end{methoddesc} |
| 156 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 157 | \begin{methoddesc}[Telnet]{interact}{} |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 158 | Interaction function, emulates a very dumb Telnet client. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 159 | \end{methoddesc} |
| 160 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 161 | \begin{methoddesc}[Telnet]{mt_interact}{} |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 162 | Multithreaded version of \method{interact()}. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 163 | \end{methoddesc} |
| 164 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 165 | \begin{methoddesc}[Telnet]{expect}{list\optional{, timeout}} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 166 | Read until one from a list of a regular expressions matches. |
| 167 | |
| 168 | The first argument is a list of regular expressions, either |
| 169 | compiled (\class{re.RegexObject} instances) or uncompiled (strings). |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 170 | The optional second argument is a timeout, in seconds; the default |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 171 | is to block indefinitely. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 172 | |
| 173 | Return a tuple of three items: the index in the list of the |
| 174 | first regular expression that matches; the match object |
| 175 | returned; and the text read up till and including the match. |
| 176 | |
| 177 | If end of file is found and no text was read, raise |
| 178 | \exception{EOFError}. Otherwise, when nothing matches, return |
| 179 | \code{(-1, None, \var{text})} where \var{text} is the text received so |
| 180 | far (may be the empty string if a timeout happened). |
| 181 | |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 182 | If a regular expression ends with a greedy match (such as \regexp{.*}) |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 183 | or if more than one expression can match the same input, the |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 184 | results are indeterministic, and may depend on the I/O timing. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 185 | \end{methoddesc} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 187 | \begin{methoddesc}[Telnet]{set_option_negotiation_callback}{callback} |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 188 | Each time a telnet option is read on the input flow, this |
| 189 | \var{callback} (if set) is called with the following parameters : |
| 190 | callback(telnet socket, command (DO/DONT/WILL/WONT), option). No other |
| 191 | action is done afterwards by telnetlib. |
| 192 | \end{methoddesc} |
| 193 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 194 | |
| 195 | \subsection{Telnet Example \label{telnet-example}} |
| 196 | \sectionauthor{Peter Funk}{pf@artcom-gmbh.de} |
| 197 | |
| 198 | A simple example illustrating typical use: |
| 199 | |
| 200 | \begin{verbatim} |
| 201 | import getpass |
| 202 | import sys |
| 203 | import telnetlib |
| 204 | |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 205 | def raw_input(prompt): |
| 206 | sys.stdout.write(prompt) |
| 207 | sys.stdout.flush() |
| 208 | return sys.stdin.readline() |
| 209 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 210 | HOST = "localhost" |
| 211 | user = raw_input("Enter your remote account: ") |
| 212 | password = getpass.getpass() |
| 213 | |
| 214 | tn = telnetlib.Telnet(HOST) |
| 215 | |
| 216 | tn.read_until("login: ") |
| 217 | tn.write(user + "\n") |
| 218 | if password: |
| 219 | tn.read_until("Password: ") |
| 220 | tn.write(password + "\n") |
| 221 | |
| 222 | tn.write("ls\n") |
| 223 | tn.write("exit\n") |
| 224 | |
| 225 | print tn.read_all() |
| 226 | \end{verbatim} |