perl script to generate server round-robin assignments



#!/usr/bin/perl

##################
### Main Begin ###
##################

if ($#ARGV < 0) {
&DoUsage;
exit;
}

my ($hostname, $limit) = @ARGV; # Script inputs

if ($hostname =~ /^([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)$/) { # Filter
on IP address
$hostString=$hostname; # Save IP
} else {
($hostString) = split /\./,$hostname; # Obtain first portion
of FQDN component
}

@B = qw(svr01 svr02 svr03 svr04 svr05 svr06); # Array containing
assignable server
$binaryNumber = DoAsc2bin($hostString); # Convert string
into base2
$binaryNumber =~ s/\s+//g; # Remove spaces
from base2 string
$decString = DoBin2dec($binaryNumber); # Convert base2
string to base10
$assignment = DoAssign(scalar(@B), $decString, $hostname, $limit); #
Call subroutine to get assignments

print "$hostname: $assignment\n";
exit;

########################
### Subroutine Begin ###
########################

sub DoUsage() {

print <<EOM;


Generates assignment values where servers in list-A need to
communicate with all or some of the servers in list-B. The input to
the script is a single server hostname or IP from list-A. This server
name will go through a conversion (ascII->Binary->Dec), then the order
of the assignments will be made. As long as no two servers have the
same hostname, the assignment will be varied across the list to ensure
the servers in List-B are distributed evenly across those in List-A.

Usage: $0 <hostname|IP> <entry limit>

EOM
}

sub DoAsc2bin { # Convert ASCII string to binary equivalent
my ($string) = @_; # Input
my @bytes; # Declare byte array
for (split //, $string) { # Run throuh for loop per character
of string being split
push @bytes, unpack "B8", $_; # Store binary equivalent of
each character into @bytes
}
return wantarray ? @bytes : join " ", @bytes; # Return @bytes or
a string of all content of @bytes
}

sub DoBin2dec {
return unpack("N", pack("B32", substr("0" x 32 . shift, -32))); #
Converts binary string to decimal equivalent
}

sub DoAssign {
my ($numListB, $num, $host, $lim) = @_; # Function inputs
$lim ||= 100; # Set lim=100 unless otherwise specified
@B = reverse(@B); # Reverse array, then pop last
my $popNum = $num % $numListB; # Get modulus, then pop that
many elements from reversed array
while ( $popNum != 0 ) { #
Iterate until all values have been popped off array
push(@popSave, pop(@B)); # Save popped elements
$popNum--; # Decrement pop counter
}
@B = reverse(@B); # Reverse array
push(@B, @popSave); # Appennd popped elements to
reversed (original direction) array

for( $i = 0; $i < scalar(@B); $i++) { # Loop
through all array elements, maintain counter
if( $B[$i] eq $host ) { # Look for match
#print "Match on $host\n"; # Debug statement
@B = ($B[$i], @B[0..($i-1)], @B[($i+1)..scalar
(@B)]); # Modify array order by moving the "match" to front

last; # Move on
}
}
return join " ", @B[0..($lim-1)]; # Return space
deleimited string
}
.



Relevant Pages

  • Re: Can someone recommend a product?
    ... string before the site will allow them to proceed. ... I don't know of a product that does this for mail servers. ... I did work at AIG with 2 postfix servers running on solaris. ... Postfix scanned for viruses and spams, then relayed 'clean' emails to exchange, lotus and groupwise servers. ...
    (microsoft.public.exchange.admin)
  • Re: List SQL servers in a network
    ... > private static extern short SQLAllocHandle(short hType, IntPtr ... > public static string[] GetServers{ ... > throw new ApplicationException("Unabled to aquire SQL Servers from ODBC ...
    (microsoft.public.dotnet.general)
  • Re: SQL Agent Job CmdExec - .net Console Exe Fails
    ... The local system account won't be able to authenticate to other servers. ... test_demo_in_dataLoad.demo.Lists.GetListItems(String listName, String ... viewName, XmlNode query, XmlNode viewFields, String rowLimit, XmlNode ...
    (microsoft.public.sqlserver.server)
  • dhcp3 failover protocol failure
    ... I am running two debian woody dhcp3 servers ... The pool is about 6 times larger than the total number of machines. ... option option-160 code 160 = string; ... # Fixed IP addresses can also be specified for hosts. ...
    (Debian-User)
  • Re: Ada Interfaces and the Liskov Substitution Principle
    ... Because syntactically banned assignments are indistinguishable from legal ... There are many aspects on which you could parameterize String and ... This is exactly the reason why I want to keep them all in one class. ... It means that the language is unable to express assignments in its own ...
    (comp.lang.ada)

Loading