Package dpkt :: Module telnet
[hide private]
[frames] | no frames]

Source Code for Module dpkt.telnet

 1  # $Id: telnet.py 23 2006-11-08 15:45:33Z dugsong $ 
 2  # -*- coding: utf-8 -*- 
 3  """Telnet.""" 
 4  from __future__ import print_function 
 5  from __future__ import absolute_import 
 6   
 7  import struct 
 8   
 9  from .compat import compat_ord 
10   
11  IAC = 255  # interpret as command: 
12  DONT = 254  # you are not to use option 
13  DO = 253  # please, you use option 
14  WONT = 252  # I won't use option 
15  WILL = 251  # I will use option 
16  SB = 250  # interpret as subnegotiation 
17  GA = 249  # you may reverse the line 
18  EL = 248  # erase the current line 
19  EC = 247  # erase the current character 
20  AYT = 246  # are you there 
21  AO = 245  # abort output--but let prog finish 
22  IP = 244  # interrupt process--permanently 
23  BREAK = 243  # break 
24  DM = 242  # data mark--for connect. cleaning 
25  NOP = 241  # nop 
26  SE = 240  # end sub negotiation 
27  EOR = 239  # end of record (transparent mode) 
28  ABORT = 238  # Abort process 
29  SUSP = 237  # Suspend process 
30  xEOF = 236  # End of file: EOF is already used... 
31   
32  SYNCH = 242  # for telfunc calls 
33   
34   
35 -def strip_options(buf):
36 """Return a list of lines and dict of options from telnet data.""" 37 l = buf.split(struct.pack("B", IAC)) 38 # print l 39 b = [] 40 d = {} 41 subopt = False 42 for w in l: 43 if not w: 44 continue 45 o = compat_ord(w[0]) 46 if o > SB: 47 # print 'WILL/WONT/DO/DONT/IAC', `w` 48 w = w[2:] 49 elif o == SE: 50 # print 'SE', `w` 51 w = w[1:] 52 subopt = False 53 elif o == SB: 54 # print 'SB', `w` 55 subopt = True 56 for opt in (b'USER', b'DISPLAY', b'TERM'): 57 p = w.find(opt + b'\x01') 58 if p != -1: 59 d[opt] = w[p + len(opt) + 1:].split(b'\x00', 1)[0] 60 w = None 61 elif subopt: 62 w = None 63 if w: 64 w = w.replace(b'\x00', b'\n').splitlines() 65 if not w[-1]: w.pop() 66 b.extend(w) 67 return b, d
68 69
70 -def test_telnet():
71 l = [] 72 s = b"\xff\xfb%\xff\xfa%\x00\x00\x00\xff\xf0\xff\xfd&\xff\xfa&\x05\xff\xf0\xff\xfa&\x01\x01\x02\xff\xf0\xff\xfb\x18\xff\xfb \xff\xfb#\xff\xfb'\xff\xfc$\xff\xfa \x0038400,38400\xff\xf0\xff\xfa#\x00doughboy.citi.umich.edu:0.0\xff\xf0\xff\xfa'\x00\x00DISPLAY\x01doughboy.citi.umich.edu:0.0\x00USER\x01dugsong\xff\xf0\xff\xfa\x18\x00XTERM\xff\xf0\xff\xfd\x03\xff\xfc\x01\xff\xfb\x1f\xff\xfa\x1f\x00P\x00(\xff\xf0\xff\xfd\x05\xff\xfb!\xff\xfd\x01fugly\r\x00yoda\r\x00bashtard\r\x00" 73 l.append(s) 74 s = b'\xff\xfd\x01\xff\xfd\x03\xff\xfb\x18\xff\xfb\x1f\xff\xfa\x1f\x00X\x002\xff\xf0admin\r\x00\xff\xfa\x18\x00LINUX\xff\xf0foobar\r\x00enable\r\x00foobar\r\x00\r\x00show ip int Vlan 666\r\x00' 75 l.append(s) 76 s = b'\xff\xfb%\xff\xfa%\x00\x00\x00\xff\xf0\xff\xfd&\xff\xfa&\x05\xff\xf0\xff\xfa&\x01\x01\x02\xff\xf0\xff\xfb&\xff\xfb\x18\xff\xfb \xff\xfb#\xff\xfb\'\xff\xfc$\xff\xfa \x0038400,38400\xff\xf0\xff\xfa#\x00doughboy.citi.umich.edu:0.0\xff\xf0\xff\xfa\'\x00\x00DISPLAY\x01doughboy.citi.umich.edu:0.0\x00USER\x01dugsong\xff\xf0\xff\xfa\x18\x00XTERM\xff\xf0\xff\xfd\x03\xff\xfc\x01\xff\xfb"\xff\xfa"\x03\x01\x03\x00\x03b\x03\x04\x02\x0f\x05\x00\xff\xff\x07b\x1c\x08\x02\x04\tB\x1a\n\x02\x7f\x0b\x02\x15\x0c\x02\x17\r\x02\x12\x0e\x02\x16\x0f\x02\x11\x10\x02\x13\x11\x00\xff\xff\x12\x00\xff\xff\xff\xf0\xff\xfb\x1f\xff\xfa\x1f\x00P\x00(\xff\xf0\xff\xfd\x05\xff\xfb!\xff\xfa"\x01\x0f\xff\xf0\xff\xfd\x01\xff\xfe\x01\xff\xfa"\x03\x01\x80\x00\xff\xf0\xff\xfd\x01werd\r\n\xff\xfe\x01yoda\r\n\xff\xfd\x01darthvader\r\n\xff\xfe\x01' 77 l.append(s) 78 exp = [([b'fugly', b'yoda', b'bashtard'], {b'USER': b'dugsong', b'DISPLAY': b'doughboy.citi.umich.edu:0.0'}), 79 ([b'admin', b'foobar', b'enable', b'foobar', b'', b'show ip int Vlan 666'], {}), 80 ([b'werd', b'yoda', b'darthvader'], {b'USER': b'dugsong', b'DISPLAY': b'doughboy.citi.umich.edu:0.0'})] 81 assert (list(map(strip_options, l)) == exp)
82 83 84 if __name__ == '__main__': 85 test_telnet() 86 print('Tests Successful...') 87