convert.pl
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:10k
- #!/usr/bin/perl
- # convert.pl
- # Converts standard named files into sql format
- # for the sqlbind-8 project
- #
- # Init the variables
- local @file_contents = ();
- $soa_flag = 0;
- $soa_text = '';
- $previous_name = '';
- $table_name = '';
- $domain = '';
- $CONVERT_FILE = '';
- $OUTPUT_FILE = '';
- $QUIET_MODE = 0;
- $AUTO_REVERSE = 0;
- $quiet_arg = '--quiet';
- $short_quiet_arg = '--q';
- $input_file_arg = '--input_file=';
- $short_input_file_arg = '--input=';
- $output_file_arg = '--output_file=';
- $short_output_file_arg = '-output=';
- $auto_reverse_arg = '--with-auto-reverse';
- $short_auto_reverse_arg = '--auto-reverse';
- foreach $arg ( @ARGV ) {
- if ( $arg eq $quiet_arg || $arg eq $short_quiet_arg ) {
- $QUIET_MODE = 1;
- }
- if ( $arg eq $auto_reverse_arg || $arg eq $short_auto_reverse_arg ) {
- $AUTO_REVERSE = 1;
- }
- if (
- $arg =~ /$input_file_arg(.*)/ ||
- $arg =~ /$short_input_file_arg(.*)/ ) {
- $CONVERT_FILE = $1;
- }
- if (
- $arg =~ /$output_file_arg(.*)/ ||
- $arg =~ /$short_output_file_arg(.*)/ ) {
- $OUTPUT_FILE = $2;
- }
- }
- if ( $CONVERT_FILE ne "" ) {
- # File name passed
- open( INPUT_FILE, $CONVERT_FILE )
- or die "Could not open input file : $CONVERT_FILE, $!n";
- }
- if ( $CONVERT_FILE eq "" || !-f $CONVERT_FILE ) {
- die <<"USAGE";
- convert.pl (Mixture of options)
- Description :
- Converts a zone file to a sql script for usage within
- the sqlbind-8 project.
- Options :
- --input_file=InputFile
- --input=InputFile
- The input file to be converted.
- --output_file=OutputFile
- --output=OutputFile
- The output file created by the conversion process.
- --with-auto-reverse
- --auto-reverse
- The output includes reverse ptr mappings for the in.addr tables.
- !Warning! - There is no checking for table existence, so you may
- have to create them.
- --quiet
- --q
- Turns off confirmation of guessed domains.
- Examples :
- convert.pl --input_file=zerodivide.net.db
- Converts the zone file of zerodivide.net and outputs it to STDOUT
- convert.pl --input_file=zerodivide.net.db --output_file=zerodivide.net.sql
- Converts the zone file of zerodivide.net and outputs it to the
- zerodivide.net.sql file.
- This script brought to you by : Jason "ZeroDiVide" Orcutt
- You can also thank Scott Kamp AKA "TrouBle" for extensive testing of
- the script.
- USAGE
- }
- if ( $OUTPUT_FILE ne "" ) {
- open( OUTPUT_FILE, "> " . $OUTPUT_FILE )
- or die "Could not open output file : $OUTPUT_FILE, $!n";
- select(OUTPUT_FILE );
- $| = 1;
- }
- while( $input_line = <INPUT_FILE> ) {
- # Read a line from stdin
- chomp( $input_line );
- # Charlie test this line below -- Removes ^M
- $input_line =~ s/rr//g;
- ( $content, $comment ) = split( ';', $input_line );
- if ( $content =~ /SOA/i ) {
- $soa_flag = 1;
- $soa_text = $content;
- next;
- }
- if ( $soa_flag == 1 && $content !~ /)/ ) {
- $soa_text .= $content;
- next;
- }
- if ( $soa_flag == 1 && $content =~ /)/ ) {
- $soa_text .= $content;
- $content = $soa_text;
- $soa_flag = 2;
- }
- $content =~ s/t/ /g;
- while( $content =~ / / ) {
- $content =~ s/ / /g;
- }
- $content =~ s/^ //;
- if ( $content eq "" ) { next; }
- # Process the line
- if (
- $content =~ /(.*)sINsSOAs(.*)s(.*)s(s(d*)s(d*)s(d*)s(d*)s(d*)s)/ ||
- $content =~ /(.*)sINsSOAs(.*)s(.*)s(s(d*)s(d*)s(d*)s(d*)s(d*))/ ) {
- local $origin = $1;
- $domain = $2;
- local $contact = $3;
- local $serial = $4;
- local $refresh = $5;
- local $retry = $6;
- local $expire = $7;
- local $min_ttl = $8;
- $domain =~ s/.$//g;
- $contact =~ s/.$//g;
- if ( $domain == '@' ) {
- print STDERR "Whoa darlin we were not able to guess the domain!n";
- print STDERR "Maybee it's within the file name : $CONVERT_FILEn";
- print STDERR "I'm trying to guess it.n";
- if ( $CONVERT_FILE ne "" ) {
- $input_domain = "";
- if ( $QUIET_MODE == 1 ) {
- $input_domain = $CONVERT_FILE;
- # Splice off the .db
- $input_domain =~ s/.db$//;
- # Splice off the .hosts
- $input_domain =~ s/.hosts$//;
- # Splice off the .dom
- $input_domain =~ s/.dom$//;
- } else {
- # Prompt em
- while( $input_domain eq "" ) {
- $input_domain = $CONVERT_FILE;
- print STDERR "Removing extensions (.db, .hosts, .dom)n";
- # Splice off the .db
- $input_domain =~ s/.db$//;
- # Splice off the .hosts
- $input_domain =~ s/.hosts$//;
- # Splice off the .dom
- $input_domain =~ s/.dom$//;
- print STDERR "Domain name [ $input_domain ] : ";
- $t_domain = <STDIN>;
- chomp( $t_domain );
- if ( $t_domain ne "" ) {
- $input_domain = $t_domain;
- } else {
- # We guessed correctly
- }
- } # End while
- } #end if not quiet
- $domain = $input_domain;
- } else {
- print STDERR "Unable to guess it. I really tried hard. Sorryn";
- die "Exitingn";
- }
- }
- print "#n";
- print "# Domain converted with convert.pln";
- print "# By: Jason "ZeroDiVide" Orcuttn";
- print "# " . $input_line . "n";
- print "#n";
- print "# Origin - $originn";
- print "# Domain - $domainn";
- print "# Contact - $contactn";
- print "# Serial - $serialn";
- print "# Refresh - $refreshn";
- print "# Retry - $retryn";
- print "# Expire - $expiren";
- print "# Min TTL - $min_ttln";
- print "#n";
- $previous_name = $domain;
- $table_name = $domain;
- $table_name =~ s/-/__/g;
- $table_name =~ s/./_/g;
- print <<"TABLE_DEF";
- CREATE TABLE $table_name (
- sqlID BIGINT DEFAULT '0' NOT NULL AUTO_INCREMENT,
- sqlOrigin TEXT DEFAULT '' NOT NULL,
- sqlOwner TEXT DEFAULT '',
- sqlClass TEXT DEFAULT '' NOT NULL,
- sqlTTL BIGINT DEFAULT '0',
- sqlType TEXT DEFAULT '' NOT NULL,
- sqlPref INT DEFAULT '0',
- sqlData TEXT DEFAULT '' NOT NULL,
- sqlTime timestamp(14),
- sqlComment TEXT,
- sqlSerialNumber BIGINT DEFAULT '0' NOT NULL,
- sqlRefresh BIGINT DEFAULT '0' NOT NULL,
- sqlRetry BIGINT DEFAULT '0' NOT NULL,
- sqlExpire BIGINT DEFAULT '0' NOT NULL,
- sqlMinTTL BIGINT DEFAULT '0' NOT NULL,
- PRIMARY KEY (sqlID),
- INDEX( sqlID )
- );
- TABLE_DEF
- insert_record(
- $domain, '@', 'IN', '', 'SOA', '', $domain, 'NULL', '',
- $serial, $refresh, $retry, $expire, $min_ttl
- );
- next;
- }
- if ( $content =~ /INsNS(.*)/ ) {
- $server = $1;
- $server =~ s/.$//;
- print "# $input_linen";
- insert_record( $domain, '', 'IN', '', 'NS', '', $server, 'NULL', $comment );
- next;
- }
- $name = '';
- $target_addr = '';
- $mx_weight = '';
- $mx_target = '';
- if ( $content =~ /^INsAs(.*)/ ) {
- $target_addr = $1;
- $target_addr =~ s/.$//;
- print "# $input_linen";
- insert_record( $domain, $previous_name, 'IN', '', 'A', '', $target_addr, 'NULL', $comment );
- next;
- }
- if ( $content =~ /^INsMXs(d*)s(.*)/ ) {
- $mx_weight = $1;
- $mx_target = $2;
- $mx_target =~ s/.$//;
- $mx_weight =~ s/.$//;
- print "# $input_linen";
- insert_record( $domain, $previous_name, 'IN', '', 'MX', $mx_weight, $mx_target, 'NULL', $comment );
- next;
- }
- if ( $content =~ /(.*)sINsAs(.*)/ ) {
- $name = $1;
- $target_addr = $2;
- $name =~ s/.$//;
- $target_addr =~ s/.$//;
- $previous_name = $name;
- print "# $input_linen";
- insert_record( $domain, $name, 'IN', '', 'A', '', $target_addr, 'NULL', $comment );
- next;
- }
- if ( $content =~ /(.*)sINsMXs(d*)s(.*)/ ) {
- $name = $1;
- $mx_weight = $2;
- $mx_addr = $2;
- $name =~ s/.$//;
- $mx_weight =~ s/.$//;
- $mx_addr =~ s/.$//;
- $previous_name = $name;
- print "# $input_linen";
- insert_record( $domain, $name, 'IN', '', 'MX', $mx_weight, $mx_target, 'NULL', $comment );
- next;
- }
- if ( $content =~ /(.*)sINsCNAMEs(.*)/ ) {
- $name = $1;
- $target_addr = $2;
- $name =~ s/.$//;
- $target_addr =~ s/.$//;
- $previous_name = $name;
- print "# $input_linen";
- insert_record( $domain, $name, 'IN', '', 'CNAME', '', $target_addr, 'NULL', $comment );
- next;
- }
- if ( $content =~ /(d*)sINsPTRs(.*)/ ) {
- $octet = $1;
- $target_name = $2;
- $octet =~ s/.$//;
- $target_name =~ s/.$//;
- print "# $input_linen";
- insert_record( $domain, $octet, 'IN', '', 'PTR', '', $target_name, 'NULL', $comment );
- }
- print "# -- " . $input_line . "n";
- }
- close( INPUT_FILE );
- if ( $OUTPUT_FILE ne "" ) {
- close( OUTPUT_FILE );
- }
- #### - subs - ####
- sub insert_record {
- local $origin = shift;
- local $owner = shift;
- local $class = shift;
- local $ttl = shift;
- local $type = shift;
- local $pref = shift;
- local $data = shift;
- local $time = shift;
- local $comment = shift;
- local $serial_number = shift;
- local $refresh = shift;
- local $retry = shift;
- local $expire = shift;
- local $min_ttl = shift;
- $sql =
- 'INSERT INTO ' .
- $table_name .
- ' ( sqlOrigin, sqlOwner, sqlClass, sqlTTL, sqlType, sqlPref, sqlData, sqlTime, sqlComment';
- if ( $serial_number ne '' ) {
- $sql .= ',
- sqlSerialNumber, sqlRefresh, sqlRetry, sqlExpire, sqlMinTTL';
- }
- $sql .= ' ) VALUES ( ' ;
- $sql .= <<"VALS";
- "$origin", "$owner", "$class", "$ttl",
- "$type", "$pref", "$data", NOW(), "$comment"
- VALS
- if ( $serial_number ne '' ) {
- $sql .= <<"SQL_STMT";
- , "$serial_number", "$refresh", "$retry", "$expire", "$min_ttl"
- SQL_STMT
- }
- $sql .= ');';
- print $sql . "nn";
- }