#!/usr/bin/perl # # opensrs.sslbot v2 revision 2002.03.27 blg # # short script to grab all active domains from OpenSRS RWI # # Released to the public domain by Tom Brown, tbrown@baremetal.com # (but I'd still like to have my name in the comments if you use it. :-) # (additional work done by bill@daze.net) # (patched again in jan 2001, tbrown@baremetal.com to match # OpenSRS search-active-domains RWI changes) # (patched 2001.04.03, bill@daze.net to match # OpenSRS search-active-domains RWI page link changes) # (patched 2002.03.27, bill@daze.net added $OPENSRS_HOST user- # definable hostname for batch RWI or regular RWI; also changed # URI from "/~vpop/resellers/index" to "/resellers/index" # # it will scan the pages of active domains... # and dump the domains (one per line) # to the file: $RPT_PATH/$RPT_NAME.YYYYMMDD # # Original script enhanced by Bill Gerrard to: # # 1) Write tab delimited file (using above file name) containing: # domain-nameregistration-dateexpiration-date # dates are formatted in ISO format: YYYY-MM-DD # # 2) Optionally write the information to a database (tested with mySQL) # Note: This clears the db table and then repopulates it with data # from the OpenSRS RWI each time the script is run. # # Table structure for table 'opensrsactive' # # CREATE TABLE opensrsactive ( # domain varchar(128) DEFAULT '' NOT NULL, # regdate date DEFAULT '0000-00-00' NOT NULL, # expdate date DEFAULT '0000-00-00' NOT NULL # ); # Change the following values to match your system configuration # my $OPENSRS_HOST = 'rr-n1-tor.opensrs.net'; # the live non-batch RWI my $OPENSRS_HOST = 'batch.opensrs.net'; # the batch/scripting okay RWI my $OPENSRS_USERID="resellerid"; # your reseller id my $OPENSRS_PASSWD="resellerpassword"; # your reseller password my $RPT_PATH=""; # report file path my $RPT_NAME="opensrs.active"; # report filename (timestamp is appended) my $WRITE2DB = 0; # 1 = write to database table / 0 = don't write my $DB_DATABASE_NAME = 'opensrs'; # name of database my $DB_TABLE_NAME = 'opensrsactive'; # name of database table my $DB_USER = 'username'; # database username my $DB_PASS = 'password'; # database password # No changes should be required beyond this point... my ($dsn, $dbh); my %ShortMonth = ( 'Jan' => "01", 'Feb' => "02", 'Mar' => "03", 'Apr' => "04", 'May' => "05", 'Jun' => "06", 'Jul' => "07", 'Aug' => "08", 'Sep' => "09", 'Oct' => "10", 'Nov' => "11", 'Dec' => "12" ); ################################################# use DBI; use strict; use Net::SSLeay qw(get_https post_https sslcat make_headers make_form) ; use vars qw($opt_c $opt_a $opt_d); use Getopt::Std; getopts('cad'); # REDUNDANT -c = completed orders # NO LONGER SUPPORTED -a = active domains # -d = debugging on die "completed orders page is no longer supported." if ($opt_c); warn "this script only supports active domains, so -a is redundant\n" if ($opt_a); my $HOSTNAME=$OPENSRS_HOST; my $URI="/resellers/index"; my $ACTION="view_domains"; # used to be view_active ... my $init_cookie = 'CheckCookie=CheckCookie'; # really should load the login page my $AGENT='dazenet automation engine'; my $headers=make_headers( 'User-Agent' => $AGENT, Cookie => $init_cookie ); my ($page, $response, %reply_headers) = post_https($HOSTNAME, 443, $URI, $headers, make_form( 'username' => $OPENSRS_USERID, 'password' => $OPENSRS_PASSWD, 'action' => 'login' )); foreach (keys %reply_headers) { printf "header: %s: %s\n", $_, $reply_headers{$_}; } print "\n$page\n" if ($opt_d); my $cookie = $reply_headers{'SET-COOKIE'} or die "can't get auth cookie!"; print "cookie set to $cookie\n" if ($opt_d); $headers=make_headers( 'User-Agent' => $AGENT, Cookie => $cookie ); # create the log file with date stamp my ($day, $month, $year) = (localtime)[3,4,5]; my $filedate = sprintf("%04d%02d%02d", $year+1900, $month+1, $day); open F, ">$RPT_PATH" . $RPT_NAME . ".$filedate"; my $date = localtime(); print F "Dump started: $date\n"; close F; my $prior_line = ''; if ($WRITE2DB) { &open_db; # initialize and open database connection &clear_table($DB_TABLE_NAME); # clear the table } my @fetchpages = ('0'); my %validpages = (0=>1); while (defined(my $fetchpage = shift(@fetchpages))) { ($page, $response, %reply_headers) = post_https($HOSTNAME, 443, $URI, $headers, "action=view_domains&orderby=createdate&domain=&page=$fetchpage&name=&alpha=&from_day_c=All&from_day_x=All&from_month_c=All&from_month_x=All&from_year_x=All&from_year_c=All&to_day_c=All&to_day_x=All&to_month_c=All&to_month_x=All&to_year_x=All&to_year_c=All" ); die "whoa, request failed: $response" unless ($response); print "\npage $fetchpage is: $page\n\n" if ($opt_d); while ($page =~ m#&page=(\d+)#g) { # changed 20010403 blg my $p = $1; print "found link to page $p\n" if ($opt_d); if (!defined($validpages{$p})) { push(@fetchpages,$p); # schedule it $validpages{$p} = 1; # record the fact it is scheduled } } print "fetched page $fetchpage\n" if ($opt_d); open F, ">>$RPT_PATH" . $RPT_NAME . ".$filedate"; $page =~ s/^.* END HEADER -->//s; # throw away page header... print "stripped header\n" if ($opt_d); my $found = 0; # note the embedded action below is not $ACTION it is the singular form # reg exp while ($page =~ m#\s*]*>(.*?)\s*]*>(.*?)\s*.*?action=view_domain&name=(.*?)">.*?#igs) { print "[$3] [$1] [$2]\n" if ($opt_d); my $domain = $3; my $reg_date = &format_date($1); my $exp_date = &format_date($2); my $this_line = "$domain\t$reg_date\t$exp_date"; print "$this_line\n" if ($opt_d); if ($prior_line ne $this_line) { print F "$this_line\n"; &write_db($domain, $reg_date, $exp_date) if ($WRITE2DB); # write line to database } $prior_line = $this_line; $found = 1; } close F; die "Yikes, I couldn't parse any domains out of this page:\n$page\n" unless $found; } &close_db if ($WRITE2DB); # close database connection open F, ">>$RPT_PATH" . $RPT_NAME . ".$filedate"; $date = localtime(); print F "Dump ended: $date\n"; close F; exit; sub open_db { # defaults for database my $dbi_driver = "mysql"; my $dbi_database = $DB_DATABASE_NAME; my $dbi_hostname = "localhost"; my $dbi_user = $DB_USER; my $dbi_password = $DB_PASS; $dsn = "DBI:$dbi_driver:database=$dbi_database;$dbi_hostname"; $dbh = DBI->connect($dsn, $dbi_user, $dbi_password) or die "connecting : $DBI::errstr\n"; } # open_db sub close_db { $dbh->disconnect; } # close_db sub clear_table { my $table_name = shift; my ($sqlcmd, $sth, $rc); $sqlcmd = qq{ DELETE FROM $table_name }; $sth = $dbh->prepare($sqlcmd); $sth->execute; $rc = $sth->finish; } # clear_table sub write_db { my ($domain, $reg_date, $exp_date) = @_; my ($sqlcmd, $sth, $rc); $sqlcmd = qq{ INSERT INTO $DB_TABLE_NAME (domain, regdate, expdate) VALUES ('$domain', '$reg_date', '$exp_date') }; $sth = $dbh->prepare($sqlcmd); $sth->execute; $rc = $sth->finish; } # write_db sub format_date { my $date_field = shift; my ($month_name, $day, $year) = split(" ", $date_field); my $month = $ShortMonth{$month_name}; chop($day); return sprintf("%04d-%02d-%02d", $year, $month, $day); } # format_date