* Added two new modules; Convert.pm for converting data types and Database.pm for managing all things related to databases.
* Created Convert->cidr() that will convert dotted decimal subnets to CIDR notation and vice versa. * Created Database->get_local_id that returns the ID from striker.conf that matches the local host, if any, * Created Get->network_details that returns the host name and a list of interfaces and their IP addresses (if any). * Created System->call() that takes a shell call and returns the output. * Created icon for the top-right bar that are "on" (lit up). * Created a skeleton striker.conf file. * Created the new scancore-database tool that will manage ScanCore databases. Signed-off-by: Digimer <digimer@alteeve.ca>main
parent
f948d7a762
commit
632f677f04
9 changed files with 522 additions and 9 deletions
@ -0,0 +1,187 @@ |
||||
package AN::Tools::Convert; |
||||
# |
||||
# This module contains methods used to convert data between types |
||||
# |
||||
|
||||
use strict; |
||||
use warnings; |
||||
use Data::Dumper; |
||||
|
||||
our $VERSION = "3.0.0"; |
||||
my $THIS_FILE = "Convert.pm"; |
||||
|
||||
### Methods; |
||||
# cidr |
||||
|
||||
=pod |
||||
|
||||
=encoding utf8 |
||||
|
||||
=head1 NAME |
||||
|
||||
AN::Tools::Convert |
||||
|
||||
Provides all methods related to converting data. |
||||
|
||||
=head1 SYNOPSIS |
||||
|
||||
use AN::Tools; |
||||
|
||||
# Get a common object handle on all AN::Tools modules. |
||||
my $an = AN::Tools->new(); |
||||
|
||||
# Access to methods using '$an->Convert->X'. |
||||
# |
||||
# Example using 'cidr()'; |
||||
my $subnet = $an->Convert->codr({cidr => "24"}); |
||||
|
||||
=head1 METHODS |
||||
|
||||
Methods in this module; |
||||
|
||||
=cut |
||||
sub new |
||||
{ |
||||
my $class = shift; |
||||
my $self = {}; |
||||
|
||||
bless $self, $class; |
||||
|
||||
return ($self); |
||||
} |
||||
|
||||
# Get a handle on the AN::Tools object. I know that technically that is a sibling module, but it makes more |
||||
# sense in this case to think of it as a parent. |
||||
sub parent |
||||
{ |
||||
my $self = shift; |
||||
my $parent = shift; |
||||
|
||||
$self->{HANDLE}{TOOLS} = $parent if $parent; |
||||
|
||||
return ($self->{HANDLE}{TOOLS}); |
||||
} |
||||
|
||||
|
||||
############################################################################################################# |
||||
# Public methods # |
||||
############################################################################################################# |
||||
|
||||
=head2 cidr |
||||
|
||||
This takes an IPv4 CIDR notation and returns the dotted-decimal subnet, or the reverse. |
||||
|
||||
# Convert a CIDR notation to a subnet. |
||||
my $subnet = $an->Convert->cidr({cidr => "24"}); |
||||
|
||||
In the other direction; |
||||
|
||||
# Convert a subnet to a CIDR notation. |
||||
my $cidr = $an->Convert->cidr({subnet => "255.255.255.0"}); |
||||
|
||||
If the input data is invalid, an empty string will be returned. |
||||
|
||||
=head2 Parameters; |
||||
|
||||
There are two parameters, each of which is optional, but one of them is required. |
||||
|
||||
=head3 cidr (optional) |
||||
|
||||
This is a CIDR notation (between 0 and 24) to convert to a dotted-decimal address. |
||||
|
||||
=head3 subnet (optional) |
||||
|
||||
This is a dotted-decimal subnet to convert to a CIDR notation. |
||||
|
||||
=cut |
||||
sub cidr |
||||
{ |
||||
my $self = shift; |
||||
my $parameter = shift; |
||||
my $an = $self->parent; |
||||
|
||||
my $cidr = defined $parameter->{cidr} ? $parameter->{cidr} : ""; |
||||
my $subnet = defined $parameter->{subnet} ? $parameter->{subnet} : ""; |
||||
my $output = ""; |
||||
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { |
||||
cidr => $cidr, |
||||
subnet => $subnet, |
||||
}}); |
||||
|
||||
if ($cidr) |
||||
{ |
||||
# Convert a cidr to a subnet |
||||
if ($cidr eq "0") { $output = "0.0.0.0"; } |
||||
elsif ($cidr eq "1") { $output = "128.0.0.0"; } |
||||
elsif ($cidr eq "2") { $output = "192.0.0.0"; } |
||||
elsif ($cidr eq "3") { $output = "224.0.0.0"; } |
||||
elsif ($cidr eq "4") { $output = "240.0.0.0"; } |
||||
elsif ($cidr eq "5") { $output = "248.0.0.0"; } |
||||
elsif ($cidr eq "6") { $output = "252.0.0.0"; } |
||||
elsif ($cidr eq "7") { $output = "254.0.0.0"; } |
||||
elsif ($cidr eq "8") { $output = "255.0.0.0"; } |
||||
elsif ($cidr eq "9") { $output = "255.128.0.0"; } |
||||
elsif ($cidr eq "10") { $output = "255.192.0.0"; } |
||||
elsif ($cidr eq "11") { $output = "255.224.0.0"; } |
||||
elsif ($cidr eq "12") { $output = "255.240.0.0"; } |
||||
elsif ($cidr eq "13") { $output = "255.248.0.0"; } |
||||
elsif ($cidr eq "14") { $output = "255.252.0.0"; } |
||||
elsif ($cidr eq "15") { $output = "255.254.0.0"; } |
||||
elsif ($cidr eq "16") { $output = "255.255.0.0"; } |
||||
elsif ($cidr eq "17") { $output = "255.255.128.0"; } |
||||
elsif ($cidr eq "18") { $output = "255.255.192.0"; } |
||||
elsif ($cidr eq "19") { $output = "255.255.224.0"; } |
||||
elsif ($cidr eq "20") { $output = "255.255.240.0"; } |
||||
elsif ($cidr eq "21") { $output = "255.255.248.0"; } |
||||
elsif ($cidr eq "22") { $output = "255.255.252.0"; } |
||||
elsif ($cidr eq "23") { $output = "255.255.254.0"; } |
||||
elsif ($cidr eq "24") { $output = "255.255.255.0"; } |
||||
elsif ($cidr eq "25") { $output = "255.255.255.128"; } |
||||
elsif ($cidr eq "26") { $output = "255.255.255.192"; } |
||||
elsif ($cidr eq "27") { $output = "255.255.255.224"; } |
||||
elsif ($cidr eq "28") { $output = "255.255.255.240"; } |
||||
elsif ($cidr eq "29") { $output = "255.255.255.248"; } |
||||
elsif ($cidr eq "30") { $output = "255.255.255.252"; } |
||||
elsif ($cidr eq "31") { $output = "255.255.255.254"; } |
||||
elsif ($cidr eq "32") { $output = "255.255.255.255"; } |
||||
} |
||||
elsif ($subnet) |
||||
{ |
||||
if ($subnet eq "0.0.0.0" ) { $output = "0"; } |
||||
elsif ($output eq "128.0.0.0" ) { $output = "1"; } |
||||
elsif ($output eq "192.0.0.0" ) { $output = "2"; } |
||||
elsif ($output eq "224.0.0.0" ) { $output = "3"; } |
||||
elsif ($output eq "240.0.0.0" ) { $output = "4"; } |
||||
elsif ($output eq "248.0.0.0" ) { $output = "5"; } |
||||
elsif ($output eq "252.0.0.0" ) { $output = "6"; } |
||||
elsif ($output eq "254.0.0.0" ) { $output = "7"; } |
||||
elsif ($output eq "255.0.0.0" ) { $output = "8"; } |
||||
elsif ($output eq "255.128.0.0" ) { $output = "9"; } |
||||
elsif ($output eq "255.192.0.0" ) { $output = "10"; } |
||||
elsif ($output eq "255.224.0.0" ) { $output = "11"; } |
||||
elsif ($output eq "255.240.0.0" ) { $output = "12"; } |
||||
elsif ($output eq "255.248.0.0" ) { $output = "13"; } |
||||
elsif ($output eq "255.252.0.0" ) { $output = "14"; } |
||||
elsif ($output eq "255.254.0.0" ) { $output = "15"; } |
||||
elsif ($output eq "255.255.0.0" ) { $output = "16"; } |
||||
elsif ($output eq "255.255.128.0" ) { $output = "17"; } |
||||
elsif ($output eq "255.255.192.0" ) { $output = "18"; } |
||||
elsif ($output eq "255.255.224.0" ) { $output = "19"; } |
||||
elsif ($output eq "255.255.240.0" ) { $output = "20"; } |
||||
elsif ($output eq "255.255.248.0" ) { $output = "21"; } |
||||
elsif ($output eq "255.255.252.0" ) { $output = "22"; } |
||||
elsif ($output eq "255.255.254.0" ) { $output = "23"; } |
||||
elsif ($output eq "255.255.255.0" ) { $output = "24"; } |
||||
elsif ($output eq "255.255.255.128" ) { $output = "25"; } |
||||
elsif ($output eq "255.255.255.192" ) { $output = "26"; } |
||||
elsif ($output eq "255.255.255.224" ) { $output = "27"; } |
||||
elsif ($output eq "255.255.255.240" ) { $output = "28"; } |
||||
elsif ($output eq "255.255.255.248" ) { $output = "29"; } |
||||
elsif ($output eq "255.255.255.252" ) { $output = "30"; } |
||||
elsif ($output eq "255.255.255.254" ) { $output = "31"; } |
||||
elsif ($output eq "255.255.255.255" ) { $output = "32"; } |
||||
} |
||||
|
||||
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output => $output }}); |
||||
return($output); |
||||
} |
@ -0,0 +1,125 @@ |
||||
package AN::Tools::Database; |
||||
# |
||||
# This module contains methods related to databases. |
||||
# |
||||
|
||||
use strict; |
||||
use warnings; |
||||
use Data::Dumper; |
||||
|
||||
our $VERSION = "3.0.0"; |
||||
my $THIS_FILE = "Database.pm"; |
||||
|
||||
### Methods; |
||||
# get_local_id |
||||
|
||||
=pod |
||||
|
||||
=encoding utf8 |
||||
|
||||
=head1 NAME |
||||
|
||||
AN::Tools::Database |
||||
|
||||
Provides all methods related to managing and accessing databases. |
||||
|
||||
=head1 SYNOPSIS |
||||
|
||||
use AN::Tools; |
||||
|
||||
# Get a common object handle on all AN::Tools modules. |
||||
my $an = AN::Tools->new(); |
||||
|
||||
# Access to methods using '$an->Database->X'. |
||||
# |
||||
# Example using 'get_local_id()'; |
||||
my $local_id = $an->Database->get_local_id; |
||||
|
||||
=head1 METHODS |
||||
|
||||
Methods in this module; |
||||
|
||||
=cut |
||||
sub new |
||||
{ |
||||
my $class = shift; |
||||
my $self = {}; |
||||
|
||||
bless $self, $class; |
||||
|
||||
return ($self); |
||||
} |
||||
|
||||
# Get a handle on the AN::Tools object. I know that technically that is a sibling module, but it makes more |
||||
# sense in this case to think of it as a parent. |
||||
sub parent |
||||
{ |
||||
my $self = shift; |
||||
my $parent = shift; |
||||
|
||||
$self->{HANDLE}{TOOLS} = $parent if $parent; |
||||
|
||||
return ($self->{HANDLE}{TOOLS}); |
||||
} |
||||
|
||||
|
||||
############################################################################################################# |
||||
# Public methods # |
||||
############################################################################################################# |
||||
|
||||
=head2 get_local_id |
||||
|
||||
This returns the database ID from 'C<< striker.conf >>' based on matching the 'C<< database::<id>::host >>' to the local machine's host name or one of the active IP addresses on the host. |
||||
|
||||
# Get the local ID |
||||
my $local_id = $an->Database->get_local_id; |
||||
|
||||
This will return a blank string if no match is found. |
||||
|
||||
=cut |
||||
sub get_local_id |
||||
{ |
||||
my $self = shift; |
||||
my $parameter = shift; |
||||
my $an = $self->parent; |
||||
|
||||
my $local_id = ""; |
||||
my $network_details = $an->Get->network_details; |
||||
foreach my $id (sort {$a cmp $b} keys %{$an->data->{database}}) |
||||
{ |
||||
if ($network_details->{hostname} eq $an->data->{database}{$id}{host}) |
||||
{ |
||||
$local_id = $id; |
||||
last; |
||||
} |
||||
} |
||||
if (not $local_id) |
||||
{ |
||||
foreach my $interface (sort {$a cmp $b} keys %{$network_details->{interface}}) |
||||
{ |
||||
my $ip_address = $network_details->{interface}{$interface}{ip}; |
||||
my $subnet_mask = $network_details->{interface}{$interface}{netmask}; |
||||
foreach my $id (sort {$a cmp $b} keys %{$an->data->{database}}) |
||||
{ |
||||
if ($ip_address eq $an->data->{database}{$id}{host}) |
||||
{ |
||||
$local_id = $id; |
||||
last; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
return($local_id); |
||||
} |
||||
|
||||
|
||||
# =head3 |
||||
# |
||||
# Private Functions; |
||||
# |
||||
# =cut |
||||
|
||||
############################################################################################################# |
||||
# Private functions # |
||||
############################################################################################################# |
@ -0,0 +1,16 @@ |
||||
# This is the main Striker (and ScanCore) configuration file. |
||||
# |
||||
|
||||
database::1::host = 192.168.122.201 |
||||
database::1::port = 5432 |
||||
database::1::name = scancore |
||||
database::1::user = admin |
||||
database::1::password = Initial1 |
||||
database::1::ping_before_connect = 1 |
||||
|
||||
database::2::host = an-striker02.alteeve.com |
||||
database::2::port = 5432 |
||||
database::2::name = scancore |
||||
database::2::user = admin |
||||
database::2::password = Initial1 |
||||
database::2::ping_before_connect = 1 |
@ -0,0 +1,40 @@ |
||||
#!/usr/bin/perl |
||||
# |
||||
# This checks the state of the database server and, if necessary, sets up the database. |
||||
|
||||
use strict; |
||||
use warnings; |
||||
use Data::Dumper; |
||||
use AN::Tools; |
||||
|
||||
my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0]; |
||||
my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0]; |
||||
if (($running_directory =~ /^\./) && ($ENV{PWD})) |
||||
{ |
||||
$running_directory =~ s/^\./$ENV{PWD}/; |
||||
} |
||||
|
||||
my $an = AN::Tools->new(); |
||||
$an->Log->level(2); |
||||
|
||||
$an->Storage->read_config({file => "/etc/striker/striker.conf"}); |
||||
|
||||
# Turn off buffering so that the pinwheel will display while waiting for the SSH call(s) to complete. |
||||
$| = 1; |
||||
|
||||
my $local_id = $an->Database->get_local_id; |
||||
if ($local_id) |
||||
{ |
||||
print "Local ID: [$local_id]\n"; |
||||
} |
||||
else |
||||
{ |
||||
print "Failed to find a local ID, no databases are stored on this machine.\n"; |
||||
} |
||||
|
||||
exit(0); |
||||
|
||||
|
||||
############################################################################################################# |
||||
# Functions # |
||||
############################################################################################################# |
Loading…
Reference in new issue