troubleshooting Question

perl script installation

Avatar of akiles99
akiles99Flag for India asked on
Perl
5 Comments2 Solutions600 ViewsLast Modified:
Hi,

How to install this perl script...

Cheers,
akiles
#!/usr/bin/env perl
 
use 5.008;
use strict;
use warnings;
use Config;
$Config{useithreads} or die('requires threads');
 
use threads;
use threads::shared;
use IO::Socket::INET;
use Time::HiRes qw(usleep);
use POSIX qw(pause);
use Log::Dispatch;
use Log::Dispatch::File;
use Log::Dispatch::Screen;
 
use Nominet;
 
use constant 'DOMAINS_FILE' => 'domains.txt';
use constant 'DAC_HOST' => 'dac.nic.uk';
use constant 'DAC_PORT' => 2043;
use constant 'DAC_DELAY' => 187 * 1000; # in microseconds
 
my @domains :shared;
our $log;
 
&main();
exit;
 
sub main() {
	my $logfile = Log::Dispatch::File->new('name' => 'dac_read', 'min_level' => 'debug', 'filename' => 'dac_read.log');
	my $logscreen = Log::Dispatch::Screen->new('name' => 'dac_read_screen', 'min_level' => 'debug', 'stderr' => 0);
 
	$log = Log::Dispatch->new;
	$log->add($logfile);
	$log->add($logscreen);
 
	$log->log('level' => 'debug' , 'message' => "* main: logging initialized\n");
 
	$SIG{'HUP'} = \&read_domains;
 
	$log->log('level' => 'debug', 'message' => "* main: installed SIGHUP handler\n");
 
	&read_domains();
 
	my $dac_sock = IO::Socket::INET->new('PeerAddr' => DAC_HOST,
					     'PeerPort' => DAC_PORT,
					     'Proto'    => 'tcp'
					    ) or die "IO::Socket::INET->new() failed: $!";
 
	sleep(5);
 
	my $dac_fd = $dac_sock->fileno();
 
	my $dac_write_thr = threads->create(\&dac_write, $dac_fd);
	&dac_read($dac_fd);
 
	$log->log('level' => 'debug', 'message' => "* main: returned from DAC reader. closing DAC sock...\n");
 
	$dac_sock->close();
 
	$log->log('level' => 'debug', 'message' => "* main: closed DAC sock.\n");
 
	$log->log('level' => 'notice', 'message' => "* main: terminating\n");
 
	return;
}
 
sub log_init() {
	my $logfile = Log::Dispatch::File->new('name' => 'epp_file', 'min_level' => 'debug', 'filename' => 'epp.log');
	my $logscreen = Log::Dispatch::Screen->new('name' => 'epp_screen', 'min_level' => 'debug', 'stderr' => 0);
 
	$log = Log::Dispatch->new;
	$log->add($logfile);
	$log->add($logscreen);
 
	return;
}
 
