<s> import base64 <EOL> from datetime import timedelta <EOL> import uuid <EOL> import xml . etree . ElementTree as ET <EOL> from isodate . isoduration import duration_isoformat <EOL> import xmltodict <EOL> from winrm . transport import HttpPlaintext , HttpKerberos , HttpSSL <EOL> class Protocol ( object ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_TIMEOUT = '<STR_LIT>' <EOL> DEFAULT_MAX_ENV_SIZE = <NUM_LIT> <EOL> DEFAULT_LOCALE = '<STR_LIT>' <EOL> def __init__ ( self , endpoint , transport = '<STR_LIT>' , username = None , <EOL> password = None , realm = None , service = None , keytab = None , <EOL> ca_trust_path = None , cert_pem = None , cert_key_pem = None , <EOL> server_cert_validation = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . endpoint = endpoint <EOL> self . timeout = Protocol . DEFAULT_TIMEOUT <EOL> self . max_env_sz = Protocol . DEFAULT_MAX_ENV_SIZE <EOL> self . locale = Protocol . DEFAULT_LOCALE <EOL> if transport == '<STR_LIT>' : <EOL> self . transport = HttpPlaintext ( endpoint , username , password ) <EOL> elif transport == '<STR_LIT>' : <EOL> self . transport = HttpKerberos ( endpoint , server_cert_validation = server_cert_validation ) <EOL> elif transport == '<STR_LIT>' : <EOL> self . transport = HttpSSL ( endpoint , username , password , <EOL> cert_pem = cert_pem , <EOL> cert_key_pem = cert_key_pem , <EOL> server_cert_validation = server_cert_validation ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> self . username = username <EOL> self . password = password <EOL> self . service = service <EOL> self . keytab = keytab <EOL> self . ca_trust_path = ca_trust_path <EOL> def set_timeout ( self , seconds ) : <EOL> """<STR_LIT>""" <EOL> return duration_isoformat ( timedelta ( seconds ) ) <EOL> def open_shell ( self , i_stream = '<STR_LIT>' , o_stream = '<STR_LIT>' , <EOL> working_directory = None , env_vars = None , noprofile = False , <EOL> codepage = <NUM_LIT> , lifetime = None , idle_timeout = None ) : <EOL> """<STR_LIT>""" <EOL> rq = { '<STR_LIT>' : self . _get_soap_header ( <EOL> resource_uri = '<STR_LIT>' , <EOL> action = '<STR_LIT>' ) } <EOL> header = rq [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> header [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( noprofile ) . upper ( ) <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( codepage ) <EOL> } <EOL> ] <EOL> } <EOL> shell = rq [ '<STR_LIT>' ] . setdefault ( <EOL> '<STR_LIT>' , { } ) . setdefault ( '<STR_LIT>' , { } ) <EOL> shell [ '<STR_LIT>' ] = i_stream <EOL> shell [ '<STR_LIT>' ] = o_stream <EOL> if working_directory : <EOL> shell [ '<STR_LIT>' ] = working_directory <EOL> if idle_timeout : <EOL> shell [ '<STR_LIT>' ] = idle_timeout <EOL> if env_vars : <EOL> env = shell . setdefault ( '<STR_LIT>' , { } ) <EOL> for key , value in env_vars . items ( ) : <EOL> env [ '<STR_LIT>' ] = { '<STR_LIT>' : key , '<STR_LIT>' : value } <EOL> rs = self . send_message ( xmltodict . unparse ( rq ) ) <EOL> root = ET . fromstring ( rs ) <EOL> return next ( node for node in root . findall ( '<STR_LIT>' ) <EOL> if node . get ( '<STR_LIT:Name>' ) == '<STR_LIT>' ) . text <EOL> def _get_soap_header ( self , action = None , resource_uri = None , shell_id = None , <EOL> message_id = None ) : <EOL> if not message_id : <EOL> message_id = uuid . uuid4 ( ) <EOL> header = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( message_id ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : resource_uri <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : action <EOL> } <EOL> } <EOL> } <EOL> if shell_id : <EOL> header [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : shell_id <EOL> } <EOL> } <EOL> return header <EOL> def send_message ( self , message ) : <EOL> return self . transport . send_message ( message ) <EOL> def close_shell ( self , shell_id ) : <EOL> """<STR_LIT>""" <EOL> message_id = uuid . uuid4 ( ) <EOL> rq = { '<STR_LIT>' : self . _get_soap_header ( <EOL> resource_uri = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> shell_id = shell_id , <EOL> message_id = message_id ) } <EOL> rq [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , { } ) <EOL> rs = self . send_message ( xmltodict . unparse ( rq ) ) <EOL> root = ET . fromstring ( rs ) <EOL> relates_to = next ( node for node in root . findall ( '<STR_LIT>' ) <EOL> if node . tag . endswith ( '<STR_LIT>' ) ) . text <EOL> assert uuid . UUID ( relates_to . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) == message_id <EOL> def run_command ( self , shell_id , command , arguments = ( ) , <EOL> console_mode_stdin = True , skip_cmd_shell = False ) : <EOL> """<STR_LIT>""" <EOL> rq = { '<STR_LIT>' : self . _get_soap_header ( <EOL> resource_uri = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> shell_id = shell_id ) } <EOL> header = rq [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> header [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( console_mode_stdin ) . upper ( ) <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( skip_cmd_shell ) . upper ( ) <EOL> } <EOL> ] <EOL> } <EOL> cmd_line = rq [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , { } ) . setdefault ( '<STR_LIT>' , { } ) <EOL> cmd_line [ '<STR_LIT>' ] = { '<STR_LIT>' : command } <EOL> if arguments : <EOL> cmd_line [ '<STR_LIT>' ] = '<STR_LIT:U+0020>' . join ( arguments ) <EOL> rs = self . send_message ( xmltodict . unparse ( rq ) ) <EOL> root = ET . fromstring ( rs ) <EOL> command_id = next ( node for node in root . findall ( '<STR_LIT>' ) <EOL> if node . tag . endswith ( '<STR_LIT>' ) ) . text <EOL> return command_id <EOL> def cleanup_command ( self , shell_id , command_id ) : <EOL> """<STR_LIT>""" <EOL> message_id = uuid . uuid4 ( ) <EOL> rq = { '<STR_LIT>' : self . _get_soap_header ( <EOL> resource_uri = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> shell_id = shell_id , <EOL> message_id = message_id ) } <EOL> signal = rq [ '<STR_LIT>' ] . setdefault ( <EOL> '<STR_LIT>' , { } ) . setdefault ( '<STR_LIT>' , { } ) <EOL> signal [ '<STR_LIT>' ] = command_id <EOL> signal [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> rs = self . send_message ( xmltodict . unparse ( rq ) ) <EOL> root = ET . fromstring ( rs ) <EOL> relates_to = next ( node for node in root . findall ( '<STR_LIT>' ) <EOL> if node . tag . endswith ( '<STR_LIT>' ) ) . text <EOL> assert uuid . UUID ( relates_to . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) == message_id <EOL> def get_command_output ( self , shell_id , command_id ) : <EOL> """<STR_LIT>""" <EOL> stdout_buffer , stderr_buffer = [ ] , [ ] <EOL> command_done = False <EOL> while not command_done : <EOL> stdout , stderr , return_code , command_done = self . _raw_get_command_output ( shell_id , command_id ) <EOL> stdout_buffer . append ( stdout ) <EOL> stderr_buffer . append ( stderr ) <EOL> return '<STR_LIT>' . join ( stdout_buffer ) , '<STR_LIT>' . join ( stderr_buffer ) , return_code <EOL> def _raw_get_command_output ( self , shell_id , command_id ) : <EOL> rq = { '<STR_LIT>' : self . _get_soap_header ( <EOL> resource_uri = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> shell_id = shell_id ) } <EOL> stream = rq [ '<STR_LIT>' ] . setdefault ( <EOL> '<STR_LIT>' , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( '<STR_LIT>' , { } ) <EOL> stream [ '<STR_LIT>' ] = command_id <EOL> stream [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> rs = self . send_message ( xmltodict . unparse ( rq ) ) <EOL> root = ET . fromstring ( rs ) <EOL> stream_nodes = [ node for node in root . findall ( '<STR_LIT>' ) <EOL> if node . tag . endswith ( '<STR_LIT>' ) ] <EOL> stdout = stderr = '<STR_LIT>' <EOL> return_code = - <NUM_LIT:1> <EOL> for stream_node in stream_nodes : <EOL> if stream_node . text : <EOL> if stream_node . attrib [ '<STR_LIT:Name>' ] == '<STR_LIT>' : <EOL> stdout += str ( base64 . b64decode ( <EOL> stream_node . text . encode ( '<STR_LIT:ascii>' ) ) ) <EOL> elif stream_node . attrib [ '<STR_LIT:Name>' ] == '<STR_LIT>' : <EOL> stderr += str ( base64 . b64decode ( <EOL> stream_node . text . encode ( '<STR_LIT:ascii>' ) ) ) <EOL> command_done = len ( [ node for node in root . findall ( '<STR_LIT>' ) <EOL> if node . get ( '<STR_LIT>' , '<STR_LIT>' ) . endswith ( <EOL> '<STR_LIT>' ) ] ) == <NUM_LIT:1> <EOL> if command_done : <EOL> return_code = int ( next ( node for node in root . findall ( '<STR_LIT>' ) <EOL> if node . tag . endswith ( '<STR_LIT>' ) ) . text ) <EOL> return stdout , stderr , return_code , command_done </s>
