#!/usr/bin/perl # # opensrs.sslbot v4.0 revision 2002.09.03 cmv # # Script to grab all active domains from OpenSRS RWI # # AUTHORS: # - released to the public domain by Tom Brown # - aditional code by: # Bill Gerrard # Colin Viebrock # my $VERSION = "4.0"; my $OPENSRS_LIVE_HOST = "rr-n1-tor.opensrs.net"; my $OPENSRS_BATCH_HOST = "batch.opensrs.net"; my $OPENSRS_USERID = ""; my $OPENSRS_PASSWD = ""; my $WRITE2FILE = 0; my $WRITE2DB = 0; my ($dsn, $dbh, $db_user, $db_pass ); my $db_table = "opensrsactive"; 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 strict; use DBI; use Net::SSLeay qw(get_https post_https sslcat make_headers make_form) ; use vars qw($opt_U $opt_P $opt_l $opt_f $opt_s $opt_t $opt_d $opt_u $opt_p $opt_v $opt_h); use vars qw($reportfile); use Getopt::Std; use File::Basename; getopts('U:P:lf:sd:u:p:t:vh'); usage() if $opt_h; die "OpenSRS user/password not specified\n" if (!$opt_U || !$opt_P); $OPENSRS_USERID = $opt_U; $OPENSRS_PASSWD = $opt_P; die "Must specify at least one of -f FILE or -d DSN\n" if (!$opt_f && !$opt_d); if ($opt_f) { $WRITE2FILE = 1; $reportfile = $opt_f; if ($opt_s) { my ($day, $month, $year) = (localtime)[3,4,5]; my $filedate = sprintf("%04d%02d%02d", $year+1900, $month+1, $day); $reportfile = "$reportfile.$filedate"; } open F, ">$reportfile"; print "\nWriting data to file: $reportfile\n" if ($opt_v); } if ($opt_d) { $WRITE2DB = 1; $dsn = "DBI:$opt_d"; $db_user = $opt_u; $db_pass = $opt_p; $db_table = $opt_t if $opt_t; $dbh = DBI->connect($dsn, $db_user, $db_pass) or die "connecting : $DBI::errstr\n"; print "\nWriting data to database: $dsn:$db_table\n" if ($opt_v); } my $HOSTNAME = $OPENSRS_BATCH_HOST; if ($opt_l) { $HOSTNAME = $OPENSRS_LIVE_HOST; } print "Using OpenSRS server: $HOSTNAME\n" if ($opt_v); my $URI = "/resellers/index"; my $ACTION = "view_domains"; my $AGENT = "opensrs_sslbot/$VERSION"; my $headers = make_headers( 'User-Agent' => $AGENT, 'Cookie' => 'CheckCookie=CheckCookie' ); 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 "HTTP header: %s: %s\n", $_, $reply_headers{$_} if ($opt_v); } my $cookie = $reply_headers{'SET-COOKIE'} or die "Can't get authentication cookie!\n"; print "Logged in - authentication cookie set to $cookie\n" if ($opt_v); $headers = make_headers( 'User-Agent' => $AGENT, 'Cookie' => $cookie ); print "Requesting domain list ...\n" if ($opt_v); ($page, $response, %reply_headers) = post_https( $HOSTNAME, 443, $URI, $headers, "action=view_domains&orderby=name&name=&search2file=1" . "&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 "Request failed: $response\n" unless ($response); print "Got domain list\n" if ($opt_v); if ($WRITE2DB) { print "Emptying database table: $db_table\n" if ($opt_v); &clear_table($db_table); } print "Parsing domain list ...\n" if ($opt_v); my @lines = split( "\n", $page ); foreach (@lines) { next if $_ eq ""; my ($date_reg, $date_exp, $domain, $domain_enc, $cost_i, $cost_t) = split(","); next if ($date_reg eq "Date Registered") || ($date_reg eq "Total:"); print "[ $_ ]\n" if ($opt_v); my $reg_date = &format_date($date_reg); my $exp_date = &format_date($date_exp); my $this_line = "$domain\t$reg_date\t$exp_date"; print "$this_line\n" if ($opt_v); print F "$this_line\n" if ($WRITE2FILE); # write line to file &write_db($domain, $reg_date, $exp_date) if ($WRITE2DB); # write line to database } # close file/db close F if ($WRITE2FILE); $dbh->disconnect if ($WRITE2DB); print "Done!\n" if ($opt_v); exit; 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 (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}; return sprintf("%04d-%02d-%02d", $year, $month, $day); } # format_date sub usage { my $script = basename($0); print <:database=;host=" e.g.: "mysql:database=opensrs;host=localhost" Actually, any DBI DSN should be valid (without the leading "DBI:"). -u DB_USER Database user name. -p DB_PASS Database password. -t TABLE Database table to write to, defaults to "opensrsactive", and needs to be of the form: 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 ); -l Use the live OpenSRS server instead of the batch server (not recommended) -v Be verbose. -h Display this help and exit. Note: you can use both the -f and -d flags simultaneously, to write the report to both a file and the database. EOF exit; } # usage