<s> import re <EOL> import base64 <EOL> import xml . etree . ElementTree as ET <EOL> from winrm . protocol import Protocol <EOL> class Response ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args ) : <EOL> self . std_out , self . std_err , self . status_code = args <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> self . status_code , self . std_out [ : <NUM_LIT:20> ] , self . std_err [ : <NUM_LIT:20> ] ) <EOL> class Session ( object ) : <EOL> def __init__ ( self , target , auth , transport = '<STR_LIT>' ) : <EOL> username , password = auth <EOL> self . url = self . _build_url ( target , transport ) <EOL> self . protocol = Protocol ( self . url , transport = transport , <EOL> username = username , password = password ) <EOL> def run_cmd ( self , command , args = ( ) ) : <EOL> shell_id = self . protocol . open_shell ( ) <EOL> command_id = self . protocol . run_command ( shell_id , command , args ) <EOL> rs = Response ( self . protocol . get_command_output ( shell_id , command_id ) ) <EOL> self . protocol . cleanup_command ( shell_id , command_id ) <EOL> self . protocol . close_shell ( shell_id ) <EOL> return rs <EOL> def run_ps ( self , script ) : <EOL> """<STR_LIT>""" <EOL> base64_script = base64 . b64encode ( script . encode ( "<STR_LIT>" ) ) <EOL> rs = self . run_cmd ( "<STR_LIT>" % ( base64_script ) ) <EOL> if len ( rs . std_err ) : <EOL> rs . std_err = self . clean_error_msg ( rs . std_err ) <EOL> return rs <EOL> def clean_error_msg ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if msg . startswith ( "<STR_LIT>" ) : <EOL> msg_xml = msg [ <NUM_LIT:11> : ] <EOL> try : <EOL> msg_xml = self . strip_namespace ( msg_xml ) <EOL> root = ET . fromstring ( msg_xml ) <EOL> nodes = root . findall ( "<STR_LIT>" ) <EOL> new_msg = "<STR_LIT>" <EOL> for s in nodes : <EOL> new_msg += s . text . replace ( "<STR_LIT>" , "<STR_LIT:\n>" ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( e ) ) <EOL> else : <EOL> if len ( new_msg ) : <EOL> msg = new_msg . strip ( ) <EOL> return msg <EOL> def strip_namespace ( self , xml ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> p = re . compile ( "<STR_LIT>" ) <EOL> allmatches = p . finditer ( xml ) <EOL> for match in allmatches : <EOL> xml = xml . replace ( match . group ( ) , "<STR_LIT>" ) <EOL> return xml <EOL> except Exception as e : <EOL> raise Exception ( e ) <EOL> @ staticmethod <EOL> def _build_url ( target , transport ) : <EOL> match = re . match ( <EOL> '<STR_LIT>' , target ) <EOL> scheme = match . group ( '<STR_LIT>' ) <EOL> if not scheme : <EOL> scheme = '<STR_LIT>' if transport == '<STR_LIT>' else '<STR_LIT:http>' <EOL> host = match . group ( '<STR_LIT:host>' ) <EOL> port = match . group ( '<STR_LIT:port>' ) <EOL> if not port : <EOL> port = <NUM_LIT> if transport == '<STR_LIT>' else <NUM_LIT> <EOL> path = match . group ( '<STR_LIT:path>' ) <EOL> if not path : <EOL> path = '<STR_LIT>' <EOL> return '<STR_LIT>' . format ( scheme , host , port , path . lstrip ( '<STR_LIT:/>' ) ) </s>
