<s> """<STR_LIT>""" <EOL> from twisted . protocols import telnet <EOL> from twisted . internet import protocol <EOL> from twisted . python import log , failure <EOL> import string , copy , sys <EOL> from cStringIO import StringIO <EOL> class Shell ( telnet . Telnet ) : <EOL> """<STR_LIT>""" <EOL> def connectionMade ( self ) : <EOL> telnet . Telnet . connectionMade ( self ) <EOL> self . lineBuffer = [ ] <EOL> def loggedIn ( self ) : <EOL> self . transport . write ( "<STR_LIT>" ) <EOL> def checkUserAndPass ( self , username , password ) : <EOL> return ( ( self . factory . username == username ) and ( password == self . factory . password ) ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . transport . write ( data ) <EOL> def telnet_Command ( self , cmd ) : <EOL> if self . lineBuffer : <EOL> if not cmd : <EOL> cmd = string . join ( self . lineBuffer , '<STR_LIT:\n>' ) + '<STR_LIT>' <EOL> self . doCommand ( cmd ) <EOL> self . lineBuffer = [ ] <EOL> return "<STR_LIT>" <EOL> else : <EOL> self . lineBuffer . append ( cmd ) <EOL> self . transport . write ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> else : <EOL> self . doCommand ( cmd ) <EOL> return "<STR_LIT>" <EOL> def doCommand ( self , cmd ) : <EOL> fn = '<STR_LIT>' <EOL> result = None <EOL> try : <EOL> out = sys . stdout <EOL> sys . stdout = self <EOL> try : <EOL> code = compile ( cmd , fn , '<STR_LIT>' ) <EOL> result = eval ( code , self . factory . namespace ) <EOL> except : <EOL> try : <EOL> code = compile ( cmd , fn , '<STR_LIT>' ) <EOL> exec code in self . factory . namespace <EOL> except SyntaxError , e : <EOL> if not self . lineBuffer and str ( e ) [ : <NUM_LIT> ] == "<STR_LIT>" : <EOL> self . lineBuffer . append ( cmd ) <EOL> self . transport . write ( "<STR_LIT>" ) <EOL> return <EOL> else : <EOL> failure . Failure ( ) . printTraceback ( file = self ) <EOL> log . deferr ( ) <EOL> self . write ( '<STR_LIT>' ) <EOL> return <EOL> except : <EOL> io = StringIO ( ) <EOL> failure . Failure ( ) . printTraceback ( file = self ) <EOL> log . deferr ( ) <EOL> self . write ( '<STR_LIT>' ) <EOL> return <EOL> finally : <EOL> sys . stdout = out <EOL> self . factory . namespace [ '<STR_LIT:_>' ] = result <EOL> if result is not None : <EOL> self . transport . write ( repr ( result ) ) <EOL> self . transport . write ( '<STR_LIT:\r\n>' ) <EOL> self . transport . write ( "<STR_LIT>" ) <EOL> class ShellFactory ( protocol . Factory ) : <EOL> username = "<STR_LIT>" <EOL> password = "<STR_LIT>" <EOL> protocol = Shell <EOL> service = None <EOL> def __init__ ( self ) : <EOL> self . namespace = { <EOL> '<STR_LIT>' : self , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:_>' : None <EOL> } <EOL> def setService ( self , service ) : <EOL> self . namespace [ '<STR_LIT>' ] = self . service = service <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> dict = self . __dict__ <EOL> ns = copy . copy ( dict [ '<STR_LIT>' ] ) <EOL> dict [ '<STR_LIT>' ] = ns <EOL> if ns . has_key ( '<STR_LIT>' ) : <EOL> del ns [ '<STR_LIT>' ] <EOL> return dict </s>
