1
2
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
12 DONT = 254
13 DO = 253
14 WONT = 252
15 WILL = 251
16 SB = 250
17 GA = 249
18 EL = 248
19 EC = 247
20 AYT = 246
21 AO = 245
22 IP = 244
23 BREAK = 243
24 DM = 242
25 NOP = 241
26 SE = 240
27 EOR = 239
28 ABORT = 238
29 SUSP = 237
30 xEOF = 236
31
32 SYNCH = 242
33
34
36 """Return a list of lines and dict of options from telnet data."""
37 l = buf.split(struct.pack("B", IAC))
38
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
48 w = w[2:]
49 elif o == SE:
50
51 w = w[1:]
52 subopt = False
53 elif o == SB:
54
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
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