Qf&[dZddlZddlZddlZddlmZddlZeje ddgZ dZ dZ e dgZe d gZe d gZe d gZe d gZe dgZe d gZe dgZe dgZe dgZe dgZe dgZe dgZe dgZe dgZe dgZe dgZe dgZe dgZ e dgZ!e dgZ"e dgZ#e dgZ$e dgZ%e dgZ&e dgZ'e d gZ(e d!gZ)e d"gZ*e d#gZ+e d$gZ,e d%gZ-e d&gZ.e d'gZ/e d(gZ0e d)gZ1e d*gZ2e d+gZ3e d,gZ4e d-gZ5e dgZ6e d.gZ7e d/gZ8e d0gZ9e d1gZ:e d2gZ;e d3gZ<e d4gZ=e d5gZ>e d6gZ?e d7gZ@e d8gZAe d9gZBe d:gZCe d;gZDe dgZGe d?gZHe d@gZIe dAgZJe dBgZKe dCgZLe dDgZMe dEgZNe dFgZOe dGgZPe dHgZQe dIgZRe dJgZSe dgZTe dgZUeVedKr ejZXn ejZXGdLdZZdMZ[e dNk(re[yy)OaQTELNET 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) )removeTelnetr r !"#$%&'()*+,-./01 PollSelectorceZdZdZdZddej fdZdej fdZdZ dZ dZ d Z d Z d Zd Zd d ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZd dZ dZ!dZ"y)!raTelnet interface class. An instance of this class represents a connection to a telnet server. The instance is initially not connected; the open() method must be used to establish a connection. Alternatively, the host name and optional port number can be passed to the constructor, too. Don't try to reopen an already connected instance. This class has many read_*() methods. Note that some of them raise EOFError when the end of the connection is read, because they can return an empty string for other reasons. See the individual doc strings. read_until(expected, [timeout]) Read until the expected string has been seen, or a timeout is hit (default is no timeout); may block. read_all() Read all data until EOF; may block. read_some() Read at least one byte or EOF; may block. read_very_eager() Read all data available already queued or on the socket, without blocking. read_eager() Read either data already queued or some data available on the socket, without blocking. read_lazy() Read all data in the raw queue (processing it first), without doing any socket I/O. read_very_lazy() Reads all data in the cooked queue, without doing any socket I/O. read_sb_data() Reads available data between SB ... SE sequence. Don't block. set_option_negotiation_callback(callback) Each time a telnet option is read on the input flow, this callback (if set) is called with the following parameters : callback(telnet socket, command, option) option will be chr(0) when there is no option. No other action is done afterwards by telnetlib. Nrct|_||_||_||_d|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ ||j|||yy)zConstructor. When called without arguments, create an unconnected instance. With a hostname argument, it connects the instance; port number and timeout are optional. Nr) DEBUGLEVEL debuglevelhostporttimeoutsockrawqirawqcookedqeofiacseqsbsbdataqoption_callbackopenselfrPrQrRs 0/opt/alt/python312/lib64/python3.12/telnetlib.py__init__zTelnet.__init__s{%         #   IIdD' * rMcd|_|st}||_||_||_t j d|||tj||f||_ y)zConnect to a host. The optional second argument is the port number, which defaults to the standard telnet port (23). Don't try to reopen an already connected instance. rztelnetlib.Telnet.openN) rW TELNET_PORTrPrQrRsysauditsocketcreate_connectionrSr]s r_r\z Telnet.opensSD    )4t<,,dD\7C rMc$|jy)z#Destructor -- close the connection.Ncloser^s r___del__zTelnet.__del__s  rMc|jdkDrEtd|jd|jdd|rt||zyt|yy)zPrint a debug message, when the debug level is > 0. If extra arguments are present, they are substituted in the message using the standard string formatting operator. rzTelnet(,z): )endN)rOprintrPrQ)r^msgargss r_rqz Telnet.msgsB ??Q  dii; EcDj!c rMc||_y)zhSet the debug level. The higher it is, the more debug output you get (on sys.stdout). N)rO)r^rOs r_set_debuglevelzTelnet.set_debuglevels %rMcz|j}d|_d|_d|_d|_|r|j yy)zClose the connection.NTrMr)rSrWrXrYri)r^rSs r_riz Telnet.close s8yy    JJL rMc|jS)z)Return the socket object used internally.)rSrjs r_ get_socketzTelnet.get_sockets yyrMc6|jjS)z9Return the fileno() of the socket object used internally.)rSfilenorjs r_ryz Telnet.filenosyy!!rMct|vr!|jtttz}tjd|||j d||j j |y)zWrite 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)IACreplacercrdrqrSsendall)r^buffers r_writez Telnet.writesP &=^^CS1F *D&9 F# &!rMcPt|}|j|jj|}|dk\r*||z}|jd|}|j|d|_|S| t |z}t 5}|j |tj|js|j|rtdt|j|z }|j|j|jj||}|dk\r3||z}|jd|}|j|d|_|cdddS|t z }|dkrn |jsddd|jS#1swY|jSxYw)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_rawqrVfind_time_TelnetSelectorregister selectors EVENT_READrWselectmax fill_rawqread_very_lazy)r^matchrRnibufdeadlineselectors r_ read_untilzTelnet.read_until*s J  LL  e $ 6!A,,r"C<<+DLJ  w(H  (   dI$8$8 9hh??7+As4<<023ANN$%%' ))%3AAvaC"ll2A.'+||AB'7 "  &&0G{hh"""$$#"""$$s?C F !F  F%c|j|js-|j|j|js-|j}d|_|S)z7Read all data until EOF; block until connection closed.rM)rrWrrVr^rs r_read_allzTelnet.read_allOsK (( NN     ((ll  rMc|j|jsE|js9|j|j|js |js9|j}d|_|S)zRead at least one byte of cooked data unless EOF is hit. Return b'' if EOF is hit. Block if no data is immediately available. rM)rrVrWrrs r_ read_somezTelnet.read_someYsY ,,txx NN     ,,txxll  rMc|j|jsM|jr=|j|j|js|jr=|j 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. )rrW sock_availrrrjs r_read_very_eagerzTelnet.read_very_eagerhsY ((t0 NN     ((t0""$$rMc$|j|jse|jsY|jrI|j |j|js|js|jrI|j S)zRead 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. )rrVrWrrrrjs r_ read_eagerzTelnet.read_eagervse ,,txxDOO4E NN     ,,txxDOO4E""$$rMcB|j|jS)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. )rrrjs r_ read_lazyzTelnet.read_lazys ""$$rMcv|j}d|_|s#|jr|js td|S)zReturn 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. rMztelnet connection closed)rVrWrTEOFErrorrs r_rzTelnet.read_very_lazys4ll txx 56 6 rMc,|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. rM)rZrs r_ read_sb_datazTelnet.read_sb_datasll  rMc||_y)zIProvide a callback function called after each receipt of a telnet option.N)r[)r^callbacks r_set_option_negotiation_callbackz&Telnet.set_option_negotiation_callbacks 'rMcXddg} |jr|j}|jsP|tk(r3|dk(r9|tk7r ||j |z||j <b|xj|z c_nSt |jdk(r|ttttfvr|xj|z c_d|_|tk(r!||j |z||j <n|tk(rd|_d|_ n,|tk(r#d|_|j|dz|_ d|d<|jr#|j|j|t ni|j#dt%|znJt |jdk(r1|jdd}d|_|}|ttfvr|j#d|tk(xrdxsd t%||jr|j|j||n|jj'ttz|zn|ttfvr~|j#d|tk(xrd xsd t%||jr|j|j||n)|jj'ttz|z|jr|j*|dz|_|j|dz|_ y #t($rd|_d|_YHwxYw) zTransfer from raw queue to cooked queue. Set self.eof when connection is closed. Don't block unless in the midst of an IAC sequence. rMrrzIAC %d not recognizedrz IAC %s %dDODONTWILLWONTN)rT rawq_getcharrXtheNULLr{rYrrrrrSBrZSEr[rSNOOPTrqordr}rrV)r^rccmdopts r_rzTelnet.process_rawqsCj; ))%%'{{G| G| Cx'*477|a'7DGG   q( %*RtT22 q(  "%DKCx'*477|a'7DGG 7&'DG+.DL"W&'DG+/<<#a&+@DL%(CF//!00AuE !HH%?t ,4K2F?o))v||c!f, ||c!f,  DKDG sKLL)(L)c6|js"|j|jrt|j|j|jdz}|jdz|_|jt |jk\rd|_d|_|S)zGet next char from raw queue. Block if no data is immediately available. Raise EOFError when connection is closed. rrMr)rTrrWrrUr)r^rs r_rzTelnet.rawq_getcharssyy NN xx IIdjjA .ZZ!^ ::TYY 'DIDJrMc|jt|jk\rd|_d|_|jj d}|j d|| |_|j|z|_y)zFill raw queue from exactly one recv() system call. Block if no data is immediately available. Set self.eof when connection is closed. rMr2zrecv %rN)rUrrTrSrecvrqrWrs r_rzTelnet.fill_rawqs` ::TYY 'DIDJiinnR  C GIIO rMct5}|j|tjt |j dcdddS#1swYyxYw)z-Test whether data is available on the socket.rN)rrrrboolr)r^rs r_rzTelnet.sock_availsB  (   dI$8$8 9*+ , ,  s :AAc:tjdk(r|jyt5}|j |t j |j tjt j  |jD]\}}|j|ura |j}|s(tjj|jdtjj!u|jtjustjj#j%d}|s dddy|j|#t$rtdYdddywxYw#1swYyxYw)z9Interaction function, emulates a very dumb telnet client.win32N(*** Connection closed by remote host ***ascii)rcplatform mt_interactrrrrstdinrfileobjrrrpstdoutrdecodeflushreadlineencode)r^rkeyeventstextlines r_interactzTelnet.interacts: <<7 "       (   dI$8$8 9   cii)=)= >#+??#4KC{{d*##'??#4D JJ,,T[[-ABJJ,,. 1"yy113::7C#"% ) )& 4($5 (#!"LM" ) )# sCA4F$E.4F7A)F!0FF.FF FFFcddl}|j|jd tjj }|sy|j |jdB)z$Multithreaded version of interact().rNr)_threadstart_new_threadlistenerrcrrrr)r^rrs r_rzTelnet.mt_interact6sN  399%%'D JJt{{7+ , rMc |j}|r/tjj |j dntjja#t$rtdYywxYw)z>Helper for mt_interact() -- this executes in the other thread.rNr)rrrprcrrrr)r^datas r_rzTelnet.listener@sh (   W!56   " @A sA##A:9A:c&d}|dd}tt|}|D]/}t||dr|sddl}|j ||||<1| t |z}t 5}|j|tj|js|j|D]e}||j|j}|s$|j} |jd| } |j| d|_ ||| fccdddS|'|j|} t z }| s|dkrn|j!|jsddd|j#} | s|jrt$dd| fS#1swY2xYw)aRead 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. Nsearchr)rangerhasattrrecompilerrrrrrWrrrVrorrrr) r^listrRrindicesrrrmerreadys r_expectz Telnet.expectMs|,AwD "A47H-9**T!W-Q  w(H  (   dI$8$8 9hh!!# AQt||4AEEG#||BQ/'+||AB'7 !1d|+ ! !!&$OOG4E&0G "Q;!$ #hh(""$ND$/ s)A"F 9FAFFc|SNrrjs r_ __enter__zTelnet.__enter__s rMc$|jyrrh)r^typevalue tracebacks r___exit__zTelnet.__exit__s  rMr)#__name__ __module__ __qualname____doc__rSre_GLOBAL_DEFAULT_TIMEOUTr`r\rkrqrtrirwryrrrrrrrrrrrrrrrrrrrrrrMr_rrs3h D q77+0 )G)GD" %" "#%J  % % %  (E-N$$", )4- #6 prMcd}tjddrVtjddk(r@|dz}tjd=tjddrtjddk(r@d}tjddrtjd}d}tjddrtjd} t|}t 5}|j||j||d |jdddy#t$rt j |d}YjwxYw#1swYyxYw) zTest program for telnetlib. Usage: python telnetlib.py [-d] ... [host [port]] Default host is localhost; default port is 23. rrNz-d localhostrtcpg?)rR) rcargvint ValueErrorre getservbynamerrtr\r)rOrPrQportstrtns r_testrsJ ((12,388A;$.\ HHQK ((12,388A;$. D xx|xx{ D xx|((1+ 8wr?sF #Xg. *   cU| cU| cU| cU| cU| * SEl SEl SEl SEl SEl SEl SEl SEl SEl SEl SEl s aSz QCj QCj aSz s A3Z aSz aSz aSz t t t t t t t t t B4[ RDk tbT{ t rd  RDk bT{ t tbT{  rd  bT{ t rd  "; ";RDk t +RDk + rd  + RDk+RDk RDk t "; 2$K cU| C5\ #< se  qc   9n%,,O..Oyyx6 zFrM