sub dac_read($) {
	my $fd = shift;
 
	$log->log('level' => 'debug', 'message' => "* DAC reader: thread started\n");
 
	my $sock = IO::Socket->new_from_fd($fd, 'r') or $log->log_and_die('level' => 'error', 'message' => "* DAC reader: IO::Socket->new_from_fd() failed: $!\n");
 
	Nominet::connect();
 
	my $epp_update = 0;
 
	$/ = "\r\n";
 
	while (my $result = <$sock>) {
		chomp $result;
 
		$result =~ m/^([^,]+),([YNERIB])(,(.+))?/;
		my ($domain, $status, $rest) = ($1, $2, $4);
 
		if ($status eq 'N') {
			$log->log('level' => 'notice', 'message' => "* result: AVAILABLE ($domain)\n");
			Nominet::create($domain);
		} else {
			if ($status eq 'Y') {
				$log->log('level' => 'info', 'message' => "* result: unavailable ($domain ... $rest)\n");
			} elsif ($status eq 'B') {
				$log->log('level' => 'info', 'message' => "* result: blocked ($domain, $rest seconds)\n");
 
				lock(@domains);
 
				$log->log('level' => 'debug', 'message' => "* block: got lock, blocking for $rest seconds\n");
 
				sleep($rest);
 
				$log->log('level' => 'debug', 'message' => "* block: unblocked\n");
			} elsif (($status eq 'E') || ($status eq 'r') || ($status eq 'I')) {
				$log->log('level' => 'notice', 'message' => "* result: got '$status' ($domain)\n");
			} else {
				$log->log('level' => 'notice', 'message' => "* result: unknown result ($domain ... $status)\n");
			}
 
			$log->log('level' => 'debug', 'message' => "* DAC reader: adding domain '$domain' back to list. getting lock...\n");
 
			{
				lock(@domains);
				push(@domains, lc($domain));
			}
 
			$log->log('level' => 'debug', 'message' => "* DAC reader: lock released\n");
		}
 
		my $time = time();
		my $delta = $epp_update - $time;
		if ($delta <= 0) {
			$log->log('level' => 'debug', 'message' => "* DAC reader: EPP needs noop (time=$time,epp update=$epp_update,delta=$delta)\n");
			$epp_update = $time + ((5 * 60) + 30);
			Nominet::noop();
			$log->log('level' => 'debug', 'message' => "* DAC reader: EPP noop sent\n");
		} else {
			$log->log('level' => 'debug', 'message' => "* DAC reader: EPP doesn't need noop (time=$time,epp update=$epp_update,delta=$delta)\n");
		}
	}
 
	Nominet::disconnect();
 
	return;
}
 
sub dac_write($) {
	my $fd = shift;
 
	my $logfile = Log::Dispatch::File->new('name' => 'dac_write', 'min_level' => 'debug', 'filename' => 'dac_write.log');
	my $logscreen = Log::Dispatch::Screen->new('name' => 'dac_write_screen', 'min_level' => 'debug', 'stderr' => 0);
 
	$log = Log::Dispatch->new;
	$log->add($logfile);
	$log->add($logscreen);
 
	$log->log('level' => 'debug', 'message' => "* DAC writer: logging initialized\n");
 
	$log->log('level' => 'debug', 'message' => "* DAC writer: thread started (fd=$fd)\n");
 
	my $sock = IO::Socket->new_from_fd($fd, 'w') or $log->log_and_die('level' => 'error', 'message' => "* DAC writer: IO::Socket->new_from_fd() failed: $!\n");
 
	for (;;) {
		my $domain;
 
		$log->log('level' => 'debug', 'message' => "* DAC writer: getting lock...\n");
 
		{
			lock(@domains);
			$domain = shift(@domains);
		}
 
		$log->log('level' => 'debug', 'message' => "* DAC writer: lock released\n");
 
		if (defined($domain)) {
			$log->log('level' => 'debug', 'message' => "* DAC writer: sending '$domain'\n");
 
			my $msg = "$domain\r\n";
			syswrite($sock, $msg, length($msg));
			#print $sock "$domain\r\n";
			#$sock->flush();
		} else {
			$log->log('level' => 'debug', 'message' => "* DAC writer: no domains in list\n");
		}
 
		$log->log('level' => 'debug', 'message' => "* DAC writer: sleeping\n");
		usleep(DAC_DELAY);
		$log->log('level' => 'debug', 'message' => "* DAC writer: woke up\n");
	}
 
	return;
}
 
sub read_file($) {
	my $filename = shift;
 
	$log->log('level' => 'debug', 'message' => "* read file: opening $filename\n");
 
	open(IN, "<$filename") or $log->log_and_die('level' => 'error', 'message' => "* read file: open() failed: $!\n");
 
	my @ret;
 
	while (my $line = <IN>) {
		chomp $line;
		push @ret, lc($line);
	}
 
	close(IN);
 
	return @ret;
}
 
sub read_domains() {
	lock(@domains);
	@domains = &read_file(DOMAINS_FILE);
 
	$log->log('level' => 'debug', 'message' => "* read domains: finished reading, list:\n");
 
	foreach my $domain (@domains) {
		$log->log('level' => 'debug', 'message' => "* domain: '$domain'\n");
	}
 
	return;
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros