o ?OgÖZã@sNdZddlZddlZddlZddlmZdgZdZdZ e dgƒZ e dgƒZ e dgƒZ e d gƒZe d gƒZe dgƒZe d gƒZe d gƒZe d gƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZe dgƒZ e dgƒZ!e dgƒZ"e dgƒZ#e dgƒZ$e dgƒZ%e dgƒZ&e d gƒZ'e d!gƒZ(e d"gƒZ)e d#gƒZ*e d$gƒZ+e d%gƒZ,e d&gƒZ-e d'gƒZ.e d(gƒZ/e d)gƒZ0e d*gƒZ1e d+gƒZ2e dgƒZ3e d,gƒZ4e d-gƒZ5e d.gƒZ6e d/gƒZ7e d0gƒZ8e d1gƒZ9e d2gƒZ:e d3gƒZ;e d4gƒZe d7gƒZ?e d8gƒZ@e d9gƒZAe d:gƒZBe d;gƒZCe dgƒZFe d?gƒZGe d@gƒZHe dAgƒZIe dBgƒZJe dCgƒZKe dDgƒZLe dEgƒZMe dFgƒZNe dGgƒZOe dHgƒZPe dgƒZQe dgƒZReSedIƒrejTZUnejVZUGdJd„dƒZWdKdL„ZXeYdMkr¥eXƒdSdS)NaQTELNET client class. Based on RFC 854: TELNET Protocol Specification, by J. Postel and J. Reynolds Example: >>> from telnetlib import Telnet >>> tn = Telnet('www.python.org', 79) # connect to finger port >>> tn.write(b'guido\r\n') >>> print(tn.read_all()) Login Name TTY Idle When Where guido Guido van Rossum pts/2 snag.cnri.reston.. >>> Note that read_all() won't read until eof -- it just reads some data -- but it guarantees to read at least one byte unless EOF is hit. It is possible to pass a Telnet object to a selector in order to wait until more data is available. Note that in this case, read_eager() may return b'' even if there was data on the socket, because the protocol negotiation may have eaten the data. This is why EOFError is needed in some cases to distinguish between "no data" and "connection closed" (since the socket also appears ready for reading when it is closed). To do: - option negotiation - timeout should be intrinsic to the connection object instead of an option on one of the read calls only éN)Ú monotonicÚTelnetééÿéþéýéüéûéðéñéòéóéôéõéöé÷éøéùéúééééééééé é é é é éééééééééééééééééé é!é"é#é$é%é&é'é(é)é*é+é,é-é.é/é0é1éŠé‹éŒÚ PollSelectorc@seZdZdZddejfdd„Zdejfdd„Zdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zd 0. If extra arguments are present, they are substituted in the message using the standard string formatting operator. rzTelnet(%s,%s):ú )ÚendN)rKÚprintrLrM)rZÚmsgÚargsr[r[r\riñs  ûz Telnet.msgcCó ||_dS)zhSet the debug level. The higher it is, the more debug output you get (on sys.stdout). N)rK)rZrKr[r[r\Úset_debuglevelÿs zTelnet.set_debuglevelcCs2|j}d|_d|_d|_d|_|r| ¡dSdS)zClose the connection.NTrIr)rOrSrTrUrc)rZrOr[r[r\rcs ÿz Telnet.closecCs|jS)z)Return the socket object used internally.)rOrdr[r[r\Ú get_socketszTelnet.get_socketcCs |j ¡S)z9Return the fileno() of the socket object used internally.)rOÚfilenordr[r[r\rnó z Telnet.filenocCsBt|vr | ttt¡}t d||¡| d|¡|j |¡dS)z­Write a string to the socket, doubling any IAC characters. Can block if the connection is blocked. May raise OSError if the connection is closed. ztelnetlib.Telnet.writezsend %rN)ÚIACÚreplacer_r`rirOÚsendall)rZÚbufferr[r[r\Úwrites  z Telnet.writecCsnt|ƒ}| ¡|j |¡}|dkr'||}|jd|…}|j|d…|_|S|dur0tƒ|}tƒx}| |tj¡|j s—|  |¡r|t dt|jƒ|ƒ}|  ¡| ¡|j ||¡}|dkr|||}|jd|…}|j|d…|_|WdƒS|durŠ|tƒ}|dkrŠn|j r>Wdƒ|  ¡SWdƒ|  ¡SWdƒ|  ¡S1s®wY|  ¡S)aRead until a given string is encountered or until timeout. When no match is found, return whatever is available instead, possibly the empty string. Raise EOFError if the connection is closed and no cooked data is available. rN)ÚlenÚ process_rawqrRÚfindÚ_timeÚ_TelnetSelectorÚregisterÚ selectorsÚ EVENT_READrSÚselectÚmaxÚ fill_rawqÚread_very_lazy)rZÚmatchrNÚnÚiÚbufÚdeadlineÚselectorr[r[r\Ú read_until&sN   ô ò þñ þÿ ðïzTelnet.read_untilcCs4| ¡|js| ¡| ¡|jr|j}d|_|S)z7Read all data until EOF; block until connection closed.rI)rvrSrrR©rZr„r[r[r\Úread_allKsþzTelnet.read_allcCs@| ¡|js|js| ¡| ¡|js|jr |j}d|_|S)z™Read at least one byte of cooked data unless EOF is hit. Return b'' if EOF is hit. Block if no data is immediately available. rI)rvrRrSrrˆr[r[r\Ú read_someUs  þzTelnet.read_somecCs<| ¡|js| ¡r| ¡| ¡|js| ¡s | ¡S)a Read everything that's possible without blocking in I/O (eager). Raise EOFError if connection closed and no cooked data available. Return b'' if no cooked data available otherwise. Don't block unless in the midst of an IAC sequence. )rvrSÚ sock_availrr€rdr[r[r\Úread_very_eagerds þzTelnet.read_very_eagercCsH| ¡|js |js | ¡r | ¡| ¡|js |js | ¡s| ¡S)zèRead readily available data. Raise EOFError if connection closed and no cooked data available. Return b'' if no cooked data available otherwise. Don't block unless in the midst of an IAC sequence. )rvrRrSr‹rr€rdr[r[r\Ú read_eagerrs þzTelnet.read_eagercCs| ¡| ¡S)aProcess and return data that's already in the queues (lazy). Raise EOFError if connection closed and no data available. Return b'' if no cooked data available otherwise. Don't block unless in the midst of an IAC sequence. )rvr€rdr[r[r\Ú read_lazy€szTelnet.read_lazycCs(|j}d|_|s|jr|jstdƒ‚|S)zÐReturn any data available in the cooked queue (very lazy). Raise EOFError if connection closed and no data available. Return b'' if no cooked data available otherwise. Don't block. rIztelnet connection closed)rRrSrPÚEOFErrorrˆr[r[r\r€‹s zTelnet.read_very_lazycCs|j}d|_|S)aReturn any data available in the SB ... SE queue. Return b'' if no SB ... SE available. Should only be called after seeing a SB or SE command. When a new SB command is found, old unread SB data will be discarded. Don't block. rI)rVrˆr[r[r\Ú read_sb_data˜szTelnet.read_sb_datacCrk)zIProvide a callback function called after each receipt of a telnet option.N)rW)rZÚcallbackr[r[r\Úset_option_negotiation_callback¤roz&Telnet.set_option_negotiation_callbackcCs<ddg}z÷|jrú| ¡}|js0|tkrq|dkrq|tkr(||j|||j<q|j|7_nÇt|jƒdkr|ttt t fvrG|j|7_qd|_|tkrY||j|||j<nž|t krdd|_d|_ n|t krwd|_|j |d|_ d|d<|jrƒ| |j|t¡nt| dt|ƒ¡njt|jƒdkr÷|jdd…}d|_|}|ttfvrÌ| d|tkr¯dp°d t|ƒ¡|jrÁ| |j||¡n6|j tt |¡n+|t t fvr÷| d|t krÛd pÜd t|ƒ¡|jrí| |j||¡n |j tt|¡|jsWnty d|_d|_Ynw|j|d|_|j |d|_ d S) z Transfer from raw queue to cooked queue. Set self.eof when connection is closed. Don't block unless in the midst of an IAC sequence. rIórrzIAC %d not recognizedrz IAC %s %dÚDOÚDONTÚWILLÚWONTN)rPÚ rawq_getcharrTÚtheNULLrprUrur”r•r–r—ÚSBrVÚSErWrOÚNOOPTriÚordrrrrR)rZr„ÚcÚcmdÚoptr[r[r\rv¨sr ÿ ÿÉ€8ýzTelnet.process_rawqcCsZ|js | ¡|jr t‚|j|j|jd…}|jd|_|jt|jƒkr+d|_d|_|S)zŽGet next char from raw queue. Block if no data is immediately available. Raise EOFError when connection is closed. rrIr)rPrrSrrQru)rZržr[r[r\r˜ðs zTelnet.rawq_getcharcCsL|jt|jƒkrd|_d|_|j d¡}| d|¡| |_|j||_dS)z¢Fill raw queue from exactly one recv() system call. Block if no data is immediately available. Set self.eof when connection is closed. rIré2zrecv %rN)rQrurPrOZrecvrirSrˆr[r[r\rs  zTelnet.fill_rawqcCsFtƒ}| |tj¡t| d¡ƒWdƒS1swYdS)z-Test whether data is available on the socket.rN)ryrzr{r|Úboolr})rZr†r[r[r\r‹s $þzTelnet.sock_availc Cstjdkr | ¡dStƒk}| |tj¡| tjtj¡ | ¡D]T\}}|j |urYz|  ¡}Wnt yGt dƒYWdƒdSw|rXtj  | d¡¡tj  ¡q#|j tjurwtj ¡ d¡}|srWdƒdS| |¡q#q1s|wYdS)z9Interaction function, emulates a very dumb telnet client.Zwin32NTú(*** Connection closed by remote host ***Úascii)r_ÚplatformÚ mt_interactryrzr{r|Ústdinr}ZfileobjrrrhÚstdoutrtÚdecodeÚflushÚreadlineÚencode)rZr†ÚkeyZeventsÚtextÚliner[r[r\Úinteracts:    õ  € î €ñüzTelnet.interactcCs<ddl}| |jd¡ tj ¡}|sdS| | d¡¡q )z$Multithreaded version of interact().rNr[rr¤)Ú_threadÚstart_new_threadÚlistenerr_r§r«rtr¬)rZr±r¯r[r[r\r¦3s üzTelnet.mt_interactcCsP z| ¡}WntytdƒYdSw|r"tj | d¡¡ntj ¡q)z>Helper for mt_interact() -- this executes in the other thread.rr£Nr¤)rrrhr_r¨rtr©rª)rZÚdatar[r[r\r³=s  þ ÷zTelnet.listenerc Cs`d}|dd…}tt|ƒƒ}|D]}t||dƒs(|sddl}| ||¡||<q|dur2tƒ|}tƒc}| |tj ¡|j s‘|  ¡|D]-}||  |j ¡}|rs| ¡} |j d| …} |j | d…|_ ||| fWdƒSqF|durŠ| |¡} |tƒ}| sŠ|dkr‰nq=| ¡|j r@Wdƒn1s›wY| ¡} | s«|j r«t‚dd| fS)a°Read until one from a list of a regular expressions matches. The first argument is a list of regular expressions, either compiled (re.Pattern instances) or uncompiled (strings). The optional second argument is a timeout, in seconds; default is no timeout. Return a tuple of three items: the index in the list of the first regular expression that matches; the re.Match object returned; and the text read up till and including the match. If EOF is read and no text was read, raise EOFError. Otherwise, when nothing matches, return (-1, None, text) where text is the text received so far (may be the empty string if a timeout happened). If a regular expression ends with a greedy match (e.g. '.*') or if more than one expression can match the same input, the results are undeterministic, and may depend on the I/O timing. NÚsearchréÿÿÿÿ)ÚrangeruÚhasattrÚreÚcompilerxryrzr{r|rSrvrµrRrgr}rr€r) rZÚlistrNr¹Úindicesrƒr…r†ÚmÚer®Zreadyr[r[r\ÚexpectJsL   €  ö  ï€þ  z Telnet.expectcCs|S©Nr[rdr[r[r\Ú __enter__‚szTelnet.__enter__cCs | ¡dSrÀrb)rZÚtypeÚvalueÚ tracebackr[r[r\Ú__exit__…s zTelnet.__exit__rÀ)"Ú__name__Ú __module__Ú __qualname__Ú__doc__raZ_GLOBAL_DEFAULT_TIMEOUTr]rXrerirlrcrmrnrtr‡r‰rŠrŒrrŽr€rr’rvr˜rr‹r°r¦r³r¿rÁrÅr[r[r[r\rs@5 ÿ  %    H 8 cCsþd}tjdd…r&tjddkr&|d}tjd=tjdd…r&tjddksd}tjdd…r4tjd}d}tjdd…rXtjd}zt|ƒ}WntyWt |d¡}Ynwtƒ}| |¡|j||dd |  ¡WdƒdS1sxwYdS) zŒTest program for telnetlib. Usage: python telnetlib.py [-d] ... [host [port]] Default host is localhost; default port is 23. rrNz-dZ localhostrZtcpgà?)rN) r_ÚargvÚintÚ ValueErrorraZ getservbynamerrlrXr°)rKrLrMZportstrZtnr[r[r\Útest‰s*þ    ÿ  "ýrÍÚ__main__)ZrÉr_rar{ÚtimerrxÚ__all__rJr^Úbytesrpr•r”r—r–r™r›ZNOPZDMZBRKZIPZAOZAYTZECZELZGAršZBINARYZECHOZRCPZSGAZNAMSZSTATUSZTMZRCTEZNAOLZNAOPZNAOCRDZNAOHTSZNAOHTDZNAOFFDZNAOVTSZNAOVTDZNAOLFDZXASCIIZLOGOUTZBMZDETZSUPDUPZ SUPDUPOUTPUTZSNDLOCZTTYPEZEORZTUIDZOUTMRKZTTYLOCZ VT3270REGIMEZX3PADZNAWSZTSPEEDZLFLOWZLINEMODEZXDISPLOCZ OLD_ENVIRONZAUTHENTICATIONZENCRYPTZ NEW_ENVIRONZTN3270EZXAUTHÚCHARSETZRSPZCOM_PORT_OPTIONZSUPPRESS_LOCAL_ECHOZTLSZKERMITZSEND_URLZ FORWARD_XZ PRAGMA_LOGONZ SSPI_LOGONZPRAGMA_HEARTBEATZEXOPLrœr¸rHryZSelectSelectorrrÍrÆr[r[r[r\Ús¶#                                                                            ÿ