#! /usr/bin/env mpscript # # Returns the value of a network field in a remote MX server. # import MpNum def parse_nf_string( nf_string ): try: i = nf_string.index( ":" ) except: return ( "localhost", 9727, nf_string ) server_string = nf_string[:i] field_name = nf_string[(i+1):] try: j = server_string.index( "@" ) except: return ( server_string, 9727, field_name ) server_name = server_string[:j] port_number = server_string[(j+1):] port_number = int(port_number) return ( server_name, port_number, field_name ) #-------------------------------------------------------------------------- def find_server( record_list, server_name, port_number ): # First, see if there is already a record in the database for # this server. list_head_record = record_list.list_head_record current_record = list_head_record.next_record while ( current_record != list_head_record ): current_type = current_record.get_field( "mx_type" ) if ( current_type == "tcp_server" ): hostname = current_record.get_field( "hostname" ) port = current_record.get_field( "port" ) port = int(port) if ( server_name == hostname ): if ( port == port_number ): return current_record current_record = current_record.next_record # If not, create one. description = "record1 server network tcp_server \"\" \"\" 0x0 %s %d" \ % ( server_name, port_number ) new_record = record_list.create_record_from_description( description ) new_record.finish_record_initialization() new_record.open_hardware() return new_record #-------------------------------------------------------------------------- def main( record_list, argv ): # Normalize the script name. (filepath, filename) = os.path.split(Mp.__program_name__) (scriptname, extension) = os.path.splitext(filename) Mp.__program_name__ = scriptname # Check to see if we have the correct number of arguments. if ( scriptname == "mpget" ): if ( len(argv) != 1 ): print "\nUsage: mpget nfname\n" sys.exit(1) elif ( scriptname == "mpput" ): if ( len(argv) <= 1 ): print \ "\nUsage: mpput nfname argument1 [argument2 ...]\n" sys.exit(1) elif ( scriptname == "mpnet" ): print \ "\nError: 'mpnet' must be invoked via the symbolic links 'mpget' and 'mpput'" print " rather than being invoked directly.\n" sys.exit(1) else: print "\nError: command name '%s' is unrecognized.\n" \ % scriptname sys.exit(1) # Parse the command line argument. (server_name, port_number, field_name) = parse_nf_string( argv[0] ) server_record = find_server( record_list, server_name, port_number ) nf = MpNum.Net( server_record, field_name ) if ( scriptname == "mpget" ): value = nf.get() print value elif ( scriptname == "mpput" ): nf.put( argv[1:] )