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   
 3  """Telnet.""" 
 4   
 5  IAC    = 255    # interpret as command: 
 6  DONT   = 254    # you are not to use option 
 7  DO     = 253    # please, you use option 
 8  WONT   = 252    # I won't use option 
 9  WILL   = 251    # I will use option 
10  SB     = 250    # interpret as subnegotiation 
11  GA     = 249    # you may reverse the line 
12  EL     = 248    # erase the current line 
13  EC     = 247    # erase the current character 
14  AYT    = 246    # are you there 
15  AO     = 245    # abort output--but let prog finish 
16  IP     = 244    # interrupt process--permanently 
17  BREAK  = 243    # break 
18  DM     = 242    # data mark--for connect. cleaning 
19  NOP    = 241    # nop 
20  SE     = 240    # end sub negotiation 
21  EOR    = 239    # end of record (transparent mode) 
22  ABORT  = 238    # Abort process 
23  SUSP   = 237    # Suspend process 
24  xEOF   = 236    # End of file: EOF is already used... 
25   
26  SYNCH  = 242    # for telfunc calls 
27   
28 -def strip_options(buf):
29 """Return a list of lines and dict of options from telnet data.""" 30 l = buf.split(chr(IAC)) 31 #print l 32 b = [] 33 d = {} 34 subopt = False 35 for w in l: 36 if not w: 37 continue 38 o = ord(w[0]) 39 if o > SB: 40 #print 'WILL/WONT/DO/DONT/IAC', `w` 41 w = w[2:] 42 elif o == SE: 43 #print 'SE', `w` 44 w = w[1:] 45 subopt = False 46 elif o == SB: 47 #print 'SB', `w` 48 subopt = True 49 for opt in ('USER', 'DISPLAY', 'TERM'): 50 p = w.find(opt + '\x01') 51 if p != -1: 52 d[opt] = w[p+len(opt)+1:].split('\x00', 1)[0] 53 w = None 54 elif subopt: 55 w = None 56 if w: 57 w = w.replace('\x00', '\n').splitlines() 58 if not w[-1]: w.pop() 59 b.extend(w) 60 return b, d
61 62 if __name__ == '__main__': 63 import unittest 64
65 - class TelnetTestCase(unittest.TestCase):
66 - def test_telnet(self):
67 l = [] 68 s = "\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" 69 l.append(s) 70 s = '\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' 71 l.append(s) 72 s = '\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' 73 l.append(s) 74 exp = [ (['fugly', 'yoda', 'bashtard'], {'USER': 'dugsong', 'DISPLAY': 'doughboy.citi.umich.edu:0.0'}), (['admin', 'foobar', 'enable', 'foobar', '', 'show ip int Vlan 666'], {}), (['werd', 'yoda', 'darthvader'], {'USER': 'dugsong', 'DISPLAY': 'doughboy.citi.umich.edu:0.0'}) ] 75 self.failUnless(map(strip_options, l) == exp)
76 77 unittest.main() 78