#NEWLINE# def expect( self , re_strings = '' , timeout = None ) : #NEWLINE# #INDENT# "\n        This function takes in a regular expression (or regular expressions)\n        that represent the last line of output from the server.  The function\n        waits for one or more of the terms to be matched.  The regexes are\n        matched using expression \n<regex>$ so you'll need to provide an\n        easygoing regex such as '.*server.*' if you wish to have a fuzzy\n        match.\n\n        :param re_strings: Either a regex string or list of regex strings\n                           that we should expect; if this is not specified,\n                           then EOF is expected (i.e. the shell is completely\n                           closed after the exit command is issued)\n        :param timeout: Timeout in seconds.  If this timeout is exceeded,\n                        then an exception is raised.\n        :return: An EOF returns -1, a regex metch returns 0 and a match in a\n                 list of regexes returns the index of the matched string in\n                 the list.\n        :raises: A socket.timeout exception is raised on timeout.\n        " #NEWLINE# timeout = ( timeout if timeout else self . timeout ) #NEWLINE# self . channel . settimeout ( timeout ) #NEWLINE# self . current_output = '' #NEWLINE# if ( isinstance ( re_strings , str ) and ( len ( re_strings ) != 0 ) ) : #NEWLINE# #INDENT# re_strings = [ re_strings ] #NEWLINE# #UNINDENT# while ( ( len ( re_strings ) == 0 ) or ( not [ re_string for re_string in re_strings if re . match ( ( ( '.*\n' + re_string ) + '$' ) , self . current_output , re . DOTALL ) ] ) ) : #NEWLINE# #INDENT# current_buffer = self . channel . recv ( self . buffer_size ) #NEWLINE# if ( len ( current_buffer ) == 0 ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# current_buffer_decoded = current_buffer . decode ( self . encoding ) #NEWLINE# current_buffer_decoded = current_buffer_decoded . replace ( '\r' , '' ) #NEWLINE# if self . display : #NEWLINE# #INDENT# sys . stdout . write ( current_buffer_decoded ) #NEWLINE# sys . stdout . flush ( ) #NEWLINE# #UNINDENT# self . current_output += current_buffer_decoded #NEWLINE# #UNINDENT# if ( len ( re_strings ) != 0 ) : #NEWLINE# #INDENT# found_pattern = [ ( re_index , re_string ) for ( re_index , re_string ) in enumerate ( re_strings ) if re . match ( ( ( '.*\n' + re_string ) + '$' ) , self . current_output , re . DOTALL ) ] #NEWLINE# #UNINDENT# self . current_output_clean = self . current_output #NEWLINE# if ( len ( self . current_send_string ) != 0 ) : #NEWLINE# #INDENT# self . current_output_clean = self . current_output_clean . replace ( ( self . current_send_string + '\n' ) , '' ) #NEWLINE# #UNINDENT# self . current_send_string = '' #NEWLINE# if ( len ( re_strings ) != 0 ) : #NEWLINE# #INDENT# self . current_output_clean = re . sub ( ( found_pattern [ 0 ] [ 1 ] + '$' ) , '' , self . current_output_clean ) #NEWLINE# self . last_match = found_pattern [ 0 ] [ 1 ] #NEWLINE# return found_pattern [ 0 ] [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( - 1 )